我试图在苹果地图上绘制两点之间的路线(Swift 代码)。
下面的结构体用于存储坐标
struct GeoLocation {
var latitude: Double
var longitude: Double
func distanceBetween(other: GeoLocation) -> Double {
let locationA = CLLocation(latitude: self.latitude, longitude: self.longitude)
let locationB = CLLocation(latitude: other.latitude, longitude: other.longitude)
return locationA.distanceFromLocation(locationB)
}
}
self.foundLocations - is an array of these structures
在自定义类中,我收到地图上点的坐标。
var coordinates = self.foundLocations.map{$0.coordinate}
然后我在地图上画出路线
self.polyline = MKPolyline(coordinates: &coordinates, count: coordinates.count)
self.mapView.addOverlay(self.polyline, level: MKOverlayLevel.AboveRoads)
为了绘制路线,我使用 MKMapViewDelegate 中的以下方法
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if let polylineOverlay = overlay as? MKPolyline {
let render = MKPolylineRenderer(polyline: polylineOverlay)
render.strokeColor = UIColor.blueColor()
return render
}
return nil
}
我得到的只是两点之间的一条直线,而不是道路上的实际路线。
如何显示实际路线?