I am trying to route between two points on an Apple map (Swift code). To store coordinates
The following structure is used:
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
In a custom class, I get the coordinates of the points on the map.
var coordinates = self.foundLocations.map{$0.coordinate}
Then I draw a route on a map
self.polyline = MKPolyline(coordinates: &coordinates, count: coordinates.count) self.mapView.addOverlay(self.polyline, level: MKOverlayLevel.AboveRoads)
To draw a route, I use the following method from 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 }
Instead of actually plotting the route along the roads, I get only a straight line between two points. How can I display the actual route?
source share