MKDirections : 方向不可用
MKDirections : Directions Not Available
我正在创建一个类似 UBER 的应用程序,我正在使用 MapKit,我想显示 route 在两个位置之间,为此我使用了以下代码
viewMap.delegate = self
let sourceLocation = CLLocationCoordinate2D(latitude: sourceLatitude, longitude: sourceLongitude)
let destinationLocation = CLLocationCoordinate2D(latitude: destinationLatitude, longitude: destinationLongitude)
let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
let sourceAnnotation = MKPointAnnotation()
sourceAnnotation.title = strSource
if let location = sourcePlacemark.location {
sourceAnnotation.coordinate = location.coordinate
}
let destinationAnnotation = MKPointAnnotation()
destinationAnnotation.title = strDestination
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
self.viewMap.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Route Error: \(error)")
}
return
}
let route = response.routes[0]
self.viewMap.add((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
self.viewMap.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
}
扩展 HomeVC:MKMapViewDelegate
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
renderer.lineWidth = 2.0
return renderer
}
我已经检查了所有可能的解决方案,我也得到了源位置和目标位置的纬度和经度,但我仍然无法正常工作,并且出现以下错误。
请帮我解决这个问题!
Apple 地图方向仅适用于特定国家/地区。请在此处查看完整列表。 Apple Map Availability list(查看 地图:方向 部分)。您应该查看 Google 您的应用程序中的 Map SDK 是否针对以上未列出的国家/地区 link。
这对我有用,它在 MK 地图中绘制了目的地,它也聚焦到目的地
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let request = MKDirectionsRequest()
request.source = MKMapItem.forCurrentLocation()
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: (view.annotation?.coordinate)!))
request.requestsAlternateRoutes = true
request.requestsAlternateRoutes = true
request.transportType = .walking
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
for route in unwrappedResponse.routes {
self.myMapView.add(route.polyline) self.myMapView.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
return
}
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let polyLine = overlay
let polyLineRenderer = MKPolylineRenderer(overlay: polyLine)
polyLineRenderer.strokeColor = UIColor.blue
polyLineRenderer.lineWidth = 2.0
return polyLineRenderer
}
这在您单击 MKAnnotationView 时有效,如果您想从另一个代码中绘制,您只需将代码剪切到您的函数并更改行
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: (view.annotation?.coordinate)!))
至
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: (CLLocationCoordinate2D(/*Your Location*/))!))
我正在创建一个类似 UBER 的应用程序,我正在使用 MapKit,我想显示 route 在两个位置之间,为此我使用了以下代码
viewMap.delegate = self
let sourceLocation = CLLocationCoordinate2D(latitude: sourceLatitude, longitude: sourceLongitude)
let destinationLocation = CLLocationCoordinate2D(latitude: destinationLatitude, longitude: destinationLongitude)
let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
let sourceAnnotation = MKPointAnnotation()
sourceAnnotation.title = strSource
if let location = sourcePlacemark.location {
sourceAnnotation.coordinate = location.coordinate
}
let destinationAnnotation = MKPointAnnotation()
destinationAnnotation.title = strDestination
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
self.viewMap.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Route Error: \(error)")
}
return
}
let route = response.routes[0]
self.viewMap.add((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
self.viewMap.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
}
扩展 HomeVC:MKMapViewDelegate
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
renderer.lineWidth = 2.0
return renderer
}
我已经检查了所有可能的解决方案,我也得到了源位置和目标位置的纬度和经度,但我仍然无法正常工作,并且出现以下错误。
请帮我解决这个问题!
Apple 地图方向仅适用于特定国家/地区。请在此处查看完整列表。 Apple Map Availability list(查看 地图:方向 部分)。您应该查看 Google 您的应用程序中的 Map SDK 是否针对以上未列出的国家/地区 link。
这对我有用,它在 MK 地图中绘制了目的地,它也聚焦到目的地
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let request = MKDirectionsRequest()
request.source = MKMapItem.forCurrentLocation()
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: (view.annotation?.coordinate)!))
request.requestsAlternateRoutes = true
request.requestsAlternateRoutes = true
request.transportType = .walking
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
for route in unwrappedResponse.routes {
self.myMapView.add(route.polyline) self.myMapView.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
return
}
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let polyLine = overlay
let polyLineRenderer = MKPolylineRenderer(overlay: polyLine)
polyLineRenderer.strokeColor = UIColor.blue
polyLineRenderer.lineWidth = 2.0
return polyLineRenderer
}
这在您单击 MKAnnotationView 时有效,如果您想从另一个代码中绘制,您只需将代码剪切到您的函数并更改行
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: (view.annotation?.coordinate)!))
至
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: (CLLocationCoordinate2D(/*Your Location*/))!))