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条回答
我想做一个坏孩纸
2楼-- · 2020-02-09 10:39

You should add this line in info.Plist file

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

code here:

if dicDetails.hasValueForKey("latitude") && dicDetails.hasValueForKey("latitude") {
        if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(URL(string:"comgooglemaps://?center=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude"))&zoom=14&views=traffic&q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude"))")!, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil)
            } else {
                // Fallback on earlier versions
                 //https://www.google.com/maps/@
                UIApplication.shared.openURL(URL(string:"https://www.google.com/?q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude")),15z")!)
            }
        } else {
            //print("Can't use comgooglemaps://")
            UIApplication.shared.openURL(URL(string:"https://www.google.com/?q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude")),15z")!)
        }
    }
查看更多
我想做一个坏孩纸
3楼-- · 2020-02-09 10:40

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

查看更多
forever°为你锁心
4楼-- · 2020-02-09 10:40

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 .

Lonely孤独者°
5楼-- · 2020-02-09 10:41

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://")
}
查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-02-09 10:42

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)
 }
查看更多
做自己的国王
7楼-- · 2020-02-09 10:47
if UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
        UIApplication.shared.open(URL(string: "comgooglemaps://?center=\(self.latitude),\(self.longitude)")!, options: [:], completionHandler: nil)
    } else {
         print("Opening in Apple Map")

    let coordinate = CLLocationCoordinate2DMake(self.latitude, self.longitude)
    let region = MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(0.01, 0.02))
    let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
    let mapItem = MKMapItem(placemark: placemark)
    let options = [
        MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: region.center),
        MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: region.span)]
    mapItem.name = theLocationName
    mapItem.openInMaps(launchOptions: options)
    }

Use this code for opening in google map if it's installed else open in default apple map.

查看更多
登录 后发表回答