Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 337
Zizoo Avatar asked Apr 07 '17 11:04

Zizoo


People also ask

How do I use dropped pin with Google Maps?

Either search for an address or scroll around the map until you find the location you want. Long-press on the screen to drop a pin. The address or location will pop up at the bottom of the screen. Tap on the location to share it, save it, add a label to it, or get directions.

How do I open a Google map with Swift?

Create the function. Create a new Swift file from Xcode and call it OpenMapDirections, then copy/paste the following code in it: We will use the above code to present an alert controller with 2 actions: one for opening Google Maps, the other to open Apple Maps. Here we pass the coordinate for both directly.


1 Answers

Here is the Swift 5 solution, where you can be sure that the map is shown in the browser if the Google Maps app is not installed or in the Google Maps app if it is installed on the device, both cases with the pin of the location.

 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)
 }
like image 168
Boris Nikolic Avatar answered Sep 27 '22 00:09

Boris Nikolic