我正在与MKMapView
并在地图上标出了几个点。我已经用过MKCoordinateRegion
and MKCoordinateSpan
围绕其中一个点启用缩放等 - 但这不是我想要的......
我正在尝试使用类似于 Javascript 缩放到边界功能的东西。所以我的所有观点都应该对用户可见。 (英国各地大约有 10 个点)我想将它们全部展示出来,或者如果其中大部分位于伦敦地区,请放大到那里。
有没有办法以编程方式解决这个问题?
当然。您想要找到注释中最大和最小的纬度和经度值(可以通过迭代 map.annotations 来完成),然后设置地图以显示所有这些值。
// 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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)