0
votes

My custom app has a map on one of the pages. If you click on that map (that is showing the location of a patient) it is supposed to open Google Maps app with the direction from wherever you are to that patient location. For this I use :

http://maps.google.com/maps?saddr=patientlocation&daddr=myaddress [where patientlocation and myaddress are long/lat]

On most phones, it works as expected. It opens the app of Google Maps, with the route loaded.

But for a reason I do not know, some phones will try to open Safari and go to Google Maps website, even though my Google Maps app is installed and up to date. It is a problem because the navigation is not as good when you're not in the actual app. I have tried to restart phone, to uninstall/reinstall my app, uninstall/reinstall Google Maps app, but nothing will do..

I have sent that link through email to some phones. If I click on the link from the native iOS email app, it opens Safari and load Google Maps website. If I click on the link from the Gmail app, it opens Google Maps app.

I am a bit lost on how Apple/iOS handles that.

Can someone help? Thank you!

1

1 Answers

1
votes

First of all make sure that you have added comgooglemaps(mandatory) and googlechromes(optional) in your LSApplicationQueriesSchemes in Info.plist.

enter image description here

Now to open the googlemaps with route loaded, call the below function on your action.

I have taken a dummy latitude, longitude as 28.5239008, 77.0846414 respectively and kept the saddr and daddr as same to it takes the route from current position. You can provide your own saddr and daddr as per your requirement.

func openGoogleMaps() {
    if UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
        UIApplication.shared.open(URL(string:
            "comgooglemaps://?saddr=&daddr=28.5239008,77.0846414&zoom=14&directionsmode=driving")!, options: [:]) { (completed:Bool) in
                print("Opened googlemaps")
        }
    } else {
        print("Can't use google maps. Opening browser");
        if UIApplication.shared.canOpenURL(URL(string: "https://maps.google.com/?saddr=&daddr=28.5239008,77.0846414")!) {
            UIApplication.shared.open(URL(string: "https://maps.google.com/?saddr=&daddr=28.5239008,77.0846414")!, options: [:], completionHandler: nil)
        }
    }
}

If GoogleMaps app is not present in your device, it will redirect to browser. If you want a route loaded in your browser too, then provide permission of your location to your browser to when prompted.