Remove only one polyline of several

Hello.


I have two polylines on the map:

var polylineRoute : MKGeodesicPolyline!
var polylineFlight : MKGeodesicPolyline!


I assign each of them a title 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, when a specific action is triggered, I would like to remove the flight overlay and leave the route overlay intact.


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 working way to remove only one polyline?


Thanks a lot!

OK. After a few attempts, came up with the following that seems to work:


func removeFlightPath()
    {
        if self.polylineFlight != nil
        {
            var overlaysToRemove = [MKOverlay]()
            let overlays = self.mapView.overlays
          
            for overlay in overlays
            {
                if overlay.title! == "flight"
                {
                    overlaysToRemove.append(overlay)
                }
            }
          
            mapView.removeOverlays(overlaysToRemove)
        }
    }

Спасибо за ваше решение, оно мне помогло

Remove only one polyline of several
 
 
Q