我正在使用 MKMapView
并在 map 上绘制了几个点。我已经使用 MKCooperativeRegion
和 MKCooperativeSpan
来启用围绕其中一个点的缩放等 - 但这不是我想要的......
我正在尝试使用类似于 Javascript 缩放到边界功能的东西。所以我的所有观点都应该对用户可见。 (英国各地大约有 10 个点)我想将它们全部展示出来,或者如果其中大部分位于伦敦地区,请放大到那里。
有没有办法以编程方式解决这个问题?
最佳答案
当然。您想要找到注释中最大和最小的纬度和经度值(可以通过迭代 map.annotations 来实现),然后将 map 设置为显示所有这些值。
// pad our map by 10% around the farthest annotations
#define MAP_PADDING 1.1
// we'll make sure that our minimum vertical span is about a kilometer
// there are ~111km to a degree of latitude. regionThatFits will take care of
// longitude, which is more complicated, anyway.
#define MINIMUM_VISIBLE_LATITUDE 0.01
MKCoordinateRegion region;
region.center.latitude = (minLatitude + maxLatitude) / 2;
region.center.longitude = (minLongitude + maxLongitude) / 2;
region.span.latitudeDelta = (maxLatitude - minLatitude) * MAP_PADDING;
region.span.latitudeDelta = (region.span.latitudeDelta < MINIMUM_VISIBLE_LATITUDE)
? MINIMUM_VISIBLE_LATITUDE
: region.span.latitudeDelta;
region.span.longitudeDelta = (maxLongitude - minLongitude) * MAP_PADDING;
MKCoordinateRegion scaledRegion = [map regionThatFits:region];
[map setRegion:scaledRegion animated:YES];
关于iphone - iOS MKMapView 缩放以显示所有标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3434020/