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
What Is an "Unwrapped Value" in Swift
How to See Saved Nsuserdefaults
Firebasestorage: How to Delete Directory
How to Test Apple Push Notification Service Without an Iphone
Enterprise App Update Distribution on iOS 8
Fade/Dissolve When Changing Uiimageview'S Image
Get Indexpath of Uitableviewcell on Click of Button from Cell
How to Get the Color of a Pixel in a Uiimage With Swift
How to Create Percentage of Total Width Using Autolayout
Converting Hex String to Nsdata in Swift
Resizing Uitableview to Fit Content
How to Wrap Text in a Uitableviewcell Without a Custom Cell
How to Handle Different Orientations in Ios
How to Support Universal Links in iOS App and Setup Server For It
How to Create Launch Images For Iphone 6/6 Plus Landscape Only Apps
Enabling Auto Layout in iOS 6 While Remaining Backwards Compatible With iOS 5