MKPolyline broken when using type satelliteFlyover?

只愿长相守 提交于 2020-04-12 06:33:07

问题


I would like to draw some Geodesic Polyline(great circle line) between points on Earth using Satellite Flyover map type.

// in viewDidLoad()

mapView.mapType = .satelliteFlyover
let geodesic = MKGeodesicPolyline(coordinates: [route.airport1, route.airport2], count: 2)
self.mapView.addOverlay(geodesic)

// rendering method
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        guard let polyline = overlay as? MKPolyline else {
            print("else??")
            return MKOverlayRenderer(overlay: overlay)
        }
        let renderer = MKPolylineRenderer.init(polyline: polyline)
        renderer.lineWidth = 1
        renderer.strokeColor = .systemTeal
        return renderer
    }

I am able to create the GeodesicPolyline partially, but the lines are not complete.

If I'm in iOS system dark mode, the line shows under the "night area".

While the line shows under the "day area" if dark mode is disabled.

I also tried using

self.mapView.addOverlay(geodesic, level: .aboveLabels)
self.mapView.addOverlay(geodesic, level: .aboveRoads)

but no help as well. Any map type other than SatelliteFlyover or HybridFlyover is working well. I found a similar topic but no solution yet.


回答1:


I had a similar problem, and I found that the MKMultiPolylineRenderer in iOS 13 is much better for handling polylines on a flyover MapType. You need to do prepare all of your polylines in an array so you have ALL polylines available:

var multiArray = [MKPolyline]()
// CREATE ALL OF YOUR POLYLINES AND APPEND THEM TO multiArray
view.addOverlay(MKMultiPolyline(multiArray))

Then you need to create a section of code to handle MKMultiPolylines in func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer. I literally copied my MKPolyline section and just changed any reference from MKPolyline to MKMultiPolyline.

IDK what to do if you can't change your target to iOS 13. Both flyover map types exhibit a similar situation before iOS 13. I should note that I don't use dark mode in my app, so I'm not sure if you'll see the same results I did. But this code fixed all of my broken/missing polyline problems.



来源:https://stackoverflow.com/questions/60251655/mkpolyline-broken-when-using-type-satelliteflyover

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