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 đỡ!
Tùy chọn tốt nhất để sử dụng GOOGLEPLACEAPI để nhận chỉ đường và thông tin địa điểm. –