Я использую mapView.showUserLocation = true
для определения местоположения пользователя и отображения его в виде аннотации на моем объекте MKMapView
.Как построить график между двумя местоположениями в iOS Swift с помощью mapView и CLLocationManager
Теперь, когда я получил местоположение пользователя, я хочу построить путь от этого набора коордов до моего пункта назначения. Я попытался с помощью MKDirections
для этого
Вот как я пытался сделать это:
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)
}
}
Однако, когда я попытался ее отладки, я получаю следующее сообщение об ошибке: 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}
Я сделал некоторые и выяснили, что MKDirections
не доступен в India, поэтому мне было интересно, как можно обойти, показывая навигационные указания для пользователя.
Спасибо за помощь!
Лучший вариант использовать GOOGLEPLACEAPI для направления Досталось и placeinformation. –