路线未显示在MKMapView中?

路线未显示在MKMapView中?,第1张

路线未显示在MKMapView中?

实际上,源变量和目标变量都为nil。因此,我从server。如果收到错误响应,请尝试以下代码

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    }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4916181.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存