I am using UIMapView to display locations on the iPhone. I want to do a directions from current location to the location of interest, I don't think its possible using MapKit (but if it is please inform) So I will open either the Google Maps application or safari to display it.
Can i do this by specifying co-ordinates from (current location) to co-ordinates (the location of interest) I have these longitudes and latitudes. Or do i have to use street addresses?
If I do have to use street addresses, can i get them from the latitude and longitude.
You can input and search maps by GPS coordinates on the iPhone easily by using the Apple Maps or Google Maps applications, as we'll demonstrate in this walkthrough.
So, open Google Maps, pop the coordinates into the Search box at the top and hit the Search key on the keyboard. You'll then see a pin on the map for the spot with its coordinates. Swipe up from the bottom to obtain directions or take another action on the location.
Yeah, it's not possible using MapKit. You could try to form a Google maps url request that contains both your current location and destination that will open in the Google maps app with the directions.
Here's an example url:
http://maps.google.com/?saddr=34.052222,-118.243611&daddr=37.322778,-122.031944
Here's how you could implement this in your code:
CLLocationCoordinate2D start = { 34.052222, -118.243611 }; CLLocationCoordinate2D destination = { 37.322778, -122.031944 }; NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", start.latitude, start.longitude, destination.latitude, destination.longitude]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
Use bellow code for both google and apple maps in Swift 3 -
if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps://")!) { let urlString = "http://maps.google.com/?daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&directionsmode=driving" // use bellow line for specific source location //let urlString = "http://maps.google.com/?saddr=\(sourceLocation.latitude),\(sourceLocation.longitude)&daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&directionsmode=driving" UIApplication.shared.openURL(URL(string: urlString)!) } else { //let urlString = "http://maps.apple.com/maps?saddr=\(sourceLocation.latitude),\(sourceLocation.longitude)&daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&dirflg=d" let urlString = "http://maps.apple.com/maps?daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&dirflg=d" UIApplication.shared.openURL(URL(string: urlString)!) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With