2013-02-27 135 views
-2

在我的應用程序中,我有很多位置,並且在當前位置的基礎上,我需要篩選結果並僅顯示5英里半徑內的結果。 有沒有辦法實現這?如何根據座標過濾數據?

感謝,

+0

三角。查找點之間的距離。除了這個註釋之外,因爲你沒有告訴我們你的數據是如何存儲的。你如何搜索它(nspredicate?),類似的東西。多一點信息會有所幫助。 – Bergasms 2013-02-27 00:40:04

回答

2

CLLocation有distanceFromLocation,可以這樣調用

CLLocation dist = [locationA distanceFromLocation:locationB]; 
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]]; 

} 
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; 
}