我有一个数组,allCollections,保持CLLocations用户已经通过我的iOS应用记录的编程方式创建的阵列。 各子阵列中allCollections保存所有位置点在所取的组合。
我绘制MKPolylines关闭在allCollections表示上的MKMapView这些旅行阵列中的CLLocations的。 我的问题是这样的:随着折线添加到地图中,我将如何去编程缩放和居中地图以显示所有的人?
我有一个数组,allCollections,保持CLLocations用户已经通过我的iOS应用记录的编程方式创建的阵列。 各子阵列中allCollections保存所有位置点在所取的组合。
我绘制MKPolylines关闭在allCollections表示上的MKMapView这些旅行阵列中的CLLocations的。 我的问题是这样的:随着折线添加到地图中,我将如何去编程缩放和居中地图以显示所有的人?
-(void)zoomToPolyLine: (MKMapView*)map polyline: (MKPolyline*)polyline animated: (BOOL)animated
{
[map setVisibleMapRect:[polyline boundingMapRect] edgePadding:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0) animated:animated];
}
-(void)zoomToPolyLine: (MKMapView*)map polyLine: (MKPolyline*)polyLine
animated (BOOL)animated
{
MKPolygon* polygon =
[MKPolygon polygonWithPoints:polyLine.points count:polyLine.pointCount];
[map setRegion:MKCoordinateRegionForMapRect([polygon boundingMapRect])
animated:animated];
}
你可以通过所有循环CLLocations
记录max/min
坐标,并用它来设置视图矩形像他们这样做在这个问题上的iOS的MKMapView缩放显示所有标记 。
或者你可以通过每叠加层,并得到他们的boundingMapRect
然后使用MKMapRectUnion
( http://developer.apple.com/library/ios/documentation/MapKit/Reference/MapKitFunctionsReference/Reference/reference.html#//apple_ref/c / FUNC / MKMapRectUnion )将它们合并所有,直到你有一个MKMapRect
涵盖所有这些,用它来设置视图。
[mapView setVisibleMapRect:zoomRect animated:YES]
这个问题给出了一个简单的循环中maprects工会结合,我建议: MKMapRect放大太多
斯威夫特:
if let first = mapView.overlays.first {
let rect = mapView.overlays.reduce(first.boundingMapRect, combine: {MKMapRectUnion($0, $1.boundingMapRect)})
mapView.setVisibleMapRect(rect, edgePadding: UIEdgeInsets(top: 50.0, left: 50.0, bottom: 50.0, right: 50.0), animated: true)
}
这将放大/平移,以适合所有叠加有一点缓冲
雨燕3.0版本的garafajon出色的代码
if let first = self.mapView.overlays.first {
let rect = self.mapView.overlays.reduce(first.boundingMapRect, {MKMapRectUnion($0, $1.boundingMapRect)})
self.mapView.setVisibleMapRect(rect, edgePadding: UIEdgeInsets(top: 50.0, left: 50.0, bottom: 50.0, right: 50.0), animated: true)
}
斯威夫特4 /稍加修改fundtimer的回答版本。
func setVisibleMapArea(polyline: MKPolyline, edgeInsets: UIEdgeInsets, animated: Bool = false) {
mapView.setVisibleMapRect(polyline.boundingMapRect, edgePadding: edgeInsets, animated: animated)
}
调用上面的使用路线的折线,留下的不是动画的默认,以及各地加入10个小边插图:
setVisibleMapArea(polyline: route.polyline, edgeInsets: UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0))
@Fundtimer指着正确的方式,唯一的事情就是填充需要调整按视觉需求,这样,它将支持所有其他的叠加和下面是所有叠加的通用的解决方案。
-(void)zoomIntoExistingMapObjectForAnnot:(CustomMapAnnotation *)annot
{
id overlay = annot.shape;//I have overlay property in custom annotation class.
[_mapView setVisibleMapRect:[overlay boundingMapRect] edgePadding:UIEdgeInsetsMake(150.0, 150.0, 150.0, 150.0) animated:YES];
}
我对这个问题的另一种解决方案
private func mapRegion() -> MKCoordinateRegion? {
let latitudes = self.coordinates.map { location -> Double in
return location.latitude
}
let longitudes = self.coordinates.map { location -> Double in
return location.longitude
}
let maxLat = latitudes.max()!
let minLat = latitudes.min()!
let maxLong = longitudes.max()!
let minLong = longitudes.min()!
let center = CLLocationCoordinate2D(latitude: (minLat + maxLat) / 2,
longitude: (minLong + maxLong) / 2)
let span = MKCoordinateSpan(latitudeDelta: (maxLat - minLat) * 1.3,
longitudeDelta: (maxLong - minLong) * 1.3)
return MKCoordinateRegion(center: center, span: span)
}
其中坐标是数组CLLocationCoordinate2D
希望这是有帮助的人