2016-07-30 1 views
1

CLLocation 배열에 대해 mapview에서 경로를 만들려면 어떻게해야합니까? 폴리 라인을 만들고 걷는 방법으로 경로를 만들고 싶습니다. 내 나쁜 영어에 대한CLLocation (Swift) 배열에 대한 mapView의 경로 그리기

@IBAction func addPolyline(sender: AnyObject) { 
    let locations = [CLLocation(latitude: annotazioni.objectAtIndex(0).coordinate.latitude, longitude:annotazioni.objectAtIndex(0).coordinate.longitude), 
        CLLocation(latitude: annotazioni.objectAtIndex(1).coordinate.latitude, longitude:annotazioni.objectAtIndex(1).coordinate.longitude), 
        CLLocation(latitude: annotazioni.objectAtIndex(2).coordinate.latitude, longitude:annotazioni.objectAtIndex(2).coordinate.longitude), 
        CLLocation(latitude: annotazioni.objectAtIndex(3).coordinate.latitude, longitude:annotazioni.objectAtIndex(3).coordinate.longitude), 
        CLLocation(latitude: annotazioni.objectAtIndex(4).coordinate.latitude, longitude:annotazioni.objectAtIndex(4).coordinate.longitude), 
        CLLocation(latitude: annotazioni.objectAtIndex(5).coordinate.latitude, longitude:annotazioni.objectAtIndex(5).coordinate.longitude), 
        CLLocation(latitude: annotazioni.objectAtIndex(6).coordinate.latitude, longitude:annotazioni.objectAtIndex(6).coordinate.longitude)] 
    addPolyLineToMap(locations) 
} 
func addPolyLineToMap(locations: [CLLocation!]){ 
    var coordinates = locations.map({ (location: CLLocation!) -> CLLocationCoordinate2D in 
     return location.coordinate 
    }) 
    let geodesic = MKGeodesicPolyline(coordinates: &coordinates, count: locations.count) 
    mapView.addOverlay(geodesic) 

} 
func mapView(mapView: MKMapView!, viewForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { 

    if (overlay is MKPolyline) { 
     let pr = MKPolylineRenderer(overlay: overlay); 
     pr.strokeColor = UIColor.blueColor().colorWithAlphaComponent(0.5); 
     pr.lineWidth = 5; 
     return pr; 
    } 

    return nil 
} 

죄송합니다 : 이 내 코드입니다 당신은 MKDirectRequestMKDirections를 사용하여 위치의 각 연속 쌍 사이 방향을 요청할 수 있습니다

+0

영어는 정상입니다. SO는 사람들이 모든 종류의 언어를 구사하는 데 도움을주는 전세계적인 웹 사이트입니다. 질문으로 돌아 가면 ... 점 A에서 점 B에서 점 C 사이의 보행 선을 그리고 마지막으로 점 D에 도착하기를 원하십니까? –

+0

예 :) thanks :) –

답변

3

D, 폴리 라인으로 경로를 가져온 다음 그립니다 귀하의지도에.

MKDirection.routes은 iOS 8 이상에서 사용할 수 있습니다. 다음은 뉴욕시 다운타운 주변을 걷는 예입니다. 파란색 핀에서 모든 녹색 핀을 통과하여 빨간색 핀에서 멈 춥니 다.

class MyPointAnnotation : MKPointAnnotation { 
    var isStartingPoint = false 
    var isEndingPoint = false 
} 

@IBAction func addPolyline(sender : AnyObject) { 
    let locations = [ 
     CLLocation(latitude: 40.759011, longitude: -73.984472), // Times Square, Manhattan, NY 10036, United States 
     CLLocation(latitude: 40.760920, longitude: -73.988665), // Palace Theater, 1564 7th Avenue & W 47th Street, New York, NY 10036, United States 
     CLLocation(latitude: 40.761417, longitude: -73.977120), // The Museum of Modern Art, 11 West 53rd Street, New York, NY 10019, United States 
     CLLocation(latitude: 40.756520, longitude: -73.973406), // Waldorf Astoria New York, 301 Park Avenue, New York, NY 10022, United States 
     CLLocation(latitude: 40.748441, longitude: -73.985664), // Empire State Building, 350 5th Avenue, New York, NY 10118, USA 
     CLLocation(latitude: 40.756359, longitude: -73.988873) // Madame Tussauds New York, 234 West 42nd Street, New York, NY 10036, United States 
    ] 

    self.mapView.region = MKCoordinateRegion(center: locations[0].coordinate, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)) 

    // Pins for the locations you want to visit 
    for loc in locations.enumerate() { 
     let point = MyPointAnnotation() 
     point.coordinate = loc.element.coordinate 
     point.title = "Point \(loc.index)" 

     if loc.index == 0 { 
      point.subtitle = "Starting point" 
      point.isStartingPoint = true 
     } else if loc.index == locations.count - 1 { 
      point.subtitle = "Ending point" 
      point.isEndingPoint = true 
     } 

     self.mapView.addAnnotation(point) 
    } 

    // Request directions from one point to the next 
    dispatch_apply(locations.count - 1, dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)) { i in 
     let sourcePlacemark  = MKPlacemark(coordinate: locations[i].coordinate, addressDictionary: nil) 
     let destinationPlacemark = MKPlacemark(coordinate: locations[i+1].coordinate, addressDictionary: nil) 

     let request = MKDirectionsRequest() 
     request.source = MKMapItem(placemark: sourcePlacemark) 
     request.destination = MKMapItem(placemark: destinationPlacemark) 
     request.transportType = .Walking 

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

      // Get the walking route as a polyline 
      let polyline = response!.routes.first!.polyline 
      self.mapView.addOverlay(polyline) 
     } 
    } 
} 

// MARK: - 
// MARK: Map View Delegate 
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    if let point = annotation as? MyPointAnnotation { 
     let view = MKPinAnnotationView(annotation: point, reuseIdentifier: "pointAnnotationView") 
     view.canShowCallout = true 

     // You walk from the blue pin through all the green pins and stop at the red pin 
     if point.isStartingPoint { 
      view.pinTintColor = UIColor.blueColor() 
     } else if point.isEndingPoint { 
      view.pinTintColor = UIColor.redColor() 
     } else { 
      view.pinTintColor = UIColor.greenColor() 
     } 

     return view 
    } 

    return nil 
} 

func mapView(mapView: MKMapView!, viewForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { 
    if (overlay is MKPolyline) { 
     let pr = MKPolylineRenderer(overlay: overlay) 
     pr.strokeColor = UIColor.blueColor().colorWithAlphaComponent(0.5) 
     pr.lineWidth = 5 
     return pr 
    } 

    return nil 
} 
+0

감사 코드 : 다른 :) 당신은 매우 귀중했습니다 :) –

+0

답변 마크가 대단히 감사합니다. –