Ayu*_*pta 9 mkmapview cllocationmanager ios swift swift2
我mapView.showUserLocation = true用来绘制用户位置并将其显示为我的MKMapView对象上的注释.
现在,当我获得用户的位置时,我想绘制从该组坐标到目的地的路径.我试过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)
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试调试它时,我收到以下错误:
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在印度没有,所以我想知道如何向用户展示导航方向.
谢谢您的帮助!
Error Domain=MKErrorDomain Code=5 "Directions Not Available"
Run Code Online (Sandbox Code Playgroud)
如果该位置不属于此列表中的任何国家/地区,您很可能会收到此错误:http://www.apple.com/ios/feature-availability/#maps-directions
在 iOS 模拟器上,您可以轻松自定义当前位置。有两种方法可以做到这一点:
iOS 模拟器 ->“调试”选项卡 -> 位置 -> {选择}
Xcode ->“调试”选项卡 -> 模拟位置 -> {选择}
所以我建议你使用这个。GoogleMaps SDK 和绘制路线。
https://gist.github.com/himanshu-benzatine/10670936c8f16ea1ae482bc6bb684adc
| 归档时间: |
|
| 查看次数: |
1809 次 |
| 最近记录: |