How to Normalize a Street Address using Java

Cloudmersive
2 min readOct 24, 2022

--

Address inputs and location services are the lifeblood of many online businesses. To effectively capture & store address information, it’s critical that such businesses provide clear, succinct inputs for client-side users. Our Normalize Street Address API makes it easy to capture & validate user addresses, providing structured & clearly labeled fields for their convenience. This API will accept an argument in the following JSON format:

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

In response, it will determine whether or not the input address is valid, and if true, it will additionally provide the latitude and longitude of the address:

{
"ValidAddress": true,
"Building": "string",
"StreetNumber": "string",
"Street": "string",
"City": "string",
"StateOrProvince": "string",
"PostalCode": "string",
"CountryFullName": "string",
"ISOTwoLetterCode": "string",
"Latitude": 0,
"Longitude": 0
}

Thankfully, it’s super easy to use this API — and you can use it for free on a limited basis. You’ll just need to register a free account on our website (free accounts provide an API key with a limit of 800 API calls per month & zero additional commitments) and use the Java code examples provided below to structure your API call.

Let’s get right to it. Our first step is to install the API client with Maven. We can do so by first adding a reference to the repository in pom.xml:

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

Next, we can add a 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>

After that, it’s time to call the function. We can begin by adding our imports to 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;

And then we’ll pass our API key and JSON arguments through their respective fields below:

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 {
NormalizeAddressResponse result = apiInstance.addressNormalizeAddress(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AddressApi#addressNormalizeAddress");
e.printStackTrace();
}

After that, you’re all set — no more code required. Simple & easy!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet