如何使用掉落的图钉打开 google 地图应用程序? - Swift
How to open google maps app with a dropped pin ? - Swift
我有此代码可以打开具有特定位置的 Google 地图应用程序,它可以正常工作,但我需要在该位置放置一个图钉,可以吗?
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://");
}
如何做到这一点?
q: The query string for your search.
它在给定的坐标中创建一个标记。试试这个
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://")
}
}
首先打开Info.plist
作为源代码(右击文件Info.plis,然后select源代码)并添加:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>
接下来为了更容易打开 Google 地图,创建这样的函数:
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://")
}
}
每当你想打开具有给定坐标的 Google 地图时,你只需调用函数 openGoogleMaps()
.
更多检查Maps URLs and Maps SDK for iOS
Swift 3 , iOS 10
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)
}
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)
}
如果已安装,请使用此代码在 google 地图中打开,否则在默认苹果地图中打开。
你可以使用这个:
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://")
}
您应该在 info.Plist 文件中添加这一行
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>
代码在这里:
if dicDetails.hasValueForKey("latitude") && dicDetails.hasValueForKey("latitude") {
if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string:"comgooglemaps://?center=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude"))&zoom=14&views=traffic&q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude"))")!, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil)
} else {
// Fallback on earlier versions
//https://www.google.com/maps/@
UIApplication.shared.openURL(URL(string:"https://www.google.com/?q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude")),15z")!)
}
} else {
//print("Can't use comgooglemaps://")
UIApplication.shared.openURL(URL(string:"https://www.google.com/?q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude")),15z")!)
}
}
这是 Swift 5 解决方案,如果 Google 地图应用程序未打开,您可以确保地图在浏览器中显示安装或在 Google 地图应用程序中,如果它安装在设备上,两种情况都带有位置的 pin。
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)
}
这在 2019 年要简单得多:只需使用通用 link
URL(string: "https://www.google.com/maps/search/?api=1&query=\(lat),\(lng)")
请参阅 https://developers.google.com/maps/documentation/urls/guide 中的完整文档。
这个简单的 URL 方案对我有用
URL(string: "comgooglemaps://?q=\(lat),\(lng)")
P.S。但是不要忘记将 comgooglemaps
URL 方案添加到 Info.plist
我有此代码可以打开具有特定位置的 Google 地图应用程序,它可以正常工作,但我需要在该位置放置一个图钉,可以吗?
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://");
}
如何做到这一点?
q: The query string for your search.
它在给定的坐标中创建一个标记。试试这个
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://")
}
}
首先打开Info.plist
作为源代码(右击文件Info.plis,然后select源代码)并添加:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>
接下来为了更容易打开 Google 地图,创建这样的函数:
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://")
}
}
每当你想打开具有给定坐标的 Google 地图时,你只需调用函数 openGoogleMaps()
.
更多检查Maps URLs and Maps SDK for iOS
Swift 3 , iOS 10
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)
}
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)
}
如果已安装,请使用此代码在 google 地图中打开,否则在默认苹果地图中打开。
你可以使用这个:
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://")
}
您应该在 info.Plist 文件中添加这一行
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>
代码在这里:
if dicDetails.hasValueForKey("latitude") && dicDetails.hasValueForKey("latitude") {
if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string:"comgooglemaps://?center=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude"))&zoom=14&views=traffic&q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude"))")!, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil)
} else {
// Fallback on earlier versions
//https://www.google.com/maps/@
UIApplication.shared.openURL(URL(string:"https://www.google.com/?q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude")),15z")!)
}
} else {
//print("Can't use comgooglemaps://")
UIApplication.shared.openURL(URL(string:"https://www.google.com/?q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude")),15z")!)
}
}
这是 Swift 5 解决方案,如果 Google 地图应用程序未打开,您可以确保地图在浏览器中显示安装或在 Google 地图应用程序中,如果它安装在设备上,两种情况都带有位置的 pin。
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)
}
这在 2019 年要简单得多:只需使用通用 link
URL(string: "https://www.google.com/maps/search/?api=1&query=\(lat),\(lng)")
请参阅 https://developers.google.com/maps/documentation/urls/guide 中的完整文档。
这个简单的 URL 方案对我有用
URL(string: "comgooglemaps://?q=\(lat),\(lng)")
P.S。但是不要忘记将 comgooglemaps
URL 方案添加到 Info.plist