我正在开发一个 iOS 应用程序,并且我正在尝试实现这一点,给定多个位置(纬度和经度),我想自动缩放我的 map 并重新定位我的 map View 以包含所有位置。
我想出的解决方案是,给定任意数量的位置,我可以使用 for 循环来查找最高和最低纬度和经度。并且中心点应该是纬度/经度的最高/最低之间的中心位置。并且使用 MKCoordinateRegionMakeWithDistance,结合核心位置框架(distanceFromLocation),我可以计算最远位置和中心位置之间的距离,并在 map 上显示该区域。
我的问题是,有没有更好的解决方案或框架可以处理这个问题?有没有我能找到的例子可以做到这一点?
最佳答案
如果您知道最高和最低纬度和经度值,您至少可以通过使用 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);
关于ios - 在 iOS map 上给定多个位置自动缩放和定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18177923/