How Google’s Geocoding solves Address Validation

For the e-commerce business, it is very important to have a valid and correct address base. Sending packages to incorrect addresses causes significant losses, since packages are not delivered and sent back. In the case of the Kickz online shop, we faced exactly this problem. Thus, we thought about how to improve and force users to enter correct addresses. Learn how we used Google’s Geocoding service to solve this problem.

There are many commercial providers for addresses, however not all of them provide a world-wide address base and the Kickz shop is delivering world-wide. It turned out that the best value for money would be to use Google’s Geocoding web service. Geocoding is the process of converting addresses into geographic coordinates, i.e. latitude and longitude.

However, as the response from Google includes not only the longitude/latitude data but also further address suggestions, this service can also be easily used to present refined/suggested addresses to the user. And that was exactly what we needed. So our basic idea of the service usage was to request exact coordinates and refined addresses (suggestions) for a given address. The coordinates were then used to show the location on the map.

The Geocoding web service is part of the Google Maps API and is offered both as free and as commercial. The basic service is free for 2500 requests per day and does not support HTTPS requests. As Kickz requires HTTPS and more requests per day, we decided to go for the commercial service, which allows of up to 100,000 requests per day and provides HTTPS access with signed web service requests.

Using the Geolocation Web Service

Google’s Geocoding web service is quite simple to use. All you need to do is send a simple HTTP request to the specified address and you will receive a response in either XML or JSON format. The response contains zero, one or even several addresses, depending on the precision of the input. It can be empty if Google cannot locate your address — but based on our experience, this rarely happens.

A JSON response is the obvious choice for JavaScript-based validation on the client-side using (AJAX). XML is more suitable for classic server-side validation. For the Kickz project, we chose the XML format. We validate using the Google web service all over the application, where the user enters a address, i.e. during customer registration and checkout. In these scenarios, the request from the browser is first sent to our server-side application, which calls the Google web service with a certificate-signed request. The request for the input “Frankfurstein ring 105a,München, de, 80000″ looks this:

https://maps-api-ssl.google.com/maps/api/geocode/xml?address=Frankfurstein+ring+105a,M%C3%BCnchen,de, 80000,&sensor=false&client=gme-kickzag&signature=VF930KLrbu98sKKLqIjn4adIoTs=

Google returns the following XML document as a response:

<GeocodeResponse>
    <status>OK</status>
    <result>
        <type>street_address</type>
        <formatted_address>Frankfurter Ring 105, 80807 Munich, Germany</formatted_address>
        <address_component>
            <long_name>105</long_name>
            <short_name>105</short_name>
            <type>street_number</type>
        </address_component>
        <address_component>
            <long_name>Frankfurter Ring</long_name>
            <short_name>Frankfurter Ring</short_name>
            <type>route</type>
        </address_component>
        <address_component>
            <long_name>M&#252nchen</long_name>
            <short_name>M&#252nchen</short_name>
            <type>sublocality</type>
            <type>political</type>
        </address_component>
        <address_component>
            <long_name>Munich</long_name>
            <short_name>Munich</short_name>
            <type>locality</type>
            <type>political</type>
        </address_component>
        <address_component>
            <long_name>Munich</long_name>
            <short_name>M</short_name>
            <type>administrative_area_level_2</type>
            <type>political</type>
        </address_component>
        <address_component>
            <long_name>Bayern</long_name>
            <short_name>BY</short_name>
            <type>administrative_area_level_1</type>
            <type>political</type>
        </address_component>
        <address_component>
            <long_name>Germany</long_name>
            <short_name>DE</short_name>
            <type>country</type>
            <type>political</type>
        </address_component>
        <address_component>
            <long_name>80807</long_name>
            <short_name>80807</short_name>
            <type>postal_code</type>
        </address_component>
        <geometry>
            <location>
                <lat>48.1883675</lat>
                <lng>11.5857053</lng>
            </location>
            <location_type>ROOFTOP</location_type>
            <viewport>
                <southwest>
                    <lat>48.1852199</lat>
                    <lng>11.5825577</lng>
                </southwest>
                <northeast>
                    <lat>48.1915151</lat>
                    <lng>11.5888529</lng>
                </northeast>
            </viewport>
        </geometry>
        <partial_match>true</partial_match>
    </result>
</GeocodeResponse>

The XML response is deserialized to Java objects using JAXB 2. After the response is processed, the user is given some options to refine the original address entered in the browser.

It’s convenient that all responses from the Google web service are localized. In the screenshots below, you see the same validation done in the German shop (left) and in the international shop (right). The only difference is the language parameter sent with the web service request and set in the HTTP request header “Accept-Language”. To visually improve validation, we also use static Google maps URL to display the address to the user graphically.

The Geolocation web service is localized: Address data returned for the german shop (left) and for the international shop (right). Click to enlarge.

Conclusion

Google’s Geocoding web service turned out to be an effective tool for the validation of the user addresses in our Kickz online shop. Furthermore, this service can be used free of charge for small projects or sites with lower traffic. In Kickz though, we are using the commercial version of the web service, since the traffic is higher and we need the HTTPS service URL.

Share

Leave a Reply

*

3 Responses to “How Google’s Geocoding solves Address Validation”

  1. Mark says:

    Hello!
    Thank you for your post. We would like to create a Google Form which validate the addresses. Can this be done simply by putting the correct code into the form code?

    Thanks!
    Mark

    • Petr Skokan says:

      Hi Mark,

      I do think you will need to process the JSON response from google anyway. So the best option is to use Javascript and call Google API, process the response and you are done with validation. Just check the link with documentation.

      Regards,

      Petr.

  2. spk says:

    Hi,
    Nice post. Was searching all around for a free address validator. Thanks a lot for sharing.