本文介绍了如何自动缩放mapView以显示覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am able to draw the polygon on mapView however I need to locate the polygon and zoom it manually. Is there a way to do this process automatically like adjust the polygon in centre? I have browsed the internet and read few related articles, most of them are based on polylines and points. Any kind of help will be appreciated, as I am finding for the solution for a while. Thanks in advance.

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

Using the following methods to draw the polygon on 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
}

推荐答案

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

If you’re trying to zoom into a particular overlay, you can:

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

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

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

If you want to zoom the map to show all the overlays, you can do:

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之上),我调用了该例程,结果是:

For example, having added two overlays (over NYC and Stamford), I called that routine, and it resulted in:

顺便说一句,我知道问题是关于Polygon叠加层的,但是该技术不管叠加层的类型如何都有效.出于演示目的,创建Circle叠加层更简单.

By the way, I know the question was about Polygon overlays, but the technique works regardless of the overlay type. It was just simpler to create Circle overlays for demonstration purposes.

这篇关于如何自动缩放mapView以显示覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:06