我有这些方法在两个CLLocation
之间添加MKMapView的路由。我有两个有效的pickUpDistanceLocation& dropOffDistanceLocation
func addRoutesOverLayForMapView(){
var source:MKMapItem?
var destination:MKMapItem?
println("\(pickUpDistanceLocation)")
println("\(dropOffDistanceLocation)")
//i also tested with these locations
//let sourcelocation = CLLocation(latitude: 40.7141667, longitude: -74.0063889)
//let destinationLocation = CLLocation(latitude: 38.89, longitude: 77.03)
CLGeocoder().reverseGeocodeLocation(pickUpDistanceLocation, completionHandler: {(placemarks,error)-> Void in
if (error != nil) {
println("Reverse geocoder failed with error" + error.localizedDescription)
return
}
if placemarks.count > 0 {
if let placemark: MKPlacemark = placemarks![0] as? MKPlacemark {
source = MKMapItem(placemark: placemark)
println("\(source)")
}
} else {
println("Problem with the data received from geocoder")
}
})
CLGeocoder().reverseGeocodeLocation(dropOffDistanceLocation, completionHandler: {(placemarks,error)-> Void in
if (error != nil) {
println("Reverse geocoder failed with error" + error.localizedDescription)
return
}
if placemarks.count > 0 {
if let placemark: MKPlacemark = placemarks![0] as? MKPlacemark {
destination = MKMapItem(placemark: placemark)
println("\(destination)")
}
} else {
println("Problem with the data received from geocoder")
}
})
let request:MKDirectionsRequest = MKDirectionsRequest()
request.setSource(source)
request.setDestination(destination)
request.transportType = MKDirectionsTransportType.Automobile
request.requestsAlternateRoutes = false
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler ({
(response: MKDirectionsResponse?, error: NSError?) in
if error == nil {
self.showRoute(response!)
}
})
}
这是在地图中添加路线叠加的方法
func showRoute(response:MKDirectionsResponse){
for route in response.routes as! [MKRoute]{
mapView.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)
}
}
我收到此错误,因为响应返回错误:400
打印错误时显示为
可选(“操作无法完成。(NSURLErrorDomain 错误-1011。)“)
答案 0 :(得分:4)
实际上源和目标变量都是零。所以我得到了来自服务器的错误响应。如果您只需要尝试以下代码
func addRoutesOverLayForMapView(){
var source:MKMapItem?
var destination:MKMapItem?
var sourcePlacemark = MKPlacemark(coordinate: pickUpDistanceLocation!.coordinate, addressDictionary: nil)
source = MKMapItem(placemark: sourcePlacemark)
var desitnationPlacemark = MKPlacemark(coordinate: dropOffDistanceLocation!.coordinate, addressDictionary: nil)
destination = MKMapItem(placemark: desitnationPlacemark)
let request:MKDirectionsRequest = MKDirectionsRequest()
request.setSource(source)
request.setDestination(destination)
request.transportType = MKDirectionsTransportType.Walking
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler ({
(response: MKDirectionsResponse?, error: NSError?) in
if error == nil {
self.showRoute(response!)
}
else{
println("trace the error \(error?.localizedDescription)")
}
})
}
func showRoute(response:MKDirectionsResponse){
for route in response.routes as! [MKRoute]{
mapView.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)
var routeSeconds = route.expectedTravelTime
let routeDistance = route.distance
println("distance between two points is \(routeSeconds) and \(routeDistance)")
}
}
你应该实现这个委托方法,不要忘记设置mapview委托
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
var polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.lineDashPattern = [14,10,6,10,4,10]
polylineRenderer.strokeColor = UIColor(red: 0.012, green: 0.012, blue: 0.012, alpha: 1.00)
polylineRenderer.lineWidth = 2.5
return polylineRenderer
}
return nil
}
答案 1 :(得分:2)
对于Swift 2.1:
func addRoutesOverLayForMapView(){
var source:MKMapItem?
var destination:MKMapItem?
let Scoordinate = CLLocationCoordinate2D(latitude: passLat, longitude: passLong)
var sourcePlacemark = MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: passLat, longitude: passLong), addressDictionary: nil)
source = MKMapItem(placemark: sourcePlacemark)
let DCoordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)
var desitnationPlacemark = MKPlacemark(coordinate: DCoordinate, addressDictionary: nil)
destination = MKMapItem(placemark: desitnationPlacemark)
let request:MKDirectionsRequest = MKDirectionsRequest()
request.source = source
request.destination = destination
request.transportType = MKDirectionsTransportType.Walking
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler ({
(response: MKDirectionsResponse?, error: NSError?) in
if error == nil {
self.showRoute(response!)
}
else{
print("some error")
}
})
}
func showRoute(response:MKDirectionsResponse){
for route in response.routes {
mapView.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)
let routeSeconds = route.expectedTravelTime
let routeDistance = route.distance
print("distance between two points is \(routeSeconds) and \(routeDistance)")
}
}
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
let polylineRenderer = MKPolylineRenderer(overlay: overlay)
if overlay is MKPolyline {
polylineRenderer.lineDashPattern = [14,10,6,10,4,10]
polylineRenderer.strokeColor = UIColor(red: 0.012, green: 0.012, blue: 0.012, alpha: 1.00)
polylineRenderer.lineWidth = 2.5
return polylineRenderer
}
return polylineRenderer
}