iOS - 显示所有注释

时间:2018-03-17 23:33:56

标签: ios mkmapview mkannotation

我目前正在使用MapKit开发一项功能。我遇到了一个小问题。目前我在地图上有我的所有注释,并在您移动地图时显示。

但是,我试图让我的所有引脚同时显示。我知道这是不可能的,因为下面的链接和缩放的限制 - 我的引脚可以遍布整个世界。

现在我知道这超出了我们的控制范围,但在这种情况下,地图有时会在注释之间居中。例如:

Antartica的针脚 钉在俄罗斯上空的北极海。

它将把地图的中心大致集中在印度 - 两个引脚之间的1/2点。

如果没有可见的,有没有办法强制至少一个或多个引脚可见?

All Annotation is not visible to user in MKMapView

1 个答案:

答案 0 :(得分:0)

您可以尝试缩小并显示所有注释

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)

}