How to Geocode a Street Address using Java

If you’re adding map features to your application, or simply trying to get more mileage out of your customer data, geolocation/geocoding plays an important role. With our Geocode a Street Address API, you can input a parse request containing a street address, city, state/province, postal code & more, and subsequently identify the exact latitude and longitude of that address (after validating the address first). The information from this API will make it possible to plot the addresses you receive on any map and visualize your customer base.

To use this API for free, you just need to register a free account on our website (this account comes with a limit of 800 API calls per month, perfect for small-scale projects/startups) and use your API key to authenticate the service. Below, I’ve included ready-to-run Java code examples to help you structure your API call.

Before we call the function, let’s first install the API client. We can begin by adding a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

And we can wrap up installation by adding an additional reference to the dependency in pom.xml:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Turning our attention to the controller, let’s include the imports at the top of our file:

// 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;

Now we can complete our API call by calling the geocoding function. Let’s first use the below JSON format to structure our arguments:

{
"StreetAddress": "string",
"City": "string",
"StateOrProvince": "string",
"PostalCode": "string",
"CountryFullName": "string",
"CountryCode": "string"
}

And let’s pass that through the below code, including our API key to authenticate the service:

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();
ValidateAddressRequest input = new ValidateAddressRequest(); // ValidateAddressRequest | Input parse request
try {
ValidateAddressResponse result = apiInstance.addressGeocode(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AddressApi#addressGeocode");
e.printStackTrace();
}

That’s all the code you’ll need — you can now validate & map any address in the blink of an eye.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloudmersive

Cloudmersive

436 Followers

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.