【问题标题】:Issue with mapKit and annotationsmapKit 和注释的问题
【发布时间】:2014-08-26 12:47:14
【问题描述】:
我在这里有一个使用 MapKit 的应用程序,我为它绘制了一些叠加层和注释。
它们确实出现了,但我必须先移动地图或在绘制它们之前稍微放大/缩小。
有什么解决办法吗?
【问题讨论】:
标签:
xcode
annotations
mapkit
overlays
【解决方案1】:
只需调用它来进行地图缩放和适合注释:-
[self zoomToFitMapAnnotations:self.mapView];
-
(void)zoomToFitMapAnnotations:(MKMapView*)aMapView {
if([aMapView.annotations 计数] == 0)
返回;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(PlaceMark *annotation in aMapView.annotations) {
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
MKCoordinateRegion 区域;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // 在边上添加一点额外的空间
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // 在边上添加一点额外的空间
region = [aMapView regionThatFits:region];
[aMapView setRegion:region Animation:YES];
}