![](/img/trans.png)
[英]iOS - MKMapView showAnnotations:animated: with padding?
[英]MKMapView showAnnotations behavior
我的MKMapView不能正确地适合我添加到其中的仅有两个注释。
我想不出为什么会发生什么。 我在另一个视图控制器中有另一个地图,并且showAnnotations(_:animated)正常工作。 通过比较它们各自的代码,我找不到任何可能导致该行为的东西。
代码基本上是这样的:
override func viewDidLoad() {
super.viewDidLoad()
annotationOrigin = aCoordinate
annotationDest = bCoordinate
mapView.addAnnotations([annotationOrigin, annotationDest], animated: true)
}
func mapViewDidFinishRenderingMap(_ mapView: MKMapView, fullyRendered: Bool) {
mapView.showAnnotations([annotationOrigin, annotationDest], animated: true)
}
你可以试试这个
func zoomToFitMapAnnotations(aMapView:MKMapView)
{
if(aMapView.annotations.count == 0)
{
return
}
var topLeftCoord = CLLocationCoordinate2D.init(latitude: -90, longitude: 180)
var bottomRightCoord = CLLocationCoordinate2D.init(latitude: 90, longitude: -180)
for i in 0..<myMapView.annotations.count
{
let annotation = myMapView.annotations[i]
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
let resd = CLLocationCoordinate2D.init(latitude: topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5, longitude: topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5)
let span = MKCoordinateSpan.init(latitudeDelta: fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.3, longitudeDelta: fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.3)
var region = MKCoordinateRegion.init(center: resd, span: span);
region = aMapView.regionThatFits(region)
aMapView.setRegion(region, animated: true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.