Swift: delete only one MKPolyline of several

There are two polylines on the map:

var polylineRoute : MKGeodesicPolyline!  
var polylineFlight : MKGeodesicPolyline! 

I assign a title to each of them and add them to the map, like this (in different methods):

let polyline = MKGeodesicPolyline(coordinates: &routeCoordinates, count: routeCoordinates.count)  
polyline.title = "route"  
self.mapView.addOverlay(polyline)  
self.polylineRoute = polyline 

and

let polyline = MKGeodesicPolyline(coordinates: &routeCoordinates, count: routeCoordinates.count)  
polyline.title = "flight"  
self.mapView.addOverlay(polyline)  
self.polylineFlight = polyline  

Now that a certain action is triggered, I would like to remove only the overhead flight and leave the route superimposed unchanged.

This does not work at all:

func removeFlightPath()  
    {  
        self.mapView.removeOverlay(self.polylineFlight)  
        self.polylineFlight = nil  
    }  

The following works, but removes both polylines:

func removeFlightPath()  
{  
        var overlays = mapView.overlays  
        mapView.removeOverlays(overlays)  
}  

Is there a way to remove only one polyline? I was looking for a forum, and there is only one answer that says you can use the title. However, he does not indicate how this can be done.

Thanks a lot!

EDIT:

This solves the problem:

func removeFlightPath()
    {
        if self.polylineFlight != nil
        {
            // Overlays that must be removed from the map
            var overlaysToRemove = [MKOverlay]()

            // All overlays on the map
            let overlays = self.mapView.overlays

            for overlay in overlays
            {
                if overlay.title! == "flight"
                {
                    overlaysToRemove.append(overlay)
                }
            }

            self.mapView.removeOverlays(overlaysToRemove)
        }
    }
+4
source share
2 answers

, . , . , MKGeodesicPolyline . . , .

self.polylineFlight = MKGeodesicPolyline(coordinates: &routeCoordinates, count: routeCoordinates.count)  
self.polylineFlight.title = "flight"  
+2

polylineFlight . routeCoordinates, , polylineRoute. .

?

/ ? , " "?

+1

Source: https://habr.com/ru/post/1629979/


All Articles