2011-11-03 77 views
2

我有一个iPhone应用程序,它显示了一个企业名单和他们在谷歌地图上的位置使用MKMapView。作为一项新功能,我尝试添加“附近”功能,从而获取用户当前的位置,并在10KM附近的谷歌地图上显示几个地图注释。目前,企业位置以地址字符串的形式存储在mysql数据库中,然后将其检索并进行地理编码以在谷歌地图上显示。iPhone SDK:附近的位置功能

我该如何去做这件事?如果有任何教程,请将我指向一个。 (我没有找到任何运气)。 在此先感谢!

UPDATE:

我得到它的部分工作 - 它只是一个半径20公里,这是伟大内显示的企业,我遇到的问题是,当他们打开这个附近的商家看来,这需要相当长的时间它要通过每项业务并检查用户与每个单独业务之间的距离。有什么办法可以加快速度吗?这里是我的代码:,这两个方法都是从viewDidLoad方法中调用的。

-(void)getMapData 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 

    NSInteger *bizMax = [[numOfBiz objectAtIndex:0]intValue]; 

    int x = 0; 
    while(x < bizMax) 
    { 

     NSURL *mapURL = [NSURL URLWithString:[NSString stringWithFormat:@"getBizMap-xml.php?biz_id=%@",[bizIDS objectAtIndex:x]]]; 
     NSMutableArray *mapArray = [[NSMutableArray alloc] initWithContentsOfURL:mapURL]; 
     self.mapLocation = mapArray; 
     self.mapStringLocation = [mapArray objectAtIndex:0]; 
     NSLog(@"Location: %@",mapStringLocation); 
     [mapArray release]; 

     CLLocationCoordinate2D trueLocation = [self getLocationFromAddressString:mapStringLocation]; 
     AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:trueLocation]; 

     CLLocation *bizLoc = [[CLLocation alloc] initWithLatitude:trueLocation.latitude longitude:trueLocation.longitude]; 

     CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:nearbyMapView.userLocation.coordinate.latitude longitude:nearbyMapView.userLocation.coordinate.longitude]; 
     //double distance = [userLocation getDistanceFrom: bizLoc]/1000; 

     double kilometers = [userLocation distanceFromLocation:bizLoc]/1000; 

     NSLog(@"Distance from %@ to current location is %g",mapStringLocation,kilometers); 

     if(kilometers < maxDistance) 
     {  
      [self.nearbyMapView addAnnotation:addAnnotation]; 
      [addAnnotation release]; 
     } 

     x++; 
    } 

    [pool release];   
} 

-(void)getNumOfBusinesses 
{  
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 

    NSString *numOfBizJSON = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"numOfBiz.php"]]]; 

    NSString *bizIDSJSON = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"allBizIDSJSON.php"]]]; 

    SBJsonParser *parser = [[SBJsonParser alloc]init]; 
    numOfBiz = [[parser objectWithString:numOfBizJSON error:nil]copy]; 
    bizIDS = [[parser objectWithString:bizIDSJSON error:nil]copy]; 
    NSLog(@"biz id 1 = %@",[bizIDS objectAtIndex:0]); 
    [parser release]; 
    [numOfBizJSON release]; 
    [bizIDSJSON release]; 

    [pool release]; 
} 
+0

源数据是否可能嵌入GPS位置以及他们的地址?或者更好的是,您打电话给的web服务提供一个摘要,其中包括所有业务中的一个命中。如果你有太多不合适的答案,那么你就有太多的问题需要单独循环。 – Craig

回答

0

这并不难。您可以获取所有数据,计算出您与该位置之间的距离。下一步将比较距离和最大距离(搜索半径)。如果距离为< maxDistance,则添加可以显示该位置。

我认为这可能用很少的可可知识来实现​​......

如果您需要更具体的东西,开始编码,我会帮你。