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

前端 未结 9 1788
无人共我
无人共我 2020-12-31 09:26

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 (UIA         


        
相关标签:
9条回答
  • This is much simpler to do in 2019: just use the universal link

    URL(string: "https://www.google.com/maps/search/?api=1&query=\(lat),\(lng)")
    

    See the complete documentation at https://developers.google.com/maps/documentation/urls/guide .

    0 讨论(0)
  • 2020-12-31 09:56

    This simple URL scheme works for me

    URL(string: "comgooglemaps://?q=\(lat),\(lng)")
    

    P.S. But don't forget to add comgooglemaps URL scheme to Info.plist

    0 讨论(0)
  • 2020-12-31 10:01

    You can use this:

    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&q=\(self.location.coordinate.latitude),\(self.location.coordinate.longitude)")!)
    } else {
        print("Can't use comgooglemaps://")
    }
    
    0 讨论(0)
  • 2020-12-31 10:03

    Here is the Swift 5 solution, where you can be sure that the map is shown in the browser if Google Maps app is not installed or in Google Maps app if 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)
     }
    
    0 讨论(0)
  • 2020-12-31 10:06

    First open Info.plist as Source Code (Righ Click on file Info.plis, then select Source Code) and add this:

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>googlechromes</string>
        <string>comgooglemaps</string>
    </array>
    

    Next to make it easier to open Google Maps, create function like this one:

    func openGoogleMaps() {
        if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
            UIApplication.shared.openURL(URL(string:
                "comgooglemaps://?center=\(myLatitude),\(myLongitude)&zoom=14&views=traffic")!)
        } else {
            print("Can't use comgooglemaps://")
        }
    }
    

    Whenever you want to open Google Maps with given coordinate, you just call the function openGoogleMaps().

    For more check Maps URLs and Maps SDK for iOS

    0 讨论(0)
  • 2020-12-31 10:09

    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://")
        }
    }
    
    0 讨论(0)
提交回复
热议问题