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 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:
Then, you can add a reference to the dependency:
To install with Gradle, add the reference to your root build.gradle at the end of repositories:
Then, you can add the dependency in build.gradle:
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:
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:
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:
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:
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:
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.