Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Trouble launching Google Maps at a specified lat/lon
-
I'm using the below code to launch Google Maps with the intent that the map be centered on the specified lat/lon.
import clipboard import webbrowser s = str(lat) + ',' + str(lon) clipboard.set(s) webbrowser.open('comgooglemaps://?center=' + s)
Google Maps opens, but just shows the current location. I then tap in the search field and paste and proceed with the search and it goes to the right location. So the clipboard contents validate that s does contain the correct lat/lon but for some reason Google Maps isn't responding to the URL as specified and going to that location.
Anyone else have trouble doing something similar? Would I have better luck just directing to the default Apple maps app (assuming it supports the same handle URL syntax)?
-
s = str(lat) + '%2C' + str(lon)
For more Pythonic solutions, see urllib.quote() and urllib.encode().
-
I tried the %2C instead of the comma yet it still just opens to the current location.
I tried urllib.quote on the whole URL but it also replaced other parts like the : etc which prevented google maps from launching.
Any idea what I'm doing wrong?
-
I'm guessing this could be a bug in the Google Maps app.
I've noticed that it works reliably (for me) when the app is already running in the background, but fails when it's not been launched yet. Not sure if there's a workaround, I've tried adding explicit
zoom
andmapmode
parameters, but that doesn't seem to help. -
Do you know if the Apple maps app has URL support? Maybe using that would be an alternative.
-
It does. Try something like this:
# ... webbrowser.open('safari-http://maps.apple.com/?ll=' + s)
(Note: the
safari-
prefix is Pythonista-specific, it basically prevents that the URL is opened in the in-app browser, which would otherwise be the default forhttp:
.) -
Apple Maps app URL scheme: https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html
I observed the following with the Google Maps iOS URL scheme (https://developers.google.com/maps/documentation/ios/urlscheme ):
- As omz pointed out, there is different behavior on first launch vs. when the Google map app is already running.
- One possible workaround for this bug in their app is to first call a URL with a comgooglemaps-x-callback:// parameter to ensure their app is launched before sending out a second URL on callback to do the actual work that you want to achieve.
- Calling urllib.quote() on the entire URL is not the way to go. Just encode the parameters that are causing grief. I had success with:
map_url = 'comgooglemaps://?center=' + urllib.quote('40.765819,-73.975866')
- I got better results using urllib.urlencode() rather than urllib.quote() because it does not encode the equals signs (=):
params_dict = { 'q' : 'starbucks', 'center' : '{latitude},{longitude}'.format(**getLocation()), 'directionsmode' : 'walking', 'zoom' : '14' } map_url = 'comgooglemaps://?' + urllib.urlencode(params_dict) print(map_url) webbrowser.open(map_url)
- As omz pointed out, there is different behavior on first launch vs. when the Google map app is already running.
-
Thanks for the replies. ccc, did you find using urlencode worked reliably or did you have to combine that with the callback approach? I'm assuming with the callback approach you'd launch Google Maps and it would then in turn launch your script again so you'd have to save off the lat/lon and then bounce it back with that info. But if it works....
I tried using the Apple maps app and that seems to work every time without even worrying about encoding the URL. My leaning would be to Google Maps but I guess the other advantage to using Apple maps is that you're guaranteed to have it installed on the device.