【问题标题】:Zoom to annotations not user location using Mapkit使用 Mapkit 缩放到注释而不是用户位置
【发布时间】:2013-11-21 18:27:24
【问题描述】:
我需要我的应用程序放大地图上填充的注释,而不是用户的位置。这是我会使用的方法吗:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
或
-(void)viewWillAppear
因此,我还需要地图不仅可以放大注释,还需要确保注释适合地图视图。我查看了一些示例,但似乎找不到适合我的答案。有什么想法吗?
【问题讨论】:
标签:
ios
objective-c
mapkit
【解决方案1】:
好的,所以我应该更清楚我的问题,但基本上我想通了。因此,如果您想在地图中添加注释,并在加载它们时缩放到这些注释,那么无论您是从 Web 获取 JSON 数据还是在初始化时定义注释,您都需要执行以下操作:
CLLocationCoordinate2D annotationCoordinate =
CLLocationCoordinate2DMake([latDest doubleValue],
[lngDest doubleValue]);
Annotation *annotation = [[Annotation alloc] init];
annotation.coordinate = annotationCoordinate;
[self.mapView setCenterCoordinate:annotationCoordinate animated: YES];
[self.mapView addAnnotation:annotation];
[self.mapView setCenterCoordinate:annotationCoordinate animated: YES];
[self.mapView addAnnotation:annotation];
我创建了一个注释类,其中包含从 JSON 数据收集的每个注释的属性,但最重要且与此问题最相关的是坐标数据,这正是我们所需要的。所以我将我的坐标定义为:
annotationCoordiate
最后在向地图添加注释后,我将地图置于从我的数据中提取的位置的中心:
[self.mapView setCenterCoordinate:annotationCoordinate animated: YES];
希望这是有道理的!谢谢!