DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Using Java Stream Gatherers To Improve Stateful Operations
  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025

Trending

  • Go 1.24+ Native FIPS Support for Easier Compliance
  • Role of Cloud Architecture in Conversational AI
  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  1. DZone
  2. Coding
  3. Java
  4. How to Validate and Geocode a Street Address in Java

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!

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Dec. 17, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
9.8K Views

Join the DZone community and get the full member experience.

Join For Free

When 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:

XML
 




x


 
1
<repositories>
2
    <repository>
3
        <id>jitpack.io</id>
4
        <url>https://jitpack.io</url>
5
    </repository>
6
</repositories>



Then, you can add a reference to the dependency:

XML
 




xxxxxxxxxx
1


 
1
<dependencies>
2
<dependency>
3
    <groupId>com.github.Cloudmersive</groupId>
4
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
5
    <version>v3.54</version>
6
</dependency>
7
</dependencies>



To install with Gradle, add the reference to your root build.gradle at the end of repositories:

Java
 




xxxxxxxxxx
1


 
1
allprojects {
2
    repositories {
3
        ...
4
        maven { url 'https://jitpack.io' }
5
    }
6
}



Then, you can add the dependency in build.gradle:

Java
 




xxxxxxxxxx
1


 
1
dependencies {
2
        implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
3
}



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:

Java
 




xxxxxxxxxx
1
25


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.AddressApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
AddressApi apiInstance = new AddressApi();
17
ParseAddressRequest input = new ParseAddressRequest(); // ParseAddressRequest | Input parse request
18
try {
19
    ParseAddressResponse result = apiInstance.addressParseString(input);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling AddressApi#addressParseString");
23
    e.printStackTrace();
24
}



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:

Java
 




xxxxxxxxxx
1
24


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.AddressApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
AddressApi apiInstance = new AddressApi();
17
ValidateAddressRequest input = new ValidateAddressRequest(); // ValidateAddressRequest | Input parse request
18
try {
19
    ValidateAddressResponse result = apiInstance.addressValidateAddress(input);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling AddressApi#addressValidateAddress");
23
    e.printStackTrace();
24
}



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:

Java
 




xxxxxxxxxx
1
24


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.AddressApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
AddressApi apiInstance = new AddressApi();
17
ValidateAddressRequest input = new ValidateAddressRequest(); // ValidateAddressRequest | Input parse request
18
try {
19
    NormalizeAddressResponse result = apiInstance.addressNormalizeAddress(input);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling AddressApi#addressNormalizeAddress");
23
    e.printStackTrace();
24
}



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:

Java
 




xxxxxxxxxx
1
24


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.AddressApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
AddressApi apiInstance = new AddressApi();
17
ValidateAddressRequest input = new ValidateAddressRequest(); // ValidateAddressRequest | Input parse request
18
try {
19
    ValidateAddressResponse result = apiInstance.addressGeocode(input);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling AddressApi#addressGeocode");
23
    e.printStackTrace();
24
}



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:

Java
 




xxxxxxxxxx
1
24


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.AddressApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
AddressApi apiInstance = new AddressApi();
17
ReverseGeocodeAddressRequest input = new ReverseGeocodeAddressRequest(); // ReverseGeocodeAddressRequest | Input reverse geocoding request
18
try {
19
    ReverseGeocodeAddressResponse result = apiInstance.addressReverseGeocodeAddress(input);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling AddressApi#addressReverseGeocodeAddress");
23
    e.printStackTrace();
24
}



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.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Using Java Stream Gatherers To Improve Stateful Operations
  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!