This blog will help you in getting geographical information of any location by using latitude/longitude.
By using Java API Geocoder, it is easy to get geographical information of any location. The response will be in JSON or XML format.
Write the following example to get Zip code from Latitude/Longitude:
private String getZipCode(Double latitude, Double longitude)
{
String zipcode = null;
try
{
LatLng latLng = new LatLng();
latLng.setLat(BigDecimal.valueOf(latitude));
latLng.setLng(BigDecimal.valueOf(longitude));
final Geocoder geocoder = new Geocoder();
GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setLocation(latLng).getGeocoderRequest();
GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
List<GeocoderResult> results = geocoderResponse.getResults();
logger.debug("results : "+results); //This will print geographical information
List<GeocoderAddressComponent> geList= results.get(0).getAddressComponents();
if(geList.get(geList.size()-1).getTypes().get(0).trim().equalsIgnoreCase("postal_code"))
{
zipcode = geList.get(geList.size()-1).getLongName();
}
else if(geList.get(0).getTypes().get(0).trim().equalsIgnoreCase("postal_code"))
{
zipcode = geList.get(0).getLongName();
}
logger.debug("zipcode : " + zipcode);
}catch (Exception e) {
logger.debug(e.toString());
logger.debug(e.getLocalizedMessage());
}
return zipcode;
}
Write the following Maven configuration in pom.xml file in your project:
<dependency>
<groupId>com.google.code.geocoder-java</groupId>
<artifactId>geocoder-java</artifactId>
<version>0.5</version>
</dependency>
Hope this will help you :)
0 Comment(s)