2016-05-23 2 views
0

私はJSONファイルからいくつかの注釈を表示し、ユーザの現在の位置を示すMKMapViewを持っています。ユーザが現在の位置からどのくらい離れているかを選ぶことができる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

(メートル単位)距離を取得するユーザ位置でインスタンスメソッド

distanceFromLocation(_:CLLocation) 

を使用し、そこから初期

​​3210

CLLocationオブジェクトを作成し、レコードの座標を用いてアノテーションはユーザからのものである。その後、メーターからマイルへの単純な変換(1609.34で割る)は、どのアノテーションが表示されているかを実際にうまくフィルタリングすることができます。がんばろう!

+0

@Aryan、私はこの解決策があなたのためにどのように機能したかを知ることに興味があります。それ以上の助けが必要ですか?答えてください。 –