【问题标题】:Google Maps SDK won't open pinGoogle Maps SDK 不会打开 pin
【发布时间】:2017-08-17 23:36:26
【问题描述】:

我的应用程序中有一个谷歌地图视图,其中显示了一个图钉。

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,它将以不同的模式打开(注释不显示地点的名称,而是显示纬度和经度)或者如果用户尚未安装谷歌地图。

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

如何在 Safari 中使用 UIApplication.shared.openURL 打开谷歌地图?

如果我打电话给UIApplication.shared.openURL,我应该如何让谷歌地图显示带有地点名称的图钉?

我想在使用UIApplication.shared.openURL后有相同的方向模式。

screenshot

【问题讨论】:

  • 您可以发布您正在寻找的所需输出的屏幕截图吗?
  • 谢谢。它不允许我添加图片,所以我添加了一个链接
  • 您显示的屏幕截图是您要查找的输出?
  • 是的,这是我正在寻找的输出
  • 你有你想在谷歌地图上显示的地方的经纬度吗?

标签: ios google-maps-sdk-ios


【解决方案1】:

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

目标-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];
    }
}

【讨论】:

  • 非常感谢!这真的很好用!不过我不能投票,它说我还没有足够的声誉:) @Hooda
  • @NatukaKh 很高兴知道它有效!您需要 15 声望才能投票。如果您愿意,可以稍后在您有足够的声望时投票。
  • 会的 :) 再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-26
  • 2011-12-09
  • 2016-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多