-3

在我的应用程序中,我有很多位置,根据当前位置,我需要过滤结果并仅显示半径 5 英里内的结果。有没有办法做到这一点?

谢谢,

4

3 回答 3

2

CLLocation 有 distanceFromLocation,可以这样调用

CLLocation dist = [locationA distanceFromLocation:locationB];
于 2013-02-27T00:41:22.573 回答
1

试试这个功能。这将对您有所帮助。

-(void)distanceBetween{


double latitude1 = [merchant.latitude doubleValue];
double longitude1 = [merchant.longitude doubleValue];

NSUserDefaults *userLocation = [NSUserDefaults standardUserDefaults];
float lat = [userLocation floatForKey:@"latitude"];
float lon = [userLocation floatForKey:@"longitude"];


CLLocation *locA = [[CLLocation alloc] initWithLatitude:latitude1 longitude:longitude1];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:lat longitude:lon];

CLLocationDistance distanceTo = [locA distanceFromLocation:locB];
NSLog(@"locaA is %f", locA);
NSLog(@"locaB is %f", locB);
NSLog(@"locdistnace is %f", distanceTo);
[[self distance] setText:[NSString stringWithFormat:@"%0.2f miles", distanceTo/1609.34]];

}
于 2013-02-27T00:41:45.843 回答
1

像这样的东西应该可以工作,但还没有测试过:

- (NSArray *)filterArrayOfLocations:(NSArray *)array byDistance:(CLLocationDistance)distance toCurrentLocation:(CLLocation*)currentLocation
{
    NSArray *filteredArray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        return [currentLocation distanceFromLocation:(CLLocation *)evaluatedObject] < distance;
    }]];
    return filteredArray;
}
于 2013-02-27T00:48:40.767 回答