how to use google Geocoding in java?
Asked Answered
N

5

5

I am developing a functionality for my project that when user enters 'Postal Code' the co-ordinates(latitude, longitude) for the corresponding 'postal code' should display. the implementation platform is 'JAVA'.

I googled for the java api but i did not find any specific resource.

Any suggstions will be greatly appriciated

Noreennorene answered 2/10, 2012 at 11:2 Comment(0)
A
3

It's quite straight forward. E.g. have a look at the geocode method of this class. Pass your postalcode (along with the country) to the method and you should be fine. (The GLatLng class can be found here but should be replaced by you according to your needs.)

[edit] I just saw that this example is still using Google Maps v2, but it should be a breeze to convert it to v3. In the linked documents you can also find the restrictions regarding the use of this service (paragraph "Usage Limits")

Assumed answered 2/10, 2012 at 11:47 Comment(1)
thank you very much for your answer, to test the result do i need to have any license form "Google" ?Noreennorene
V
8

You can (now) check out the open-source Java client library for Geocoding, Directions, DistanceMatrix, Elevation and TimeZone too: https://github.com/googlemaps/google-maps-services-java

Viaduct answered 28/10, 2014 at 0:25 Comment(0)
A
3

It's quite straight forward. E.g. have a look at the geocode method of this class. Pass your postalcode (along with the country) to the method and you should be fine. (The GLatLng class can be found here but should be replaced by you according to your needs.)

[edit] I just saw that this example is still using Google Maps v2, but it should be a breeze to convert it to v3. In the linked documents you can also find the restrictions regarding the use of this service (paragraph "Usage Limits")

Assumed answered 2/10, 2012 at 11:47 Comment(1)
thank you very much for your answer, to test the result do i need to have any license form "Google" ?Noreennorene
S
2

The docs on this are not new user friendly. For me the java docs worked best although theyre quite rigid: https://www.javadoc.io/doc/com.google.maps/google-maps-services/latest/com/google/maps/GeocodingApiRequest.html

Here is how to do so using the newest Google Geocoding API:

//use your google api key to create a GeoApiContext instance
GeoApiContext context = new GeoApiContext.Builder().apiKey("xxxxxx").build();

//this will get geolocation details via zip 
GeocodingResult[]results = GeocodingApi.newRequest(context).components(ComponentFiler.postalCode("75002")).await(); 
System.out.println(results[0]);

//this will get geolocation details via address 
GeocodingResult[] results2 = GeocodingApi.geocode(context, "One Apple Park Way Cupertino, CA 95014").await();
System.out.println(results2[0]);

//another way to get geolocation details via address 
GeocodingResult[] results3 = GeocodingApi.newRequest(context).address("One Apple Park Way Cupertino, CA 95014").await();
System.out.println(results3[0]);

//geolocation details via lat lng 
GeocodingResult[] results4 = GeocodingApi.newRequest(context).latlng(latLng).await(); 
System.out.println(results4[0]);           

from there you'll get a bunch of data on the returned location in the results array, you can parse it as you wish to extract your choice of data. heres an ex of what that data looks like....

[GeocodingResult placeId=ChIJyTSQVXm0j4ARmdUQoA1BpwQ [Geometry: 37.31317000,-122.07238160 (APPROXIMATE) bounds=[37.34159500,-121.99557710, 37.24799500,-122.14382300], viewport=[37.34159500,-121.99557710, 37.24799500,-122.14382300]], formattedAddress=MONTE VISTA, CA 95014, USA, types=[postal_code], addressComponents=[[AddressComponent: "95014" ("95014") (postal_code)], [AddressComponent: "MONTE VISTA" ("MONTE VISTA") (locality, political)], [AddressComponent: "Santa Clara County" ("Santa Clara County") (administrative_area_level_2, political)], [AddressComponent: "California" ("CA") (administrative_area_level_1, political)], [AddressComponent: "United States" ("US") (country, political)]], postcodeLocalities=[Cupertino, MONTE VISTA, Permanente]]
Shuffleboard answered 10/2, 2021 at 3:35 Comment(0)
T
1

You could try to write a REST client (using e.g. Apache HttpClient) for Google geocoding API RESTful web service.

Tipster answered 2/10, 2012 at 11:27 Comment(0)
G
0

Here is a way for doing it (include source): http://halexv.blogspot.mx/2015/07/java-geocoding-using-google-maps-api.html

The blog explain how to do it without having and API_KEY and a way for filtering the locatiotions into the most probable one.

Give it a try.

Garrow answered 12/7, 2015 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.