2016-05-23 22 views
0

我有一個MKMapView顯示一些JSON文件的註釋,並顯示用戶當前位置我想在mapView中有一個過濾器,用戶可以選擇距離他目前的位置有多遠,他希望MKAnnotations顯示直到例5英里。。過濾MKMapView

基本上就像在Foursquare上,你可以過濾你想從你的位置多遠餐館中顯示

這是涉及到的問題我目前的主要符號映射過濾..

@interface ViewController() <MKMapViewDelegate , CLLocationManagerDelegate> 

@property (weak, nonatomic) IBOutlet MKMapView *mapView; 
@property (weak, nonatomic) CLLocationManager *locationManager; 

@end 

@implementation ViewController 

@synthesize mapView; 

/* 
Establish MapView delegate 
Add the Gesture Recogniser to the Map 
*/ 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.mapView setDelegate:self]; 
    //[self addGestureRecogniserToMapView]; 

    mapView.showsUserLocation= YES; 
    mapView.showsTraffic = YES; 
    mapView.showsBuildings = YES; 

    __block NSArray *annoations; 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    annoations = [self parseJSONCities]; 

    dispatch_async(dispatch_get_main_queue(), ^(void) { 

     [self.mapView addAnnotations:annoations]; 
     [self.mapView showAnnotations:annoations animated:YES]; 

    }); 
}); 

    if ([CLLocationManager locationServicesEnabled]) { 
     self.locationManager.delegate = self; 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     [self.locationManager startUpdatingLocation]; 
     [self.locationManager requestWhenInUseAuthorization]; 
     NSLog(@"Location :)"); 
    } else { 
     NSLog(@"-)_"); 
    } 


    } 


- (NSMutableArray *)parseJSONCities{ 

    NSMutableArray *retval = [[NSMutableArray alloc]init]; 
    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"capitals" 
                 ofType:@"json"]; 
    NSData *data = [NSData dataWithContentsOfFile:jsonPath]; 
    NSError *error = nil; 
    NSArray *json = [NSJSONSerialization JSONObjectWithData:data 
                options:kNilOptions 
                 error:&error]; 

    for (JFMapAnnotation *record in json) { 

     JFMapAnnotation *temp = [[JFMapAnnotation alloc]init]; 
     [temp setTitle:[record valueForKey:@"Capital"]]; 
     [temp setSubtitle:[record valueForKey:@"Country"]]; 
     [temp setCoordinate:CLLocationCoordinate2DMake([[record valueForKey:@"Latitude"]floatValue], [[record valueForKey:@"Longitude"]floatValue])]; 
     [retval addObject:temp]; 

    } 

    return retval; 
} 

回答

0

使用記錄的座標,創建一個CLLocation對象與初始化

​​3210

從那裏,使用實例方法

distanceFromLocation(_:CLLocation) 

與用戶的位置來獲得距離(以米爲單位)該註釋來自用戶。之後,從米到英里的簡單轉換(除以1609.34)應該塑造你真正好,以過濾哪些註釋是可見的。祝你好運!

+0

@Aryan,我很想知道這個解決方案是如何爲你制定的。你需要進一步的幫助嗎?請回復。 –