【问题标题】:iPhone SDK: Nearby Locations FeatureiPhone SDK:附近位置功能
【发布时间】:2025-12-20 09:35:06
【问题描述】:

我有一个 iphone 应用程序,它使用 MKMapView 在 Google 地图上显示企业列表及其位置。作为一项新功能,我正在尝试添加一个“附近”功能,它可以获取用户的当前位置,并在 10 公里附近的谷歌地图上显示几个地图注释。目前,企业位置作为地址字符串存储在 mysql 数据库中,然后对其进行检索和地理编码以显示在谷歌地图上。

我该怎么做呢?如果周围有任何教程,请指点我一个。 (我没有找到任何运气)。 提前致谢!

更新:

我让它部分工作 - 它只显示 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];   
}

【问题讨论】:

  • 源数据是否有可能嵌入 GPS 位置及其地址?或者更好的是,您调用的 Web 服务可提供包含所有业务的摘要。如果您有太多无法合理地放入一个响应中,那么您也有太多需要单独循环。

标签: iphone mkmapview


【解决方案1】:

这并不难。您可以获取所有数据,计算您与该位置之间的距离。下一步是将距离与 maxDistance(搜索半径)进行比较。如果距离

我认为这可以通过很少的可可知识来实现​​...

如果您需要更具体的内容,请开始编码,我会为您提供帮助。

【讨论】: