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

白昼怎懂夜的黑 提交于 2019-11-29 04:23:19

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

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

Srijan Kumar
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.

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)
}

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

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")!)
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!