当我按下应用程序中的按钮时,如何打开Goog​​le地图? [英] How would I be able to open google maps when I press a button in my app?

查看:108
本文介绍了当我按下应用程序中的按钮时,如何打开Goog​​le地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个应用程序,当用户在我的应用程序中按下按钮时,我想使用谷歌地图或苹果地图打开。我将如何能够做到这一点?有没有像打开应用程序的地图的链接,还是其他东西?如果你能指出我正确的方向,那将是非常有用的。谢谢!

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

触摸(触摸){
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)

if node.name ==openMaps{

//打开谷歌地图或苹果地图的代码....

}

<$ p $


$ b $如果使用UIApplication.sharedApplication()。canOpenURL(NSURL()();如果node.name ==openMaps{
let customURL =comgooglemaps://

,字符串: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:UIAlertAc tionStyle.Default,handler:nil)
alert.addAction(ok)
self.presentViewController(alert,animated:true,completion:nil)
}
}

您可以在Google地图网址方案此处

编辑:您必须为您的钥匙添加钥匙 info.plist 为此工作。

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

编辑:每更新 Google地图文档在plist上添加了googlechromes。


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....

    }

解决方案

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.

这篇关于当我按下应用程序中的按钮时,如何打开Goog​​le地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆