Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Getting address and city from lot/long in Java

    • 0
    • 4
    • 1
    • 4
    • 0
    • 0
    • 0
    • 0
    • 1.10k
    Comment on it

    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 Address and city from Latitude/Longitude:

    public static String[] getCityAndAddress(Double latitude, Double longitude)
    {
        logger.debug("latitude :  "+latitude+" longitude: "+longitude);
        String[] addressArray = new String[2];
        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();
            //This will print geographical information        
            logger.debug("results :  "+results);
            List<GeocoderAddressComponent> geList= results.get(0).getAddressComponents();
    
            for(int i =0; i < geList.size(); i++)
            {
                if(geList.get(i).getTypes().get(0).equalsIgnoreCase("locality"))
                {
                    addressArray[0] = geList.get(i).getLongName();
                }
            }
            addressArray[1] = results.get(0).getFormattedAddress();
            logger.debug("addressArray :  " + addressArray);
    
        }catch (Exception e) {
            logger.debug(e.toString());
            logger.debug(e.getLocalizedMessage());
        }
        return addressArray;
    }
    

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: