Opening Google maps

Open Google maps

Search location and find route using Python script

This is a simple script to open Google maps using Python script.

I prefer this using code editor than Jupyter, as we can pass command-line arguments to the script.

Below is the script

In [ ]:
import webbrowser, sys, pyperclip, os, signal

if len(sys.argv) > 1:
    address = ''.join(sys.argv[1:])

else:
    address = pyperclip.paste()

google_url = 'https://www.google.co.in/maps/'


url = "{}{}/{}".format(google_url, 'dir' if '/' in address else 'place', address)

webbrowser.open(url)


os.kill(os.getppid(), signal.SIGHUP)  #this closes the terminal after running the script, comment this if you dont want that way

Say, you save this script as 'open_google.py'

For running this, 'python open_google.py'

This 'open_google.py' is now read as a list of strings.

We can make use of this and pass location names to the script.

Like 'python open_google.py Hyderabad Bengaluru'

This is read as ['open_google.py', 'Hyderabad', 'Bengaluru']

Above script reads this and use them for showing in the Google maps.

I used a small if condition in the format script, so if you use '/' in the command-line it tries to find the route between these two locations or else just the location of it in Google maps.

Example for location : python open_google.py hyderabad

Example for route : python open_google.py hyderabad/bengaluru

Don't worry about the spacing, the script don't care.

Something particularly we want to search use comma. Like 'python open_google.py jubilee hills, hyderabad / koramangala, bengaluru'

If you don't use any location in command-line, then it takes the text in the clipboard.

Styling cell, ignore this

In [1]:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))