在iOS地图上为多个位置自动缩放和定位

时间:2013-08-12 00:01:30

标签: ios map latitude-longitude

我正在开发iOS应用程序,我正在尝试实现这一点,给定了多个位置(纬度和经度),我想自动缩放我的地图并重新定位我的地图视图以包含所有位置。

我提出的解决方案是,给定任意数量的位置,我可以使用for循环来查找最高和最低的纬度和经度。并且中心点应该是lat / lng的最高/最低之间的中心位置。使用MKCoordinateRegionMakeWithDistance,结合核心位置框架(distanceFromLocation),我可以计算最远位置和中心位置之间的距离,并在地图上显示该区域。

我的问题是,有没有更好的解决方案或框架可以解决这个问题?是否有任何我能找到的例子可以达到这个目的?

2 个答案:

答案 0 :(得分:2)

如果您知道纬度和经度的最高和最低值,那么使用MKCoordinateRegionMake代替MKCoordinateRegionMakeWithDistance至少可以简化一些事情。也许是这样的:

CLLocationDegrees highestLatitude;
CLLocationDegrees lowestLatitude;
CLLocationDegrees highestLongitude;
CLLocationDegrees lowestLongitude;

// Calculate the above variable values...

// These calculations would have to be more complex if you need to handle the fact that longitude values wrap
CLLocationDegrees latitudeDelta = highestLatitude - lowestLatitude;
CLLocationDegrees longitudeDelta = highestLongitude - lowestLongitude;
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(lowestLatitude + (latitudeDelta / 2.0f), lowestLongitude + (longitudeDelta / 2.0f));
MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
MKCoordinateRegionMake(center, span);

答案 1 :(得分:1)

您也可以使用使用longitudeSpan和latitudeSpan的setRegion,这更简单,因为它可以避免距离计算。

Calc min和max纬度和经度。然后通过使用中点(minLat + latSpan / 2.0,minLon + lonSpan / 2.0)来计算latSpan和longSpan:max - min,然后计算calc中心