2016-12-28 8 views
0

둘 이상의 다른 핀 위치에 대해 하나의 위치 대상을 표시하려하지만 문제는 핀의 위치를 ​​변경할 때 경로가 재설정되지 않는다는 것입니다. 예를 들어 앱을 시작하고 위치를 설정하면 경로가 표시되지만지도에서 위치를 추가하거나 변경하면 이전 경로는 디퍼가되지 않습니다. 길 http://i.hizliresim.com/pX6Ppn.png하나의 대상에 대해 두 개 이상의 서로 다른 핀의 폴리 라인을 표시하려면 어떻게합니까?

의 방향의

먼저보기 http://i.hizliresim.com/kGOvy9.png

두 번째보기

// 추가 또는 목적지 핀의 위치를 ​​변경

@IBAction func longpress(_ sender: UILongPressGestureRecognizer) { 

    let location = sender.location(in: self.map) 
    let locCoordinate = self.map.convert(location, toCoordinateFrom: self.map) 

    annotation.coordinate = locCoordinate 
    annotation.title = "Store" 
    annotation.subtitle = "Location of Store" 

    self.map.addAnnotation(annotation) 

    annotationLat = annotation.coordinate.latitude 
    annotationLong = annotation.coordinate.longitude 

    for item in arr { 
     let dic = item as? [String: Any] 
     pinDestinations(lat: dic?["lat"] as! CLLocationDegrees, long: dic?["long"] as! CLLocationDegrees) 
    } 

    let directions = MKDirections(request: request) 

    directions.calculate { [unowned self] response, error in 
     guard let unwrappedResponse = response else { return } 

     for route in unwrappedResponse.routes { 
      // I tried here remove previous polyline while i'm adding new one 
      self.map.removeOverlays([route.polyline]) 
     } 
    } 


func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 
    let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline) 

    renderer.strokeColor = UIColor.blue 
    renderer.lineWidth = 1 
    return renderer 
} } 

답변

0

당신은 이전에 추가 한 폴리 라인을 추적하는 속성을 정의 할 수 있습니다 :

var polylines: [MKPolyline]? 

그런 다음 지시를 업데이트 할 때, 다음지도에서 이전 폴리 라인 (있는 경우)를 제거 업데이트 폴리 라인을 새 값으로하고, 지도에 추가하십시오.

func updateDirections() { 
    let request = ... 

    let directions = MKDirections(request: request) 
    directions.calculate { response, error in 
     guard let response = response, error == nil else { 
      print("\(error)") 
      return 
     } 

     if let polylines = self.polylines { 
      self.mapView.removeOverlays(polylines) 
     } 

     self.polylines = response.routes.map { $0.polyline } 
     self.mapView.addOverlays(self.polylines!) 
    } 
} 
+0

감사합니다. 두 개 이상의 핀으로이 작업을 시도하면 제대로 작동하지 않습니다. 핀 중 하나에 대해 하나의 폴리 라인을 보여줍니다. – GreatCornholio

+0

그래, 나는 단지 2 점 사이의 1 세트의 방향을 위해 그것을 썼다. 그러나 당신이 가지고있는 많은 사람들과 관계없이 아이디어는 동일하다. 방향을 다시 계산하고 폴리 라인을 새로운 폴리선으로 교체하기 전에지도에서 쉽게 제거 할 수있는 일부 폴리선을 일부 속성에 유지하십시오. 나는 당신의 모델을 잘 알고 있는지 잘 모르겠다. (예를 들어'MKPolyline'의 단순한 배열보다는 상상할 수있다. 배열의 배열이나 배열의 사전을 가질 수도있다. 당신은 제거하고 올바른 배열을 대체),하지만 잘하면 당신은 기본적인 생각을 얻을. – Rob