Retrieve geocoordinates of an address using Google Map API and Here API

November 25, 2020 read
Retrieve geocoordinates of an address using Google Map API and Here API
Location Intelligence
Web Development
Computer Programming
Computer Science

You may sometimes want to place a marker on a map to visually indicate the location of a place or visualize a list of addresses on a map in your application. To do this, you may need to first convert the addresses into geolocation coordinates in order to pin and connect them on the map.

In this article, I will show you how to do this conversion, referred to as forward geocoding, with both HERE and Google maps APIs in Python.

Geocoding with HERE Geocode API

First, you need to create a free account with HERE at HERE Developer Platform and then generate a REST API app. Afterwards, create and copy an API key as shown in the picture below:

Now you can use the code below to get the coordinates of any address:

import requests

url = "https://geocode.search.hereapi.com/v1/geocode?apiKey=<your api key>&q=<address>"

response = requests.request("GET", url)

print(response.json())

Replace <your api key> and <address> with the right values. 

For example, getting the geo-coordinates of this address (Damrak 56, 1012 LL, Amsterdam, The Netherlands) will return a result as shown below:

{
    "items": [
        {
            "title": "Damrak 56, 1012 LL Amsterdam, Nederland",
            "id": "here:af:streetsection:AErBxkYdHlIuMz4ExeSRwA:EAIaAjU2KGQ",
            "resultType": "houseNumber",
            "houseNumberType": "interpolated",
            "address": {
                "label": "Damrak 56, 1012 LL Amsterdam, Nederland",
                "countryCode": "NLD",
                "countryName": "Nederland",
                "stateCode": "NH",
                "state": "Noord-Holland",
                "county": "Amsterdam",
                "city": "Amsterdam",
                "district": "Amsterdam-Centrum",
                "street": "Damrak",
                "postalCode": "1012 LL",
                "houseNumber": "56"
            },
            "position": {
                "lat": 52.37528,
                "lng": 4.89546
            },
            "access": [
                {
                    "lat": 52.37519,
                    "lng": 4.89564
                }
            ],
            "mapView": {
                "west": 4.89399,
                "south": 52.37438,
                "east": 4.89693,
                "north": 52.37618
            },
            "scoring": {
                "queryScore": 0.92,
                "fieldScore": {
                    "country": 1.0,
                    "city": 1.0,
                    "streets": [
                        1.0
                    ],
                    "houseNumber": 1.0,
                    "postalCode": 1.0
                }
            }
        }
    ]
}

where position key contains the latitude and longitude of the address.

Geocoding with Google Maps API

To begin using Google's APIs, you need a Google Cloud Platform (GCP) account setup with billing. Then, you have to enable Geocoding API, generate credentials and create an API key. This link below explains the steps you have to take: https://developers.google.com/maps/documentation/javascript/get-api-key

After this, you can in a similar manner as above fetch or get the geo-coordinates of an address with the API key.

import requests

url = "https://maps.googleapis.com/maps/api/geocode/json?address=<address>&key=<api key>"

response = requests.request("GET", url)

print(response.json())

A sample JSON output from geocoding “Damrak 56, 1012 LL, Amsterdam, The Netherlands” is as shown below:

{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "56",
                    "short_name": "56",
                    "types": [
                        "street_number"
                    ]
                },
                {
                    "long_name": "Damrak",
                    "short_name": "Damrak",
                    "types": [
                        "route"
                    ]
                },
                {
                    "long_name": "Amsterdam-Centrum",
                    "short_name": "Amsterdam-Centrum",
                    "types": [
                        "political",
                        "sublocality",
                        "sublocality_level_1"
                    ]
                },
                {
                    "long_name": "Amsterdam",
                    "short_name": "Amsterdam",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Amsterdam",
                    "short_name": "Amsterdam",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "Noord-Holland",
                    "short_name": "NH",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "Netherlands",
                    "short_name": "NL",
                    "types": [
                        "country",
                        "political"
                    ]
                },
                {
                    "long_name": "1012 LL",
                    "short_name": "1012 LL",
                    "types": [
                        "postal_code"
                    ]
                }
            ],
            "formatted_address": "Damrak 56, 1012 LL Amsterdam, Netherlands",
            "geometry": {
                "location": {
                    "lat": 52.3750586,
                    "lng": 4.895734099999999
                },
                "location_type": "RANGE_INTERPOLATED",
                "viewport": {
                    "northeast": {
                        "lat": 52.37640758029151,
                        "lng": 4.897083080291502
                    },
                    "southwest": {
                        "lat": 52.37370961970851,
                        "lng": 4.894385119708497
                    }
                }
            },
            "place_id": "EilEYW1yYWsgNTYsIDEwMTIgTEwgQW1zdGVyZGFtLCBOZXRoZXJsYW5kcyIaEhgKFAoSCdvctofHCcZHEc_1HfxuOJOgEDg",
            "types": [
                "street_address"
            ]
        }
    ],
    "status": "OK"
}

where the latitude and longitude of the address is in the location key of the JSON output. 

Note also how the API returns other relevant information about the address. Let me know in the comments section if you have any questions or comments.

User profile image

Created by

Evans Boateng Owusu

Evans is a Computer Engineer and cloud technology enthusiast. He has a Masters degree in Embedded Systems (focusing on Software design) from the Technical University of Eindhoven (The Netherlands) and a Bachelor of Science in Electronic and Computer Engineering from the Polytechnic University of Turin (Italy). In addition, he has worked for the high-tech industry in the the Netherlands and other large corporations for over seven years.


© Copyright 2024, The BoesK Partnership