在mapView中调整多边形

问题描述 投票:0回答:1

我能够在mapView上绘制多边形,但是我需要找到该多边形并对其进行手动缩放。有没有办法像在中心调整多边形那样自动执行此过程?我浏览了互联网,并阅读了一些相关的文章,其中大多数是基于折线和点的。任何形式的帮助将不胜感激,因为我正在寻找解决方案一段时间。预先感谢。

使用以下方法在mapView上绘制多边形:-

 func drawFence(coordinates: UnsafePointer<CLLocationCoordinate2D>, count: Int) {
        let makePoly = MKPolygon(coordinates: coordinates, count: count)
        mapview.addOverlay(makePoly)
    }

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
            guard let polyOverlay = overlay as? MKPolygon else { return MKOverlayRenderer() }
            let polyRender = MKPolygonRenderer(polygon: polyOverlay)
            polyRender.fillColor = #colorLiteral(red: 0.9764705882, green: 0.09803921569, blue: 0.2588235294, alpha: 0.6)
            polyRender.strokeColor = #colorLiteral(red: 0.9764705882, green: 0.09803921569, blue: 0.2588235294, alpha: 1)
            polyRender.lineWidth = 2
            return polyRender
        }
ios swift mapkit core-location mkpolygon
1个回答
0
投票

如果您要放大特定的叠加层,则可以:

let insets = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50)

func zoom(for overlay: MKOverlay) {
    mapView.setVisibleMapRect(overlay.boundingMapRect, edgePadding: insets, animated: true)
}

enter image description here


如果要缩放地图以显示所有叠加层,可以执行以下操作:

func zoomForAllOverlays() {
    guard let initial = mapView.overlays.first?.boundingMapRect else { return }

    let mapRect = mapView.overlays
        .dropFirst()
        .reduce(initial) { $0.union($1.boundingMapRect) }

    mapView.setVisibleMapRect(mapRect, edgePadding: insets, animated: true)
}

例如,添加了两个叠加层(在NYC和Stamford之上),我调用了该例程,结果是:

mapview w two overlays


顺便说一下,我知道问题是关于Polygon叠加层,但是该技术不管叠加层类型如何都有效。创建Circle叠加图以进行演示只是更简单。

© www.soinside.com 2019 - 2024. All rights reserved.