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

后端 未结 2 2002
慢半拍i
慢半拍i 2020-12-31 06:09

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 map

2条回答
  •  一生所求
    2020-12-31 06:37

    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.

    LSApplicationQueriesSchemes
    
        googlechromes
        comgooglemaps
    
    

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

提交回复
热议问题