HOME All Posts My Etsy Follow me on Mastodon for updates

How to Use locationsharinglib to Get Location from Google Maps


Published January 15th, 2019 by Emily

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 "email@address.com" -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 Python3 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

 
import locationsharinglib
from locationsharinglib import Service

import io
import reverse_geocoder as rg

import json
import datetime

##############################################################################
############################ PULL LOCS #######################################
##############################################################################

email = 'MY EMAIL'
password = 'MY PASSWORD'
cookiesfile = 'PATH TO COOKIES FILE'
fullname = 'MY FULL NAME'

service = Service(email, password, cookies_file=cookiesfile)
person = service.get_person_by_full_name(fullname)
person_coords = service.get_coordinates_by_full_name(fullname)

##############################################################################
############################ DECODE COORDS ###################################
##############################################################################

coords_db_filepath = 'custom_data_source_db.csv'
geo = rg.RGeocoder(
    mode=2, 
    verbose=True, 
    stream=io.StringIO(
        open(
            coords_db_filepath, 
            encoding='utf-8'
            ).read()
        )
    )
coords_edit = person_coords,(51.5214588, -0.1729636)
# the previous line is a workaround to a weird bug I experienced where I couldn't
# get my coords to transfer. I need to fix this in the future.
person_location = geo.query(coords_edit)[0]['name']
person_location_state = geo.query(coords_edit)[0]['admin1']

##############################################################################
############################ SAVE COORDS #####################################
##############################################################################

now = datetime.datetime.today().strftime("%Y-%m-%d %H:%M")

json_export = {
    'time':now,
    'city':person_location,
    'state':person_location_state,
    'coords':str(person_coords)
    }
export_file_path = '/home/pi/current_locs.json'
with open(export_file_path,'w') as outfile:
    json.dump(json_export,outfile)