【问题标题】:How can I launch the Google Maps iPhone application from within my own native application?如何从我自己的本机应用程序中启动 Google Maps iPhone 应用程序?
【发布时间】:2010-09-07 00:01:44
【问题描述】:

Apple Developer Documentation(链接现已失效)解释说,如果您在网页中放置一个链接,然后在 iPhone 上使用 Mobile Safari 时单击它,则作为 iPhone 标准提供的 Google 地图应用程序将启动.

如何在我自己的本地 iPhone 应用程序(即不是通过 Mobile Safari 的网页)中启动具有特定地址的同一 Google 地图应用程序,就像在“通讯录”中点击地址启动地图一样?

注意:这仅适用于设备本身。不在模拟器中。

【问题讨论】:

  • 它可以在模拟器中运行,它只是在移动 safari 中打开。

标签: ios objective-c google-maps


【解决方案1】:

这是地图链接的 Apple URL 方案参考:https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html

创建有效地图链接的规则如下:

  • 域必须是 google.com,子域必须是 maps 或 ditu。
  • 如果查询包含 site 作为键和 local 作为值,则路径必须是 /、/maps、/local 或 /m。
  • 路径不能是 /maps/*。
  • 必须支持所有参数。支持的参数列表见表 1**。
  • 如果值为 URL,则参数不能为 q=*(因此不会提取 KML)。
  • 参数不能包含 view=text 或 dirflg=r。

**有关支持的参数列表,请参见上面的链接。

【讨论】:

    【解决方案2】:

    Swift 4 上的工作代码:

    第 1 步 - 将关注添加到 info.plist

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

    第 2 步 - 使用以下代码显示 Google 地图

        let destinationLatitude = "40.7128"
        let destinationLongitude = "74.0060"
    
        if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps:")!) {
            if let url = URL(string: "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"), !url.absoluteString.isEmpty {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
        }else{
            if let url = URL(string: "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"), !url.absoluteString.isEmpty {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
        }
    

    【讨论】:

      【解决方案3】:

      只需调用此方法并将 Google Maps URL Scheme 添加到您的 .plist 文件中,与此 Answer 相同。

      Swift-4 :-

      func openMapApp(latitude:String, longitude:String, address:String) {
      
          var myAddress:String = address
      
          //For Apple Maps
          let testURL2 = URL.init(string: "http://maps.apple.com/")
      
          //For Google Maps
          let testURL = URL.init(string: "comgooglemaps-x-callback://")
      
          //For Google Maps
          if UIApplication.shared.canOpenURL(testURL!) {
              var direction:String = ""
              myAddress = myAddress.replacingOccurrences(of: " ", with: "+")
      
              direction = String(format: "comgooglemaps-x-callback://?daddr=%@,%@&x-success=sourceapp://?resume=true&x-source=AirApp", latitude, longitude)
      
              let directionsURL = URL.init(string: direction)
              if #available(iOS 10, *) {
                  UIApplication.shared.open(directionsURL!)
              } else {
                  UIApplication.shared.openURL(directionsURL!)
              }
          }
          //For Apple Maps
          else if UIApplication.shared.canOpenURL(testURL2!) {
              var direction:String = ""
              myAddress = myAddress.replacingOccurrences(of: " ", with: "+")
      
              var CurrentLocationLatitude:String = ""
              var CurrentLocationLongitude:String = ""
      
              if let latitude = USERDEFAULT.value(forKey: "CurrentLocationLatitude") as? Double {
                  CurrentLocationLatitude = "\(latitude)"
                  //print(myLatitude)
              }
      
              if let longitude = USERDEFAULT.value(forKey: "CurrentLocationLongitude") as? Double {
                  CurrentLocationLongitude = "\(longitude)"
                  //print(myLongitude)
              }
      
              direction = String(format: "http://maps.apple.com/?saddr=%@,%@&daddr=%@,%@", CurrentLocationLatitude, CurrentLocationLongitude, latitude, longitude)
      
              let directionsURL = URL.init(string: direction)
              if #available(iOS 10, *) {
                  UIApplication.shared.open(directionsURL!)
              } else {
                  UIApplication.shared.openURL(directionsURL!)
              }
      
          }
          //For SAFARI Browser
          else {
              var direction:String = ""
              direction = String(format: "http://maps.google.com/maps?q=%@,%@", latitude, longitude)
              direction = direction.replacingOccurrences(of: " ", with: "+")
      
              let directionsURL = URL.init(string: direction)
              if #available(iOS 10, *) {
                  UIApplication.shared.open(directionsURL!)
              } else {
                  UIApplication.shared.openURL(directionsURL!)
              }
          }
      }
      

      希望,这就是您要找的。任何问题都可以回复我。 :)

      【讨论】:

      • 没用。我讲情节。在按钮上单击我通过saddr 和daddr 传递url 并调用UIApplication.shared.canOpenURL(unwrappedURL)。任何帮助
      • 在模拟器中,您没有谷歌地图应用程序。所以你的 else 条件将被执行。并且 URL 在 SAFARI 浏览器中打开。请卸载设备上的谷歌地图应用程序,检查它。它将与模拟器相同。
      • 谢谢,题外话了。我如何检查 iphone 中是否安装了 Gmap
      • 看来您还没有使用过 iPhone。只需打开 Appstore,搜索 Google 地图。并检查是否有安装选项或打开应用程序选项。另一种选择是,通过在 iPhone 主屏幕上按下手势,直接在打开的位置搜索谷歌地图应用程序。
      • 是的,我不是更好的安卓用户。我想我没有正确地提问。如何通过代码检查谷歌地图应用程序是否安装在 iPhone 中?打开它或重定向到应用商店使用
      【解决方案4】:

      要移动到谷歌地图,请使用此 api 并发送目的地纬度和经度

      NSString* addr = nil;
           addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", destinationLat,destinationLong];
      
      NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
      [[UIApplication sharedApplication] openURL:url];
      

      【讨论】:

        【解决方案5】:

        如果您使用的是 ios 10,请不要忘记在 Info.plist 中添加查询方案

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

        如果你使用的是objective-c

        if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps:"]]) {
            NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?ll=%@,%@",destinationLatitude,destinationLongitude];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
            } else { 
            NSString *string = [NSString stringWithFormat:@"http://maps.google.com/maps?ll=%@,%@",destinationLatitude,destinationLongitude];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
            }
        

        如果您使用的是 swift 2.2

        if UIApplication.sharedApplication().canOpenURL(NSURL(string: "comgooglemaps:")!) {
            var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
            UIApplication.sharedApplication().openURL(NSURL(string: urlString)!)
        }
        else {
            var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
            UIApplication.sharedApplication().openURL(NSURL(string: string)!)
        }
        

        如果您使用的是 swift 3.0

        if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps:")!) {
            var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
            UIApplication.shared.openURL(URL(string: urlString)!)
        }
        else {
            var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
            UIApplication.shared.openURL(URL(string: string)!)
        }
        

        【讨论】:

          【解决方案6】:
          **Getting Directions between 2 locations**
          
                  NSString *googleMapUrlString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%@,%@&daddr=%@,%@", @"30.7046", @"76.7179", @"30.4414", @"76.1617"];
              [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapUrlString]];
          

          【讨论】:

            【解决方案7】:

            iPhone4 iOS 6.0.1 (10A523)

            适用于 Safari 和 Chrome。 迄今为止的最新版本(2013 年 6 月 10 日)。

            下面的 URL 方案也可以使用。 但是如果 Chrome 只能在页面内部工作,则不能从地址栏工作。

            maps:q=GivenTitle@latitude,longtitude

            【讨论】:

              【解决方案8】:

              现在还有 App Store Google Maps 应用,记录在 https://developers.google.com/maps/documentation/ios/urlscheme

              所以你首先要检查它是否已安装:

              [[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps://"]];

              然后您可以有条件地将http://maps.google.com/maps?q= 替换为comgooglemaps://?q=

              【讨论】:

                【解决方案9】:

                如果您需要比 Google URL 格式更大的灵活性,或者您想在应用程序中嵌入地图而不是启动地图应用程序here is an example

                它甚至会为您提供执行所有嵌入的源代码。

                【讨论】:

                  【解决方案10】:

                  对于 iOS 5.1.1 及更低版本,使用 UIApplicationopenURL 方法。它将执行正常的 iPhone 神奇 URL 重新解释。所以

                  [someUIApplication openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]]
                  

                  应该调用 Google 地图应用程序。

                  从 iOS 6 开始,您将调用 Apple 自己的地图应用程序。为此,请使用您要显示的位置配置 MKMapItem 对象,然后向其发送 openInMapsWithLaunchOptions 消息。要从当前位置开始,请尝试:

                  [[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];
                  

                  为此,您需要与 MapKit 相关联(我相信它会提示您进行位置访问)。

                  【讨论】:

                  • 带有参数描述的 Wiki:mapki.com/wiki/Google_Map_Parameters Apple iphone os 编程指南附录 A 列出了 iphone 上支持的参数。
                  • 这是泄漏的 - 您应该使用 [NSURL URLWithString:] 而不是 [[NSURL alloc] initWithString:],因为它提供了一个自动释放的对象。
                  • @Tim,已修复 - 谢谢。公平地说,首次发布此答案的时间并不重要,因为应用程序将在通话完成后立即终止。
                  • 它总是很重要!此外,您可以只输入[UIApplication sharedApplication] 而不是someUIApplication(因为我实际上不知道除了制作一个您会得到UIApplication 的任何其他情况)
                  • 根据 Apple 的文档 (developer.apple.com/library/ios/#featuredarticles/…),您应该使用 maps.apple.com,如:maps.apple.com/?q=London 这样可以避免根据 iOS 5 的操作系统版本输入不同的代码,它将重定向到谷歌地图。
                  【解决方案11】:

                  如果您需要比 Google URL 格式提供的更多灵活性,或者您想在应用程序中嵌入地图而不是启动地图应用程序,可以在 https://sourceforge.net/projects/quickconnect 找到一个示例。

                  【讨论】:

                    【解决方案12】:

                    没错。实现这一点所需的代码是这样的:

                    UIApplication *app = [UIApplication sharedApplication];
                    [app openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]];
                    

                    由于as per the documentation,除非您调用 sharedApplication,否则 UIApplication 仅在 Application Delegate 中可用。

                    【讨论】:

                    【解决方案13】:

                    如果您仍然遇到问题,此视频展示了如何从 google 获取“我的地图”以显示在 iphone 上 - 然后您可以获取链接并将其发送给任何人,并且它可以工作。

                    http://www.youtube.com/watch?v=Xo5tPjsFBX4

                    【讨论】:

                      【解决方案14】:

                      要在特定坐标处打开 Google 地图,请尝试以下代码:

                      NSString *latlong = @"-56.568545,1.256281";
                      NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@",
                      [latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
                      

                      您可以将 latlong 字符串替换为 CoreLocation 中的当前位置。

                      您还可以使用 (“z”) 标志指定缩放级别。值为 1-19。这是一个例子:

                      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];

                      【讨论】:

                      【解决方案15】:

                      “g”改为“q”

                      [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]]
                      

                      【讨论】:

                        【解决方案16】:

                        对于电话问题,您是否在模拟器上进行测试?这仅适用于设备本身。

                        此外,openURL 返回一个布尔值,您可以使用它来检查您正在运行的设备是否支持该功能。例如,您不能在 iPod Touch 上拨打电话 :-)

                        【讨论】:

                          猜你喜欢
                          • 2013-08-14
                          • 2016-06-13
                          • 1970-01-01
                          • 2012-02-07
                          • 2015-05-26
                          • 1970-01-01
                          • 1970-01-01
                          • 2012-05-17
                          • 1970-01-01
                          相关资源
                          最近更新 更多