How to open google maps app with a dropped pin ? -

2020-02-09 10:07发布

I have this code to open Google Maps App with specific location and it's working but what I need is to drop a pin on that location, is it possible ?

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

How to to do this ?

8条回答
可以哭但决不认输i
2楼-- · 2020-02-09 10:57

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)
}
查看更多
Fickle 薄情
3楼-- · 2020-02-09 10:58

q: The query string for your search.

It creates a marker in given coordinates. Try this

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://")
    }
}
查看更多
登录 后发表回答