Google Maps SDK 无法打开图钉
Google Maps SDK won't open pin
我的应用程序中有一个 google 地图视图,其中显示了一个图钉。
func setUpGoogleMapView() {
let camera = GMSCameraPosition.camera(withLatitude: self.site.latitude, longitude: self.site.longitude, zoom: 15.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.frame = googleMapView.frame
self.view.addSubview(mapView)
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: self.site.latitude, longitude: self.site.longitude)
marker.title = self.site.name
marker.map = mapView
}
如果我手动调用 UIApplication.shared.openURL
并在查询中传入 URL,它将以不同的模式打开(注释不显示地点名称,而是显示纬度和经度) 或者如果用户没有安装 google 地图则不会打开。
@IBAction func directionsButtonPressed(_ sender: UIButton) {
if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
UIApplication.shared.openURL(URL(string:
"comgooglemaps://?center=\(self.site.latitude),\(self.site.longitude)&zoom=14&views=traffic&q=\(self.site.latitude),\(self.site.longitude)")!)
} else {
print("Can't use comgooglemaps://");
}
}
如何使用 UIApplication.shared.openURL
在 Safari 中打开 google 地图?
如果我调用 UIApplication.shared.openURL
,我应该如何让 google 地图显示带有地点名称的图钉?
我想在使用 UIApplication.shared.openURL
后使用相同的路线模式。
screenshot
Swift 3.x:
let strPlaceName = "batumi+botanical+garden" as String!
let url = URL(string: "https://www.google.com/maps?q="+strPlaceName!)
if UIApplication.shared.canOpenURL(url!) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
} else {
// for iOS 10, below is deprecated
UIApplication.shared.openURL(url!)
}
}
Objective-C:
NSString *strPlaceName = @"Batumi+Botanical+Garden";
NSString *str = [NSString stringWithFormat:@"https://www.google.com/maps/place/%@",strPlaceName];
NSURL *url = [NSURL URLWithString:str];
if([[UIApplication sharedApplication] canOpenURL:url]) {
if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
} else {
// for iOS 10, below is deprecated
[[UIApplication sharedApplication] openURL:url];
}
}
我的应用程序中有一个 google 地图视图,其中显示了一个图钉。
func setUpGoogleMapView() {
let camera = GMSCameraPosition.camera(withLatitude: self.site.latitude, longitude: self.site.longitude, zoom: 15.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.frame = googleMapView.frame
self.view.addSubview(mapView)
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: self.site.latitude, longitude: self.site.longitude)
marker.title = self.site.name
marker.map = mapView
}
如果我手动调用 UIApplication.shared.openURL
并在查询中传入 URL,它将以不同的模式打开(注释不显示地点名称,而是显示纬度和经度) 或者如果用户没有安装 google 地图则不会打开。
@IBAction func directionsButtonPressed(_ sender: UIButton) {
if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
UIApplication.shared.openURL(URL(string:
"comgooglemaps://?center=\(self.site.latitude),\(self.site.longitude)&zoom=14&views=traffic&q=\(self.site.latitude),\(self.site.longitude)")!)
} else {
print("Can't use comgooglemaps://");
}
}
如何使用 UIApplication.shared.openURL
在 Safari 中打开 google 地图?
如果我调用 UIApplication.shared.openURL
,我应该如何让 google 地图显示带有地点名称的图钉?
我想在使用 UIApplication.shared.openURL
后使用相同的路线模式。
screenshot
Swift 3.x:
let strPlaceName = "batumi+botanical+garden" as String!
let url = URL(string: "https://www.google.com/maps?q="+strPlaceName!)
if UIApplication.shared.canOpenURL(url!) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
} else {
// for iOS 10, below is deprecated
UIApplication.shared.openURL(url!)
}
}
Objective-C:
NSString *strPlaceName = @"Batumi+Botanical+Garden";
NSString *str = [NSString stringWithFormat:@"https://www.google.com/maps/place/%@",strPlaceName];
NSURL *url = [NSURL URLWithString:str];
if([[UIApplication sharedApplication] canOpenURL:url]) {
if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
} else {
// for iOS 10, below is deprecated
[[UIApplication sharedApplication] openURL:url];
}
}