How to Reverse Geocode a Latitude and Longitude Input into an Address using Java
Most international addresses can be geocoded into their latitude and longitude coordinates on a map using readily available geographical data. In this article, I’m going to demonstrate how you can reverse that process and identify a street address based on a given latitude and longitude. You can do so using our Reverse Geocode API (please note: this is useful to use in conjunction with our Geocode Street Address API, which you can find on this page under the Address section). With valid latitutde and longitude inputs, this API will return street address information in the following format:
JSON:
{
"Successful": true,
"StreetAddress": "string",
"City": "string",
"StateOrProvince": "string",
"PostalCode": "string",
"CountryFullName": "string",
"CountryCode": "string"
}XML:
<?xml version="1.0" encoding="UTF-8"?>
<ReverseGeocodeAddressResponse>
<Successful>true</Successful>
<StreetAddress>string</StreetAddress>
<City>string</City>
<StateOrProvince>string</StateOrProvince>
<PostalCode>string</PostalCode>
<CountryFullName>string</CountryFullName>
<CountryCode>string</CountryCode>
</ReverseGeocodeAddressResponse>
It’s important to keep in mind not all of this information will always be available for each input; the API response will include whatever information it can access. Below, I’ll walk through a few simple steps to help you structure your Reverse Geocode API call using complementary, ready-to-run Java code examples.
Before we call the function, we’ll first need to install the API client with maven. To do so, let’s first add a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
After that, let’s add another reference to the pom.xml dependency, which will compile our library:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Finally, let’s call the Reverse Geocode function. We’ll start by adding the imports to the top of our file, and after that, we’ll satisfy the API key authentication field (you can get a free API key by registering a free account on our website):
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.AddressApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");AddressApi apiInstance = new AddressApi();
ReverseGeocodeAddressRequest input = new ReverseGeocodeAddressRequest(); // ReverseGeocodeAddressRequest | Input reverse geocoding request
try {
ReverseGeocodeAddressResponse result = apiInstance.addressReverseGeocodeAddress(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AddressApi#addressReverseGeocodeAddress");
e.printStackTrace();
}
When we input our reverse geocode request, we’ll want to do so in the following format:
{
"Latitude": 0,
"Longitude": 0
}
And with that, we’re all done — no more code required. Nice and simple!