2016-11-05 27 views
9

Tôi đang sử dụng mapView.showUserLocation = true để vẽ vị trí của người dùng và hiển thị nó dưới dạng chú thích trên đối tượng MKMapView của tôi.Cách vẽ Chỉ đường giữa hai vị trí trong iOS Swift bằng mapView và CLLocationManager

Bây giờ, khi tôi đã có được vị trí của người dùng, tôi muốn vẽ một đường dẫn từ bộ hợp tác đó đến đích của tôi. Tôi đã cố gắng sử dụng MKDirections cho điều này

Dưới đây là làm thế nào tôi đã cố gắng để làm điều đó:

func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) { 
    mapView.showsUserLocation = true 
    print("test") 
    //Setting Up Source Location 
    let sourceLocation = self.mapView.userLocation.coordinate 
    let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil) 
    let sourceMapItem = MKMapItem(placemark: sourcePlacemark) 
    let sourceAnnotation = MKPointAnnotation() 
    sourceAnnotation.title = "You are Here" 

    if let location = sourcePlacemark.location { 
     sourceAnnotation.coordinate = location.coordinate 
    } 

    //Setting Up Destination Location 
    let destinationLocation = CLLocationCoordinate2D(latitude: 28.6621292, longitude: 77.30198310000003) 
    let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil) 
    let destinationMapItem = MKMapItem(placemark: destinationPlacemark) 
    let destinationAnnotation = MKPointAnnotation() 
    destinationAnnotation.title = "Conclave is Here" 

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


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


    //Plotting a course from Source to Destination 
    let directionRequest = MKDirectionsRequest() 
    directionRequest.source = sourceMapItem 
    directionRequest.destination = destinationMapItem 
    directionRequest.transportType = .Automobile 

    // Calculate the direction 
    let directions = MKDirections(request: directionRequest) 

    // 8. 
    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) 
    } 

} 

Tuy nhiên, khi tôi đã cố gắng gỡ lỗi nó, tôi nhận được lỗi sau: Error: Error Domain=MKErrorDomain Code=5 "Directions Not Available" UserInfo=0x1742f7600 {NSLocalizedFailureReason=A route could not be determined between these locations., MKErrorGEOError=-403, MKDirectionsErrorCode=1, NSLocalizedDescription=Directions Not Available}

tôi đã làm một số nghiên cứu và phát hiện ra rằng MKDirections không có sẵn trong Ấn Độ, vì vậy tôi đã tự hỏi làm thế nào để có thể nhận được xung quanh hiển thị Hướng dẫn điều hướng cho người dùng.

Cảm ơn sự giúp đỡ!

+0

Tùy chọn tốt nhất để sử dụng GOOGLEPLACEAPI để nhận chỉ đường và thông tin địa điểm. –

Trả lời

4
Error Domain=MKErrorDomain Code=5 "Directions Not Available" 

Bạn có nhiều khả năng nhận được lỗi này nếu địa điểm không thuộc về bất kỳ của các quốc gia trong danh sách này: http://www.apple.com/ios/feature-availability/#maps-directions

Trong khi trên iOS mô phỏng, bạn có thể dễ dàng tùy chỉnh vị trí hiện tại của mình. Hai cách để làm điều đó:

iOS Simulator - tab> 'Debug' -> Vị trí -> {Chọn}

Xcode -> tab 'Debug' -> Mô phỏng Location -> {Chọn}

Vì vậy, tôi đề nghị bạn sử dụng tính năng này. SDK GoogleMaps và vẽ tuyến đường.

https://gist.github.com/himanshu-benzatine/10670936c8f16ea1ae482bc6bb684adc

+0

Cảm ơn! Điều đó thực sự hữu ích. Chỉ cần tự hỏi, Nếu tôi đã chuyển mã đó từ C# sang Swift, mọi thay đổi sẽ phải được thực hiện ngoài cú pháp? –

+0

Có nếu bạn thay đổi mã từ C# sang cú pháp nhanh và cú pháp của nó sẽ thay đổi. –

+0

Được rồi, cảm ơn! –

3

Tốt hơn để sử dụng google api để vẽ polyline từ nguồn & đích. Bạn có thể sử dụng api

https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=%@&destinations=%@&key=

Sau đó, bạn lấy dữ liệu json của lat dài (s) từ nơi con đường có thể được rút ra. Giải mã các dữ liệu json và vẽ polyline trong MKMapkit

+0

Bản đồ Google và bản đồ Apple không có cùng dữ liệu cơ bản. Nếu bạn thực hiện việc này, tuyến đường của bạn có thể vẽ trên các con đường không tồn tại trong bản đồ Apple hoặc nằm cách đường được vẽ trong bản đồ Apple vài mét. –

+0

Nếu bạn định làm điều này, bạn nên sử dụng Google Maps SDK thay vì sử dụng Apple Maps. –

Các vấn đề liên quan