ios - 快速删除多段线

标签 ios swift mapkit

我正在 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/

相关文章:

ios - 如何观察 NSView 框架的变化?

swift - 如何在 Swift 中为广播直播服务实现 HLS

ios - 继续使用 didSelectRowAtIndexPath

ios - 快速导入 CoreGraphics.CGBase VS。导入 CoreGraphics 导致 LLDB 发生故障

ios - 如何在 GMSMapView 上设置自定义注释标记(点周围的动画环)

iphone - 如何自动对 UICollectionView 中的项目进行分组?

objective-c - 具有数百个数据点的 UIScrollView 最佳实践

objective-c - 在类之间传递信息

ios - 使用 CGPattern 填充 MKPolygon 时,多边形叠加相互干扰(使用 Quartz 2D)

iOS6 CLPlacemark 显示街道名称