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

Zizoo picture Zizoo · Apr 7, 2017 · Viewed 16.2k times · Source

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 ?

Answer

RajeshKumar R picture RajeshKumar R · Apr 7, 2017

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