/home/adeel

Programmatically organising your backpacking trip using Google My Maps

This blog post has been converted from a presentation I gave during the Thematic CERN School of Computing 2019.

When planning a journey to a new country or a city it helps to mark down all the places you would like to visit and eventually create a travel plan for each day. I personally use Google Maps for finding places of interest including historical buildings, museums, and libraries. As an example, if I was to visit say Split, Croatia I could search for “places to visit split” on Google Maps. It will then list all the attractions based on features such as reviews and popularity.

Things to do in Split

Although it is possible to individually “Save” each place in Google Maps, it does not allow customisability options including categorisation of places and marking places based on personal interests.

Google My Maps

Google My Maps is a tool which allows creating custom maps and is useful for planning trips, marking hiking routes, and other sports activities. It also allows sharing the map either publicly or privately. Some of its features include:

  • Updating the base map to satellite or street view
  • Personalising the map by styling the icons and changing their color
  • Importing data from a spreadsheet including CSV, XML, and KML files

Adding a new entry

The manual process for adding a new entry involves finding a location using the search bar and then adding it to the map layer which places a waypoint icon on the base map.

Adding entry

A layer is Google My Map’s way of categorising different topics, for example, there can be a separate layer for different cities.

This, however, is a tedious and repetitive task, which can be done programmatically using Python.

Adding entries using Google Maps API

In principal, if we have a spreadsheet with a list of places, the only task left is to import these places into our custom map. Thus we require a way of automating the process of creating this spreadsheet.

The Google Maps API provides an elegant way of querying the data using their Places API. For example, to get a list of places to visit in Split, the request can be formatted as:

https://maps.googleapis.com/maps/api/place/textsearch/json?query=things+to+do+split&language=en&key=API_KEY

This returns a JSON object containing a list of places. Each record contains the place name, its latitude, longitude, address, icon, etc.

The code block below sends a GET request to the server, and then performs four major steps including getting the result object, enumerating through the places list and then writing the appropriate entries to a CSV file.

# Fetch the data.
places = requests.get(f'https://maps.googleapis.com/maps/api/place/textsearch/json?query={args.query}&language=en&key=API_KEY')

# Convert the response to a JSON object.
places = json.loads(places.text)['results']

# Note: in this case we only keep the these three columns, but we can further populate the CSV file with the place description, its rating, and so on...
columns = ['names', 'coordinates', 'icon']

# Write the data to a CSV file.
with open(f'places/{query}.csv', 'w') as out_file:
    writer = csv.writer(out_file, delimiter=',')
    writer.writerow(columns)
    for place in places:
        name = place['name']
        icon = place['icon']
        lat, lng = place['geometry']['location']['lat'], place['geometry']['location']['lng']
        data = [name, (lat, lng), icon]
        print(f'{static} -> {data}')
        writer.writerow(data)

Invoking the script

To get a list of all the places to visit in Split:

$ python fetch-google-maps.py "things to do split"

This will generate a CSV file called Things To Do Split.csv in the places sub-directory.

Importing the file

Google My Maps supports populates the map from CSV, XML, KML, and GPL files. Instead of using coordinates (as we do in this case) we can also use street addresses.

To import the file, create a new layer in your custom map and use the “Import” utility. A window will pop up prompting for the file upload. Note that you will need to guide Google My Maps by marking the columns that correspond to the place name and its coordinates.

Coordinates Title

Note: By default, if there are more than 10 items in the CSV file Google My Maps will group them all under a single entry. To view each item separately select the “Uniform style” option and update “group places by” dropdown to “Individual styles”.

Limitations

Although our map is now populated with the landmark icons at their correct positions, they are not yet styled and all have the same icon. They are also not color coded which makes it difficult to keep track of all the places to visit on a particular day (changing the icon also allows quick identification, e.g. a museum or library should be clearly distinguishable). A fully styled map may look like:

Map simple Map styled

I could not find a way to embed this information within the CSV file, so this step still has to be done manually. Another limitation with Google My Maps is that it is not possible to import multiple CSV files into a single layer (thus requiring us to merge the files before importing them).

A styled map from one of my recent trips can be seen here.

References

Source code: https://gist.github.com/adl1995/a66fa3532364ff87aaaec72be332abe7

Blog: https://www.google.com/earth/outreach/learn/visualize-your-data-on-a-custom-map-using-google-my-maps