I'm trying to show one location destination for two or more different pins locations, but problem is that when i change the location of pin the routes doesn't reset. For example, when i started app and set a location, it shows a route, but when i add or change location on map the previous route doesn't disapper.
First View of Directions http://i.hizliresim.com/kGOvy9.png
Second View of Directions http://i.hizliresim.com/pX6Ppn.png
// Adding or changing the location of destination pin
@IBAction func longpress(_ sender: UILongPressGestureRecognizer) {
let location = sender.location(in: self.map)
let locCoordinate = self.map.convert(location, toCoordinateFrom: self.map)
annotation.coordinate = locCoordinate
annotation.title = "Store"
annotation.subtitle = "Location of Store"
self.map.addAnnotation(annotation)
annotationLat = annotation.coordinate.latitude
annotationLong = annotation.coordinate.longitude
for item in arr {
let dic = item as? [String: Any]
pinDestinations(lat: dic?["lat"] as! CLLocationDegrees, long: dic?["long"] as! CLLocationDegrees)
}
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
for route in unwrappedResponse.routes {
// I tried here remove previous polyline while i'm adding new one
self.map.removeOverlays([route.polyline])
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.blue
renderer.lineWidth = 1
return renderer
} }
You can define property to keep track of previously added polylines:
Then, when you update directions, remove the old polylines (if any) from the map, update the polylines with the new values, and then add them to the map: