在我的应用程序中,我有很多位置,并且根据当前位置,我需要过滤结果并仅显示5英里半径范围内的结果。 有没有办法实现这个?
谢谢,
答案 0 :(得分:2)
CLLocation有distanceFromLocation,可以像
一样调用CLLocation dist = [locationA distanceFromLocation:locationB];
答案 1 :(得分: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]];
}
答案 2 :(得分: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;
}