Use locationsharinglib to Get Location from Google Maps

Updated: Jun 19

Share your location with a secondary account

First, set up a secondary Google account and use Google's location sharing feature to share your location with that secondary account. Since you'll be putting your password in plaintext in this file, using a secondary account provides you with a little bit of extra security.

Install locationsharinglib and use get-maps-cookies

Next, you'll need to install locationsharinglib. The documentation is here. Through locationsharinglib you will be able to get the coordinates of your location that is accessible on Google Location Sharing.

$ pip install locationsharinglib

After installation, you'll have to use the get-maps-cookies command line tool to get a cookies file to use in your script. I had a lot of trouble with this part, so if you're smarter than me skip this next section!

I had to install a virtual environment (which I'm not super familiar with):

Install virtual environments with Python3:

$ sudo apt-get install python3-venv

Make a directory where you will keep your environments:

$ mkdir enviros && cd enviros

Make a virtual environment in this directory:

$ python3 -m venv env

Activate the environment:

$ source enviros/env/bin/activate

Install locationsharinglib in the virtual environment (the point of virtual environments is that you can't access packages installed in your regular set-up)

$ pip install locationsharinglib

Verify that a file path prints in response to this:

$ which get-maps-cookies

Go back to your home directory:

$ cd

Use the get-maps-cookies tool to make the cookies file:

$ get-maps-cookies -e "" -p "supersecretpassword" -c "cookies.txt"

Verify that the cookies file shows up:

$ ls

Exit the virtual environment:

$ deactivate

Install reverse_geocoder

Locationsharinglib just gives you a set of coordinates. This doesn't help me too much, since I don't know where a coordinate is off the top of my head.

The most precise way to translate a coordinate into a location is to use Google Map's API to turn it into an address. However, they only give you a certain number of free API calls per day and I have other priorities with my free Google API calls.

But bigger than that, there are two other reasons I decided not to use Google.

1. My end goal is to only display a city and state (or country) name. Google's addresses are inconsistently formatted and it's difficult to the name you want.

2. I wanted to be able to customize the names.

The best solution I could find is reverse_geocoder. Documentation can be found here.

Reverse_geocoder uses a csv file that lists the location name and coordinates. The default file has a one-mile resolution, but you can add as many points as you want to manually if you, for example, would like to make your hometown include places that are close together like "home", "work", or "school". It uses a distance formula to find the point in the file that has the smallest distance from your actual coordinates.

Install with:

$ pip3 install reverse_geocoder

Potential problems installing reverse_geocoder

I had issues installing each time. The problem is the scipy package that is installed with it. If you're also having problems, make sure you install scipy first. See this documentation for instructions specific to your system.

With a Raspberry Pi, running Python 3 I installed scipy (and all its dependencies) using:

$ python3 -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose

Save your location for later use

I have a separate script that displays the location so I opted to pass it along to that script by saving the location in a file.

Full code

View it on Dropbox here