过滤 MKMapView
Posted
技术标签:
【中文标题】过滤 MKMapView【英文标题】:Filtering MKMapView 【发布时间】:2016-05-23 17:57:41 【问题描述】:我有一个 MKMapView,它显示 JSON 文件中的一些注释并显示用户当前位置5 英里..
基本上就像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;
【问题讨论】:
【参考方案1】:使用记录的坐标,用初始化器创建一个CLLocation
对象
init(latitude latitude: CLLocationDegrees, longitude longitude: CLLocationDegrees)
从那里,使用实例方法
distanceFromLocation(_:CLLocation)
使用用户位置获取注释与用户之间的距离(以米为单位)。之后,从米到英里的简单转换(除以 1609.34) 应该可以很好地过滤掉哪些注释是可见的。祝你好运!
【讨论】:
@Aryan,我很想知道这个解决方案是如何为你工作的。您需要进一步的帮助吗?请回复。以上是关于过滤 MKMapView的主要内容,如果未能解决你的问题,请参考以下文章