How to Draw a Route Between Two Locations Using Mapkit in Swift

How to draw a route in mapKit?

Implement rendererFor method.

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 3
return renderer
}

Draw a path between two coordinates using MapKit Swift

Simply look at the documentation for MKDirectionsRequest. There is a source property and a destination property.

The lines:

directionsRequest.setSource(MKMapItem(placemark: markSecondPoint))
directionsRequest.setDestination(MKMapItem(placemark: markFirstPoint))

become:

directionsRequest.source = MKMapItem(placemark: markSecondPoint)
directionsRequest.destination = MKMapItem(placemark: markFirstPoint)

Draw route on map in swift 4

Looks like Apple Maps doesn't yet support India. (quora.com/…) I guess if this is a critical part of your app, you may have to consider Google Maps

Those new coordinates are in India, and it just looks like directions are not available there. I get an error that says "Error Domain=MKErrorDomain Code=4 "Directions Not Available" UserInfo={NSLocalizedDescription=Directions Not Available, MKErrorGEOError=-8, MKErrorGEOErrorUserInfo={ }, MKErrorGEOTransitIncidentKey=<_GEOTransitRoutingIncidentMessage: 0x60800023df20>, MKDirectionsErrorCode=0, NSLocalizedFailureReason=Directions are not available between these locations.}"

Thanx Rob for this answar.

How to draw route between two locations and plot main points also using MapKit?

The question has been asked several times. I guess you are taking code from http://iosguy.com/2012/05/22/tracing-routes-with-mapkit/
You can also look at that SO question: Plotting Route with Multiple Points in iOS

You can get the code from http://iosboilerplate.com too and contribute to it.

And last but not least, there's a framework out there that can help you do it for a small sum of money (but nothing compared to what it would take you to do same):
http://www.cocoacontrols.com/controls/mtdirectionskit

Drawing a route in MapKit in iOS

This is a tricky one. There is no way to do that with MapKit: it's easy enough to draw lines when you know the coordinates, but MapKit won't give you access to the roads or other routing information. I'd say you need to call an external API to get your data.

I've been playing with cloudmade.com API. The vector stream server should return what you need, and then you can draw that over your map. However, discrepancies between the Google maps and the OSM maps used by cloudmade may make you want to use cloudmade maps all the way: they have an equivalent to MapKit.

P.S.: Other mapping providers - Google, Bing, etc. may also provide equivalent data feeds. I've just been looking at OSM/Cloudmade recently.

P.P.S.: None of this is trivial newbie stuff! Best of luck!



Related Topics



Leave a reply



Submit