我正在 Swift 绘制导航路。我正在使用当前位置到另一个位置并进行抽签。之后,我选择另一个位置并重新绘制它。但即使我在代码中编写了 mapView.remove(rotapoly)
,它也不会删除它。我该如何解决这个问题?
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
cizim = 1;
let capital = view.annotation as! Station
guard let locValue: CLLocationCoordinate2D = locationManager.location?.coordinate else { return }
let neresi = CLLocationCoordinate2D(latitude: capital.latitude, longitude: capital.longitude)
let nerdeyim = CLLocationCoordinate2D(latitude: locValue.latitude, longitude: locValue.longitude)
let request = MKDirectionsRequest()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: nerdeyim, addressDictionary: nil))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: neresi, addressDictionary: nil))
request.requestsAlternateRoutes = true
request.transportType = .walking
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
if (unwrappedResponse.routes.count > 0) {
self.showRoute(response!)
}
}
}
func showRoute(_ response: MKDirectionsResponse) {
mapView.remove(rotapoly)
for route in response.routes {
rotapoly = route.polyline
mapView.add(rotapoly, level: MKOverlayLevel.aboveRoads)
for step in route.steps {
print(step.instructions)
}
}
}
最佳答案
使用 map 查看方式
self.mapview.removeOverlays(self.mapview.overlays)
这将删除您添加的所有叠加层,因此您必须再次执行整个过程,就像重新加载 map View 一样
关于ios - 快速删除多段线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50372633/