Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Google Maps to show route using Swift

I read around the Internet but couldn't find a legit answer. I need to open Google Maps when the user clicks a button to show directions. Start and destination must be automatically filled.

How can I achieve this in Swift?

If anyone has a solution, please provide me with a sample code. The start is always going to be the user's current location.

like image 520
sumesh Avatar asked Aug 16 '15 20:08

sumesh


1 Answers

OK I found the answer myself.

If you want to show directions from the user's current location, leave the field saddr blank and in the field daddr you can enter the destination coordinates.

This is how I did it

if (UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!)) {             UIApplication.sharedApplication().openURL(NSURL(string:                 "comgooglemaps://?saddr=&daddr=\(place.latitude),\(place.longitude)&directionsmode=driving")!)          } else {             NSLog("Can't use comgooglemaps://");         }     } 

for any further queries you can refer to this link Google Map URL Scheme

like image 125
sumesh Avatar answered Sep 25 '22 08:09

sumesh