Geolocation with Google API’s & Python – mapping addresses to GPS coordinates

Google does some pretty impressive things – not just all the web-based search stuff, but they also have lot’s and lot’s of really cool API’s for programmers to peruse.

In order to access these API’s, you need a personal authorization key, which you can create at Google Developer Console, which in most cases will not be for free. (google it! 🙂

Anyhow, below a short Python example on how to map addresses – some of them being very vague! – to GPS coordinates. Considering that Google’s Geolocation services covers the entire world, it’s even more impressive that it can figure out the exact position of an address as vaque as for instance “Globen” below…!

import requests

key='your-google-api-key'


def map_addr_to_gps(address):
    
    payload = {'address':address,'key':key}
    url = 'https://maps.googleapis.com/maps/api/geocode/json'

    r = requests.get(url,payload)
    result = r.json()

    result = result['results']

    for res in result:
        lat = res['geometry']['location']['lat']
        lon = res['geometry']['location']['lng']
    return (lat,lon)


addr1 = 'arlanda t5'
addr2 = 'stadshuset stockholm'
addr3 = 'Globen'

addresses = [addr1,addr2,addr3]

for a in addresses:
    gps = map_addr_to_gps(a)

    print (gps[0],gps[1])

 

About swdevperestroika

High tech industry veteran, avid hacker reluctantly transformed to mgmt consultant.
This entry was posted in API, Data Analytics, Python, Web and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s