Skip to main content
Engineering LibreTexts

13.6: Google geocoding web service

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Google has an excellent web service that allows us to make use of their large database of geographic information. We can submit a geographical search string like "Ann Arbor, MI" to their geocoding API and have Google return its best guess as to where on a map we might find our search string and tell us about the landmarks nearby.

    The geocoding service is free but rate limited so you cannot make unlimited use of the API in a commercial application. But if you have some survey data where an end user has entered a location in a free-format input box, you can use this API to clean up your data quite nicely.

    When you are using a free API like Google's geocoding API, you need to be respectful in your use of these resources. If too many people abuse the service, Google might drop or significantly curtail its free service.

    You can read the online documentation for this service, but it is quite simple and you can even test it using a browser by typing the following URL into your browser:

    Make sure to unwrap the URL and remove any spaces from the URL before pasting it into your browser.

    The following is a simple application to prompt the user for a search string, call the Google geocoding API, and extract information from the returned JSON.

    Code 13.6.1 (Python)
    import urllib.request, urllib.parse, urllib.error
    import json
    # Note that Google is increasingly requiring keys
    # for this API
    serviceurl = ''
    while True:
        address = input('Enter location: ')
        if len(address) < 1: break
        url = serviceurl + urllib.parse.urlencode(
            {'address': address})
        print('Retrieving', url)
        uh = urllib.request.urlopen(url)
        data =
        print('Retrieved', len(data), 'characters')
            js = json.loads(data)
            js = None
        if not js or 'status' not in js or js['status'] != 'OK':
            print('==== Failure To Retrieve ====')
        print(json.dumps(js, indent=4))
        lat = js[\"results\"][0][\"geometry\"][\"location\"][\"lat\"]
        lng = js[\"results\"][0][\"geometry\"][\"location\"][\"lng\"]
        print('lat', lat, 'lng', lng)
        location = js['results'][0]['formatted_address']
    # Code:

    The program takes the search string and constructs a URL with the search string as a properly encoded parameter and then uses urllib to retrieve the text from the Google geocoding API. Unlike a fixed web page, the data we get depends on the parameters we send and the geographical data stored in Google's servers.

    Once we retrieve the JSON data, we parse it with the json library and do a few checks to make sure that we received good data, then extract the information that we are looking for.

    The output of the program is as follows (some of the returned JSON has been removed):

    $ python3
    Enter location: Ann Arbor, MI
    Retrieved 1669 characters
        "status": "OK",
        "results": [
                "geometry": {
                    "location_type": "APPROXIMATE",
                    "location": {
                        "lat": 42.2808256,
                        "lng": -83.7430378
                "address_components": [
                        "long_name": "Ann Arbor",
                        "types": [
                        "short_name": "Ann Arbor"
                "formatted_address": "Ann Arbor, MI, USA",
                "types": [
    lat 42.2808256 lng -83.7430378
    Ann Arbor, MI, USA
    Enter location:

    You can download to explore the XML variant of the Google geocoding API.

    This page titled 13.6: Google geocoding web service is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Chuck Severance via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.