How to prevent overlays on the map from disappearing when zoom scale changes in swift?

随声附和 提交于 2019-12-13 04:18:55

问题


I am trying to render an overlay (polygon) on the map using swift MapKit. I can render successfully but when I zoom out too much then zoom in again, all the rendered polygons get disappear. How can I prevent that from happening and force the rendered polygons to stay on the map at all zoom scales? I am using MapKit and MKPolygonRenderer.

I add the polygons to the map using the following method in my viewController:

// function to add polygon overlay to the map
    private func addPolygonsToMap() {
        guard let polygons = arrayOfpolygons else {
            return
        }
        for polygon in polygons {
            mapView.addOverlay(polygon)
        }
    }

My view controller conforms to MKMapViewDelegate and here is the delegate method for it.

// method for overlay on map
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let renderer = MKPolygonRenderer(overlay: overlay)
        renderer.fillColor = UIColor.purple.withAlphaComponent(0.4)
        renderer.strokeColor = .black
        renderer.lineWidth = 2

        return renderer
    }

here is a simple demonstration with some points to draw a test polygon. https://developer.apple.com/documentation/mapkitjs/mapkit/polygonoverlay I did not find any swift solution for this problem.

I am running on simulator with iOS 13.

来源:https://stackoverflow.com/questions/58319549/how-to-prevent-overlays-on-the-map-from-disappearing-when-zoom-scale-changes-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!