我一直在寻找一种解决方案,以放大MapView以适应Swift中MKPolyline的边界。我已经能够找到Objective-C的示例代码here on SO,但是我对Objective-C或如何将其转换为Swift一点都不熟悉。

有人会在Swift中举个例子吗?谢谢。

-(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];
}

最佳答案

此代码设置区域以显示整个折线以及每侧的12.5%填充。

        var regionRect = myPolyline.boundingMapRect


        var wPadding = regionRect.size.width * 0.25
        var hPadding = regionRect.size.height * 0.25

        //Add padding to the region
        regionRect.size.width += wPadding
        regionRect.size.height += hPadding

        //Center the region on the line
        regionRect.origin.x -= wPadding / 2
        regionRect.origin.y -= hPadding / 2

        myMapView.setRegion(MKCoordinateRegionForMapRect(regionRect), animated: true)

如果您不希望填充,请执行此操作
        var regionRect = myPolyline.boundingMapRect
        myMapView.setRegion(MKCoordinateRegionForMapRect(regionRect), animated: true)

10-08 06:05