在swift删除折线

时间:2018-05-16 13:45:20

标签: ios swift mapkit

我正在斯威夫特画航道。我使用当前位置到另一个位置并进行了平局。然后,我选择另一个位置并重绘它。但即使我在代码中编写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)
        }
    }
}

2 个答案:

答案 0 :(得分:0)

使用地图视图方法

self.mapview.removeOverlays(self.mapview.overlays)

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

答案 1 :(得分:0)

以下是从Google Maps iOS Swift中删除游历折线的方法:

var oldPolyLines = [GMSPolyline]() /* Global Array Variable of your Class */

将以下代码放在解析路线并从Direction API获取新折线的位置。

if self.oldPolyLines.count > 0 {
    for polyline in self.oldPolyLines {
        polyline.map = nil
    }
}
self.oldPolyLines.append(yourNewPolyline)
yourNewPolyLine.map = self.mapView