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

问题描述 投票:5回答:6

我有这个代码打开具有特定位置的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://");
            }

怎么做?

ios swift google-maps google-maps-sdk-ios
6个回答
21
投票

q:搜索的查询字符串。

它在给定坐标中创建标记。试试这个

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://")
    }
}

5
投票

首先打开qazxsw poi作为源代码(右键单击文件Info.plis,然后选择源代码)并添加:

Info.plist

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

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

每当您想要使用给定坐标打开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://") } } 函数即可。

有关更多信息,请查看openGoogleMaps()Maps URLs


4
投票
Maps SDK for iOS

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


2
投票

Swift 3,iOS 10

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)
    }

1
投票

你可以用这个:

let lat = 0.0
let lon = 0.0

if UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
    UIApplication.shared.open(URL(string: "comgooglemaps://?center=\(lat),\(lon)&zoom=14&views=traffic&q=loc:\(lat),\(lon)")!, options: [:], completionHandler: nil)
} else {
    print("Can't use comgooglemaps://")
    UIApplication.shared.open(URL(string: "http://maps.google.com/maps?q=loc:\(lat),\(lon)&zoom=14&views=traffic")!, options: [:], completionHandler: nil)
}

0
投票

您应该在info.Plist文件中添加此行

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&q=\(self.location.coordinate.latitude),\(self.location.coordinate.longitude)")!)
} else {
    print("Can't use comgooglemaps://")
}

代码在这里:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>googlechromes</string>
    <string>comgooglemaps</string>
</array>
© www.soinside.com 2019 - 2024. All rights reserved.