Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display route on map in Swift

Tags:

ios

swift

I am trying to draw the route between two points on Apple map (Swift code). The following structure is used to store the coordinates

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 the custom class I recieve the coordinates of the points on the map.

var coordinates = self.foundLocations.map{$0.coordinate}

Then I draw the route on the map

self.polyline = MKPolyline(coordinates: &coordinates, count: coordinates.count)
        self.mapView.addOverlay(self.polyline, level: MKOverlayLevel.AboveRoads)

To draw the 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 the actual route laying on roads I get just a straight line between two points. How can I display the actual route?

like image 840
Oleksandr Isaiev Avatar asked Feb 25 '15 15:02

Oleksandr Isaiev


1 Answers

You actually have to fetch the route from Apple's maps' server using calculateDirectionsWithCompletionHandler.

First create the relevant MKMapItems for both the source and destination, ex:

let geocoder = CLGeocoder()
let location = CLLocation(latitude: sourceLatitude, longitude: sourceLongitude)

geocoder.reverseGeocodeLocation(location, completionHandler: {
        (placemarks:[AnyObject]?, error:NSError?) -> Void in
        if placemarks?.count > 0 {
            if let placemark: MKPlacemark = placemarks![0] as? MKPlacemark {
                self.source =  MKMapItem(placemark: placemark)
            }
        }
        })

(Repeat for destination.)

Then fetch the MKRoute, ex:

let request:MKDirectionsRequest = MKDirectionsRequest()

// source and destination are the relevant MKMapItems
request.setSource(source)
request.setDestination(destination)

// Specify the transportation type
request.transportType = MKDirectionsTransportType.Automobile;

// If you're open to getting more than one route, 
// requestsAlternateRoutes = true; else requestsAlternateRoutes = false;
request.requestsAlternateRoutes = true

let directions = MKDirections(request: request)

directions.calculateDirectionsWithCompletionHandler ({
    (response: MKDirectionsResponse?, error: NSError?) in

    if error == nil {
        self.directionsResponse = response
        // Get whichever currentRoute you'd like, ex. 0
        self.route = directionsResponse.routes[currentRoute] as MKRoute
    }
})

Then after retrieving the MKRoute, you can add the polyline to the map like so:

mapView.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)
like image 187
Lyndsey Scott Avatar answered Oct 10 '22 02:10

Lyndsey Scott