Drawing a Route in Mapkit in Ios

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
}

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!

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.

Draw route on pin touch mapkit swift

As always, I find my information right after I ask a question...
So for those who need the information too:

To trigger touch event when tapping on a mapkit pin, you just need to use this MKMapViewDelegate function:

func mapView(mapView: MKMapView!,
didSelectAnnotationView view: MKAnnotationView!){

println("Selected annotation")
}

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

How to draw user's route in MapView

I asume you used the following delegate to get the last position?

func locationManager(manager: CLLocationManager!,
didUpdateToLocation newlocation: CLLocation!,
fromLocation oldLocation: CLLocation!) {

The delegate above is deprecated in iOS 6. Now the following should be used:

func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {

In order to get the last position, simply get the last object of the array:

locations.lastObject

Try the following code to draw user's route:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let newLocation = locations.last else {
return
}

guard let oldLocation = oldLocation else {
// Save old location
self.oldLocation = newLocation
return
}

let oldCoordinates = oldLocation.coordinate
let newCoordinates = newLocation.coordinate
var area = [oldCoordinates, newCoordinates]
let polyline = MKPolyline(coordinates: &area, count: area.count)
mapView.add(polyline)

// Save old location
self.oldLocation = newLocation
}


Related Topics



Leave a reply



Submit