Google GeoCoding API

less than 1 minute read

Find Latitude and Longitude

Required dependency for accessing the API via Java/Spring

implementation 'com.google.maps:google-maps-services:2.1.0'

GeoCodingAPI Documentation

public GeocodingResult[] getLatLngFromGeoAPI(String address){
    GeoApiContext context = new GeoApiContext.Builder()
            .apiKey("API KEY")
            .build();

    GeocodingResult[] results = new GeocodingResult[0];
    try {
        results = GeocodingApi.geocode(context, address).await();//pass postal address
    } catch (ApiException | InterruptedException | IOException e ) {
        e.printStackTrace();
    }

    // Invoke .shutdown() after making requests
    context.shutdown();

    return results;
}

From the invoking method, extract the latitude and longitude from the GeoCodingResult Array

GeocodingResult -> Geometry -> LatLng -> has double lat & double lng; USe

GeocodingResult[] results = getLatLngFromGeoAPI(address);
Gson gson = new GsonBuilder().setPrettyPrinting().create();

 if (results.length != 0) {
    //log.info(address);

    String latitude = gson.toJson(results[0].geometry.location.lat);
    String longitude = gson.toJson(results[0].geometry.location.lng);
 }