Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Getting ZipCode from lat/long in android

    • 0
    • 3
    • 1
    • 4
    • 0
    • 0
    • 0
    • 0
    • 3.76k
    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 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&#95;code"))
            {
                zipcode = geList.get(geList.size()-1).getLongName();
            }
            else if(geList.get(0).getTypes().get(0).trim().equalsIgnoreCase("postal&#95;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)

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: