omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    3
    8
    4942
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • bitbucket158
      bitbucket158 last edited by

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

      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        s = str(lat) + '%2C' + str(lon)

        For more Pythonic solutions, see urllib.quote() and urllib.encode().

        1 Reply Last reply Reply Quote 0
        • bitbucket158
          bitbucket158 last edited by

          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?

          1 Reply Last reply Reply Quote 0
          • omz
            omz last edited by

            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 and mapmode parameters, but that doesn't seem to help.

            1 Reply Last reply Reply Quote 0
            • bitbucket158
              bitbucket158 last edited by

              Do you know if the Apple maps app has URL support? Maybe using that would be an alternative.

              1 Reply Last reply Reply Quote 0
              • omz
                omz last edited by

                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 for http:.)

                1 Reply Last reply Reply Quote 0
                • ccc
                  ccc last edited by

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

                  1. 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.
                  2. 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')
                  3. 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)
                  
                  1 Reply Last reply Reply Quote 0
                  • bitbucket158
                    bitbucket158 last edited by

                    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.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post
                    Powered by NodeBB Forums | Contributors