Trifork Blog

Axon Framework, DDD, Microservices

Using Google Geocoding API with Geocoder

April 14th, 2011 by
|

I was looking for a way to retrieve latitude and longitude for a given address. I thought that Google could provide me with a solution and eventually it did. First I was looking at the Google Data API, but found that some parts were deprecated and not supported anymore. The Google Geocoding API, that is part of the Google Maps API, should be used. You can execute a request. The response will be in JSON or XML format. Next to that there is a nice Java API called the Geocoder. Through this API it is easy to obtain geographical information.

The following example shows how to retrieve latitude and longitude in a test:

@Test
public void testGetLocation() {
    final Geocoder geocoder = new Geocoder();
    GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress("Rijnsburgstraat 9-11, Amsterdam, The Netherlands").getGeocoderRequest();
    GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
    List<GeocoderResult> results = geocoderResponse.getResults();
    float latitude = results.get(0).getGeometry().getLocation().getLat().floatValue();
    float longitude = results.get(0).getGeometry().getLocation().getLng().floatValue();
    assertEquals(52.347797f, latitude);
    assertEquals(4.8507648f, longitude);
}

If you want to use this in your project, you can obtain the Java client from a Maven repository. The following code block shows the Maven configuration of the artifact:

<dependency>
    <groupId>com.google.code.geocoder-java</groupId>
    <artifactId>geocoder-java</artifactId>
    <version>0.5</version>
</dependency>

Now you can determine the latitude and longitude of the place where you live!

One Response

  1. October 2, 2012 at 14:21 by chinna

    Hi,

    I am looking around in the google for this type of solution in my project. i found this post is very interestiong and i implemented it in my project. while executing the above code it is saying the following error ?
    [main] ERROR com.google.code.geocoder.Geocoder – Connection refused: connect
    java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[na:1.7.0_03]
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69) ~[na:1.7.0_03]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) ~[na:1.7.0_03]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) ~[na:1.7.0_03]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) ~[na:1.7.0_03]
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157) ~[na:1.7.0_03]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391) ~[na:1.7.0_03]
    at java.net.Socket.connect(Socket.java:579) ~[na:1.7.0_03]
    at java.net.Socket.connect(Socket.java:528) ~[na:1.7.0_03]
    at sun.net.NetworkClient.doConnect(NetworkClient.java:180) ~[na:1.7.0_03]
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:388) ~[na:1.7.0_03]
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:483) ~[na:1.7.0_03]
    at sun.net.www.http.HttpClient.(HttpClient.java:213) ~[na:1.7.0_03]
    at sun.net.www.http.HttpClient.New(HttpClient.java:300) ~[na:1.7.0_03]
    at sun.net.www.http.HttpClient.New(HttpClient.java:316) ~[na:1.7.0_03]
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:992) ~[na:1.7.0_03]
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:928) ~[na:1.7.0_03]
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:846) ~[na:1.7.0_03]
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1296) ~[na:1.7.0_03]
    at java.net.URL.openStream(URL.java:1035) ~[na:1.7.0_03]
    at com.google.code.geocoder.Geocoder.request(Geocoder.java:72) ~[geocoder-java-0.9.jar:na]
    at com.google.code.geocoder.Geocoder.geocode(Geocoder.java:63) ~[geocoder-java-0.9.jar:na]
    at de.dmc.intersport.enfinity.ch.b2c.app.capi.GoogleGeoLocation.getGeoCoordinates(GoogleGeoLocation.java:25) [lib/:na]
    at de.dmc.intersport.enfinity.ch.b2c.app.capi.GoogleGeoLocation.main(GoogleGeoLocation.java:17) [lib/:na]
    Exception in thread “main” java.lang.NullPointerException

    What is the reason for this.
    Any help will be greatly appriciated