如何显示一个目的地的两个或多个不同引脚的多段线?
How can I show polylines of two or more different pins for one destination?
我正在尝试为两个或多个不同的引脚位置显示一个位置目的地,但问题是当我更改引脚位置时,路线不会重置。例如,当我启动应用程序并设置一个位置时,它会显示一条路线,但是当我在地图上添加或更改位置时,以前的路线不会消失。
First View of Directions
http://i.hizliresim.com/kGOvy9.png
Second View of Directions
http://i.hizliresim.com/pX6Ppn.png
// 添加或更改目标引脚的位置
@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
} }
您可以定义 属性 以跟踪之前添加的多段线:
var polylines: [MKPolyline]?
然后,当您更新方向时,从地图中删除旧折线(如果有),使用新值更新折线,然后将它们添加到地图:
func updateDirections() {
let request = ...
let directions = MKDirections(request: request)
directions.calculate { response, error in
guard let response = response, error == nil else {
print("\(error)")
return
}
if let polylines = self.polylines {
self.mapView.removeOverlays(polylines)
}
self.polylines = response.routes.map { [=11=].polyline }
self.mapView.addOverlays(self.polylines!)
}
}
我正在尝试为两个或多个不同的引脚位置显示一个位置目的地,但问题是当我更改引脚位置时,路线不会重置。例如,当我启动应用程序并设置一个位置时,它会显示一条路线,但是当我在地图上添加或更改位置时,以前的路线不会消失。
First View of Directions http://i.hizliresim.com/kGOvy9.png
Second View of Directions http://i.hizliresim.com/pX6Ppn.png
// 添加或更改目标引脚的位置
@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
} }
您可以定义 属性 以跟踪之前添加的多段线:
var polylines: [MKPolyline]?
然后,当您更新方向时,从地图中删除旧折线(如果有),使用新值更新折线,然后将它们添加到地图:
func updateDirections() {
let request = ...
let directions = MKDirections(request: request)
directions.calculate { response, error in
guard let response = response, error == nil else {
print("\(error)")
return
}
if let polylines = self.polylines {
self.mapView.removeOverlays(polylines)
}
self.polylines = response.routes.map { [=11=].polyline }
self.mapView.addOverlays(self.polylines!)
}
}