Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have this app and I want to use google maps or apple maps to open when the user presses a button in my app. How would I be able to do this? Is there like a link to the maps that opens the app or is it something else? If you can point me in the right direction it would be really helpful. Thanks! I have the button set up below like this:

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch in (touches ) {
    let location = touch.locationInNode(self)
    let node = self.nodeAtPoint(location)

    if node.name == "openMaps" {

     //code to open google maps or apple maps....

    }
like image 203
coding22 Avatar asked Sep 24 '15 23:09

coding22


People also ask

Why is my Google Maps not opening?

Clear the app's cache & data On your Android phone or tablet, open the Settings app . Tap Apps & notifications. Follow the steps on your device to find the Maps app. After you select the app, storage & cache options should be available.

How do I make my iPhone open Google Maps?

Check Your App OptionsTap on the address and tap the share button. If you have Google Maps installed on your phone, you should see it as an option for opening the directions to that destination.


2 Answers

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.

like image 62
Sman25 Avatar answered Oct 04 '22 17:10

Sman25


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!

like image 41
JaspreetKour Avatar answered Oct 04 '22 18:10

JaspreetKour