如何用掉落的针脚打开谷歌地图应用程序? - 斯威夫特

Ziz*_*zoo 9 google-maps ios google-maps-sdk-ios swift

我有这个代码打开具有特定位置的Google Maps App并且它正在运行但我需要的是在该位置放置一个引脚,是否可能?

if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {

                UIApplication.shared.openURL(URL(string:
                    "comgooglemaps://?center=\(self.location.coordinate.latitude),\(self.location.coordinate.longitude)&zoom=14&views=traffic")!)
            } else {
                print("Can't use comgooglemaps://");
            }
Run Code Online (Sandbox Code Playgroud)

怎么做?

Raj*_*r R 24

试试这个

if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
        UIApplication.shared.open(URL(string:"comgooglemaps://?center=\(self.location.coordinate.latitude),\(self.location.coordinate.longitude)&zoom=14&views=traffic&q=\(self.location.coordinate.latitude),\(self.location.coordinate.longitude)")!, options: [:], completionHandler: nil)
    } else {
        print("Can't use comgooglemaps://")
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 有什么方法可以在标记中显示位置名称而不是坐标? (6认同)

Emm*_*Emm 5

首先开放Info.plist源代码(右击单击文件Info.plis,然后选择源代码),并添加这样的:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>googlechromes</string>
    <string>comgooglemaps</string>
</array>
Run Code Online (Sandbox Code Playgroud)

接下来可以更轻松地打开Goog​​le地图,创建如下功能:

func openGoogleMaps() {
    if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
        UIApplication.shared.openURL(URL(string:
            "comgooglemaps://?center=\(myLatitude),\(myLongitude)&zoom=14&views=traffic")!)
    } else {
        print("Can't use comgooglemaps://")
    }
}
Run Code Online (Sandbox Code Playgroud)

每当您想要使用给定坐标打开Goog​​le地图时,只需调用该函数即可openGoogleMaps().

有关更多信息,请查看地图网址适用于iOS的Maps SDK


Sri*_*mar 5

if UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
        UIApplication.shared.open(URL(string: "comgooglemaps://?center=\(self.latitude),\(self.longitude)")!, options: [:], completionHandler: nil)
    } else {
         print("Opening in Apple Map")

    let coordinate = CLLocationCoordinate2DMake(self.latitude, self.longitude)
    let region = MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(0.01, 0.02))
    let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
    let mapItem = MKMapItem(placemark: placemark)
    let options = [
        MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: region.center),
        MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: region.span)]
    mapItem.name = theLocationName
    mapItem.openInMaps(launchOptions: options)
    }
Run Code Online (Sandbox Code Playgroud)

如果已安装,请使用此代码在谷歌地图中打开,否则在默认的苹果地图中打开。


Bor*_*lic 5

这是Swift 5解决方案,如果未安装 Google Maps 应用程序,您可以确保地图显示在浏览器中,或者如果安装在设备上,则显示在 Google Maps 应用程序中,这两种情况都带有位置图钉.

 if UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
     UIApplication.shared.open(URL(string:"comgooglemaps://?center=\(latitude),\(longitude)&zoom=14&views=traffic&q=\(latitude),\(longitude)")!, options: [:], completionHandler: nil)
 } else {
     UIApplication.shared.open(URL(string: "http://maps.google.com/maps?q=loc:\(latitude),\(longitude)&zoom=14&views=traffic&q=\(latitude),\(longitude)")!, options: [:], completionHandler: nil)
 }
Run Code Online (Sandbox Code Playgroud)