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

    • 0
    • 3
    • 1
    • 4
    • 1
    • 0
    • 0
    • 0
    • 1.84k
    Comment on it

    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 Latitude/Longitude from Zip code:

    String postCode = "07001"; // you can use address of any location here instead of zipcode
    final Geocoder geocoder = new Geocoder();
    GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(postCode).getGeocoderRequest();
    GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
    List results = geocoderResponse.getResults();
    
    System.out.println("results :  "+results); //This will print geographical information
    
    float latitude = results.get(0).getGeometry().getLocation().getLat().floatValue();
    float longitude = results.get(0).getGeometry().getLocation().getLng().floatValue();
    System.out.println("Lat/Long :  " + latitude +" , "+longitude);

    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>
    

    Hope this will help you :)

 1 Comment(s)

  • In you first code at line 5 : List results = geocoderResponse.getResults();

    can you please tell me import of List, because when Im trying with "java.util" im getting error at below lines.

    float latitude = results.get(0).getGeometry().getLocation().getLat().floatValue(); float longitude = results.get(0).getGeometry().getLocation().getLng().floatValue();
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: