Monday, July 4, 2011

Using Google Geocoding API for address validation


In e-commerce world it is very important to have valid and correct address base. Sending packages to an incorrect addresses causes
significant looses to eshops,when packages are not delivered and sent back to the eshop. In the kickz e-shop our customer had exactly those problems with many incomplete or incorrect returns due to bad addresses (especially from France). Thus we were thinking how to improve and force users to enter correct addresses. There are many commercial providers for addresses, however not all do have world address bases and as kickz is delivering to whole world it turned out that best value for money will be to use google geocoding web-service. Google has famous google maps application and as a side service they are providing either free or commercial web service for localisation of the position by GPS. So basically original idea of the service is to provide exact coordinates on the map for requested address. However as google response is not only the longitude/lattitude, this service might be used easily to refine addresses as well. That is exactly what we needed.
Basic geocoding api web service is free for 1000 requests per day and does not support https requests. As we did need https and more request per day, kickz has decided to go for paid service, which is unlimited and provides https URL with signed web-service requests.

Google 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 recieve either xml or JSON response. Response might contain either one address or more addresses based on quality of input. It might as well be empty if google cannot locate your input address, but based on our experience it is rare case.Response in JSON is obvious choice for JavaScript (AJAX) based validation on client side, XML is more suitable for classic server side (request/response) validation. In kickz project we have chosen xml response option.In kickz shop we do validation through google WS everywhere, where user enters his address. This is either during customer registration or during checkout in shop. Request from browser is first sent to tomcat and on server side we do request to google WS signed with certificate and we process xml response returned and deserialise with JAXB to Java objects. After response is processed, user is shown options to refine his address entered in browser. Advantage of service is that all responses from google are localized. In the screenshots below you might see same validation once done in german shop and second time in international shop with different language parameters. Language parameter is set in the http request header with parameter "Accept-Language". To improve validation visually we do use also static google maps URL to show address graphically to the user.






Request

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=RD8P7J07rJbfmClUeMEY4adIoTs=


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ünchen</long_name>   
            <short_name>München</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>



Conclusion

Google geocoding web service turned out to be effective tool for validation of the user addresses in kickz eshop. Furthermore this service might be used free of charge for small projects or pages with lower traffic. In kickz though we are using paid version of web service as traffic is higher and we do need https service URL. You can find more information about the web service URLs etc. on Geocoding API webpage.