Looking Up Zipcodes With Google Geolocation API + Emacs Trick

I got an email from my advisor with the subject: “Location Magic?”

She wrote, “If I give you a lists of latitude and longitudes, are you able to determine the state or the (large) geographic region?”

This is the quick and dirty way I created to find city and state locations via their latitude and longitude values. I thought this was super interesting to think about.

The Problem

We needed to divvy up responses to North, South, East, West of the United States, but the Qualtrics output only gave the IP addresses of the survey participants.

Obviously we weren’t going to do this manually or via a lookup table.

My Solution

I tried a reverse lookup with the Google Geolocation API.

Google Geolocation

1) Here is the source to get started: https://developers.google.com/maps/documentation/geocoding/start

2) Scroll down until you get here; follow the instructions.

3) Then use the URL with the API key they gave you.

It’ll look something like this: https://maps.googleapis.com/maps/api/geocode/json?latlng=numberhere,-numberhere&key=blahblahblahsecretkey

4) Voila, the output will look something like this:

"long_name" : "Ohio",
               "short_name" : "OH",
               "types" : [ "administrative_area_level_1", "political" ]

Emacs Magic Step

1) F3 C-y C-X F4 (copied the address X times for X number of responses)

2) Kill & yank-rectangle from the source to the copied addresses

3) Your reverse lookup URLs are ready!


There’s definitely more efficient ways to extract the queries or cobble together the URLs. This was just for fun though.

I was able to consider the API approach because I’d participated in a hackathon where we harnessed the Fitbit API to scrape heart rate data for our mobile web app. I also recently built a Tweeting chatbot that used the Twitter API via my Raspberry Pi.

I doubt I would have thought of looking into APIs had I not been exposed to those things before.

So – getting on my soapbox here – if you’re a student trying to figure out your paths and interests, do consider participating in hackathons or going to conferences outside your field.