How would I be able to open google maps when I press a button in my app? How would I be able to open google maps when I press a button in my app? xcode xcode

How would I be able to open google maps when I press a button in my app?


Use this:

if node.name == "openMaps" {    let customURL = "comgooglemaps://"    if UIApplication.sharedApplication().canOpenURL(NSURL(string: customURL)) {        UIApplication.sharedApplication().openURL(NSURL(string: customURL))    }    else {        var alert = UIAlertController(title: "Error", message: "Google maps not installed", preferredStyle: UIAlertControllerStyle.Alert)        var ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)        alert.addAction(ok)        self.presentViewController(alert, animated:true, completion: nil)    }}

You can find more info about the google maps URL scheme here

Edit: You must add a key to your info.plist for this to work.

<key>LSApplicationQueriesSchemes</key><array>    <string>googlechromes</string>    <string>comgooglemaps</string></array>

Edit: Per updated Google Maps docs added "googlechromes" to plist above also.


At button click (in action) call function "directions()" with following code :

func directions() {    // if GoogleMap installed    if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {        UIApplication.shared.openURL(NSURL(string:            "comgooglemaps://?saddr=&daddr=\(Float(event.addressLat)!),\(Float(event.addressLong)!)&directionsmode=driving")! as URL)    } else {        // if GoogleMap App is not installed        UIApplication.shared.openURL(NSURL(string:            "https://maps.google.com/?q=@\(Float(event.addressLat)!),\(Float(event.addressLong)!)")! as URL)    }}

Edit your info.plist for LSApplicationQueriesSchemes :

enter image description here

Hope will help!