缩放MKMapView以适合注释引脚? [英] Zooming MKMapView to fit annotation pins?
问题描述
我使用MKMapView并在地图上添加了大量5-10公里的注释针脚。当我运行应用程序时,我的地图开始缩小以显示整个世界,什么是缩放地图的最佳方式,以便引脚适合视图?
I am using MKMapView and have added a number of annotation pins to the map about a 5-10 kilometre area. When I run the application my map starts zoomed out to show the whole world, what is the best way to zoom the map so the pins fit the view?
EDIT:
我的初步想法是使用MKCoordinateRegionMake并从我的注释计算坐标中心,经度和纬度。我确定这将工作,但我只是想检查我没有丢失任何明显的。
My initial thinking would be to use MKCoordinateRegionMake and calculate the coordinate centre, longitudeDelta and latitudeDelta from my annotations. I am pretty sure this will work, but I just wanted to check I was not missing anything obvious.
代码添加,BTW:FGLocation是一个符合 MKAnnotation
,locationFake是这些对象的 NSMutableArray
。
Code added, BTW: FGLocation is an class that conforms to MKAnnotation
, locationFake is an NSMutableArray
of these objects. Comments are always welcome ....
- (MKCoordinateRegion)regionFromLocations {
CLLocationCoordinate2D upper = [[locationFake objectAtIndex:0] coordinate];
CLLocationCoordinate2D lower = [[locationFake objectAtIndex:0] coordinate];
// FIND LIMITS
for(FGLocation *eachLocation in locationFake) {
if([eachLocation coordinate].latitude > upper.latitude) upper.latitude = [eachLocation coordinate].latitude;
if([eachLocation coordinate].latitude < lower.latitude) lower.latitude = [eachLocation coordinate].latitude;
if([eachLocation coordinate].longitude > upper.longitude) upper.longitude = [eachLocation coordinate].longitude;
if([eachLocation coordinate].longitude < lower.longitude) lower.longitude = [eachLocation coordinate].longitude;
}
// FIND REGION
MKCoordinateSpan locationSpan;
locationSpan.latitudeDelta = upper.latitude - lower.latitude;
locationSpan.longitudeDelta = upper.longitude - lower.longitude;
CLLocationCoordinate2D locationCenter;
locationCenter.latitude = (upper.latitude + lower.latitude) / 2;
locationCenter.longitude = (upper.longitude + lower.longitude) / 2;
MKCoordinateRegion region = MKCoordinateRegionMake(locationCenter, locationSpan);
return region;
}
推荐答案
。查找您的最大和最小纬度和经度,应用一些简单的算术,并使用 MKCoordinateRegionMake
。
You've got it right. Find your maximum and minimum latitudes and longitudes, apply some simple arithmetic, and use MKCoordinateRegionMake
.
(参考MKMapView.h)://定位地图,使得提供的注释数组尽可能全部可见。
For iOS 7 and above (Referring MKMapView.h) : // Position the map such that the provided array of annotations are all visible to the fullest extent possible.
- (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated NS_AVAILABLE(10_9,7_0);
这篇关于缩放MKMapView以适合注释引脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!