iPhone SDK:附近位置功能

时间:2011-11-03 11:09:59

标签: iphone mkmapview

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

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

更新:

我得到了部分工作 - 它只显示20KM范围内的业务,这很好,我遇到的问题是,当他们打开这个附近的企业视图时,它需要相当长的时间来完成每个业务和检查用户与每个业务之间的距离。有什么方法可以加快速度吗?这是我的代码:,这两个方法都是从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];   
}

1 个答案:

答案 0 :(得分:0)

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

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

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