Wednesday, 29 December 2010

Parsing and Standardizing an Address using GeoGoogle API

Google provides an API to address standardization. It standardizes addresses by utilizing google's geocoding service.
GeoGoogle is a client API to google's geocoding service, if you are looking for a offline solution (which has no usage limit), take a look at http://jgeocoder.sourceforge.net

To use geocoding web service, you need sign-up for your own API key at http://www.google.com/apis/maps/signup.html

more information on http://geo-google.sourceforge.net/usage.html

here is a sample:

public class GeoGoogleTest {
final static String PROXY_SERVER = "myproxy.com";
final static int PROXY_PORT = 80;
private static GeoAddressStandardizer createGeoAddressStandardizer() {
final HttpClientParams httpClientParams = new HttpClientParams();
final HttpClient httpClient = new HttpClient(httpClientParams);
//enable these 3 lines if you are behind a proxy server.
/*
final HostConfiguration hostConfiguration = new HostConfiguration();
hostConfiguration.setProxy(PROXY_SERVER, PROXY_PORT);
httpClient.setHostConfiguration(hostConfiguration);
*/
GeoAddressStandardizer.setHttpClient(httpClient);
//this is my key, you must get you own from "http://code.google.com/apis/base/signup.html" and insert it here.
return new GeoAddressStandardizer("ABQXXXXXXXXXXXXXXXXXXX");
}
public static void main(final String[] args) throws GeoException {
final String testAddress = "Via Tommaso Campanella, 15b, 00355 Roma, Italy";
final GeoAddressStandardizer geoAddressStandardizer = createGeoAddressStandardizer();
final List<GeoAddress> geoAddresses = geoAddressStandardizer.standardizeToGeoAddresses(testAddress);
for (final GeoAddress geoAddress : geoAddresses) {
System.out.println(geoAddress.getAddressLine());
}
final String addressLine1 = geoAddressStandardizer.standardize(testAddress, new XmlMappingFunctor<String>() {
@Override
public String execute(final String xml) {
try {
// parse the xml string into a document object
final Document doc = XmlUtils.parse(xml);
System.out.println(xml);
// use XPath to select an value from the document
return XmlUtils.selectValue(doc, "//ThoroughfareName");
} catch (final Exception e) {
System.out.println(e.getMessage());
}
return null;
}
});
System.out.println("Output ==> " + addressLine1);
}
}

No comments:

Post a Comment