Remove an overlay in mapKit Swift

2019-06-11 08:00发布

问题:

:D

I'm trying to delete an overlay that is in a mapView

i try this :

myLocation is the mapView

        let overlays = myLocation.overlays
        myLocation.removeOverlays(overlays)

and this :

        var polyline : MKPolyline = MKPolyline()    
        myLocation.removeOverlay(polyline)

but both don't work for me :C

anyone can help me pls?

回答1:

Try mapView.removeOverlays(mapView.overlays). You can also tag specific overlays and loop through them to remove specific ones.



回答2:

//add overlay:

let polyline: MKPolyline = MKPolyline() polyline.tag = 100 //any number that you will use to identify this overlay mapView.addOverlay(polyline)

//delete overlay:

for overlay in mapView.overlays { if (overlay.tag == 100) { mapView.removeOverlay(overlay) } }