How to Validate and Geocode a Street Address in Java
When working with location services, it is important that the information you collect is accurate for your users or clients. Find out more!
Join the DZone community and get the full member experience.
Join For FreeWhen working with location services, it is important that the information you collect is accurate for your users or clients. This will prevent any mistakes in shipping, billing, and many other aspects of operations that rely on correct location information. For businesses that have applications using location services, this is especially important as any incorrect data can mean the displacement of goods or interrupted services.
The following APIs will allow you to fully validate street addresses by first parsing address data input and then verifying and normalizing the information. The last two APIs will also allow you to geocode and reverse geocode an address to receive more accurate location data for your applications.
To use any of these APIs, you will first need to install the SDK library using Maven or Gradle. To install with Maven, add a Jitpack reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, you can add a reference to the dependency:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
To install with Gradle, add the reference to your root build.gradle at the end of repositories:
xxxxxxxxxx
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Then, you can add the dependency in build.gradle:
xxxxxxxxxx
dependencies {
implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
}
The first API will allow you to parse an unstructured input address into an international, standardized address. This is especially useful when receiving information via email or other unformatted correspondence. By using this function, you can take a shared address and ensure that it is formatted correctly and that the information is accurate before storage or use.
To run the API, install the SDK library as shown above and add the imports to the top of the file. Then, you can call the function:
xxxxxxxxxx
// 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();
ParseAddressRequest input = new ParseAddressRequest(); // ParseAddressRequest | Input parse request
try {
ParseAddressResponse result = apiInstance.addressParseString(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AddressApi#addressParseString");
e.printStackTrace();
}
This will return a response stating whether the function was successful, as well as the building, street number, street, city, state or province, postal code, and country name, and ISO code. To ensure that this API works properly, you need to ensure certain requirements are met:
- The address and capitalization mode are input correctly.
- You have input your API Key. This can be retrieved at no cost on the Cloudmersive website, providing 800 monthly calls across our API library.
This second API will validate whether an input address is valid. This is important for organizations collecting user information, as incorrect address information can lead to the misplacement of items and services. The input for this function should include the street address, city, state or province, postal code, and country name and code. Like before, install the SDK library and add the imports to the top of your file. Then, call the function:
xxxxxxxxxx
// 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();
ValidateAddressRequest input = new ValidateAddressRequest(); // ValidateAddressRequest | Input parse request
try {
ValidateAddressResponse result = apiInstance.addressValidateAddress(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AddressApi#addressValidateAddress");
e.printStackTrace();
}
This will return a response stating whether the address was valid, as well as its latitude and longitude.
If you need to retrieve more data for a street address beyond the first two functions, you can use this next API to normalize a street address, which will return all relevant information. You will use the same input information as described in the previous example: street address, city, state or province, postal code, and country name and code. Install the SDK, add the imports to the top of the file, and call the function as shown below:
xxxxxxxxxx
// 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();
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();
}
Beyond address validation, you can also perform geocoding, which will take a text string input for an address and return the geographical information for its location (i.e., latitude and longitude). This will provide exact location data, which is useful for phone applications like locator, delivery, and ride-sharing platforms, as well as many others. To run the API, install the SDK and call the function as shown below:
xxxxxxxxxx
// 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();
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();
}
You can also perform the reverse of this action using this final API. Reverse geocoding will take a latitude and longitude pair and return the street address for that location. For applications that find and highlight locations on a map, this can be useful for identifying buildings and other landmarks, as well as providing a way for users to use their current location as an option for location. Install the SDK and use this API by calling the function below:
xxxxxxxxxx
// 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();
}
This will return the street address, city, state or province, postal code, and country name and code for the location.
With these, you can ensure that you have accurate location information whether it is for your users or your services. If you have any questions about using these APIs or inquiries concerning other API solutions, you can visit the Cloudmersive website, where our team is happy to help with anything you might need.
Opinions expressed by DZone contributors are their own.
Comments