2016-04-07 2 views
0

にドロールートを削除するには:どのように私はこの機能を地図上にドロールートを実装しているマップ

func drawRouteOnMap(sourceLocation sourceLocation: CLLocation, destinationLocation: CLLocation) { 
     drawMapActivated = true 
     let sourcePlacemark = MKPlacemark(coordinate: sourceLocation.coordinate, addressDictionary: nil) 
     let destinationPlacemark = MKPlacemark(coordinate: destinationLocation.coordinate, addressDictionary: nil) 

     let sourceMapItem = MKMapItem(placemark: sourcePlacemark) 
     let destinationMapItem = MKMapItem(placemark: destinationPlacemark) 

     let destinationAnnotation = MKPointAnnotation() 
     destinationAnnotation.title = "ATM" 

     if let location = destinationPlacemark.location { 
      destinationAnnotation.coordinate = location.coordinate 
     } 

     self.mapView.showAnnotations([destinationAnnotation], animated: true) 

     let directionRequest = MKDirectionsRequest() 
     directionRequest.source = sourceMapItem 
     directionRequest.destination = destinationMapItem 
     directionRequest.transportType = .Automobile 

     let directions = MKDirections(request: directionRequest) 

     directions.calculateDirectionsWithCompletionHandler { 
      (response, error) -> Void in 

      guard let response = response else { 
       if let error = error { 
        print("Error: \(error)") 
       } 

       return 
      } 

      let route = response.routes[0] 
      self.mapView.addOverlay((route.polyline), level: MKOverlayLevel.AboveRoads) 

      let rect = route.polyline.boundingMapRect 
      self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true) 
     } 

    } 

しかし、ここでの問題は、私は別のルートを描きたいとき、それは以前のルートを上書きですので、非常に醜く見える。私は古いものを削除し、新しいものを描きたい、これを行う方法。

ご協力いただきありがとうございます。

答えて

2

私はこのように試してみました:

mapView.removeOverlays(mapView.overlays) 

そして、それが動作するようになりました。

関連する問題