快速删除多段线

Kur*_*ken 1 mapkit ios 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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dev*_*der 5

使用地图查看方法

self.mapview.removeOverlays(self.mapview.overlays)
Run Code Online (Sandbox Code Playgroud)

这将删除您添加的所有叠加层,因此您必须再次执行整个过程,就像重新加载地图视图一样