Reverse Geocoding Coordinates to Addresses with the Go Programming Language

By Nic Raboy | 15 October 2018

When working with GPS devices and popular software, you will typically find yourself with information around latitude and longitude coordinate information, which is great, but not exactly human readable. A more readable format would be address information with streets, cities, countries, etc., something that we can make sense of. HERE offers a reverse geocoder API that allows you to take latitude and longitude information collected from elsewhere and convert it into address information.

To get a better idea what we’re trying to accomplish, take a look at the following image:

In the above image you’ll notice that a CLI was created. When running the application, a latitude and longitude was passed and we got address information as a response. The command is more complex than it needs to be because we’re pretty printing the JSON data with a Python script, which isn’t completely necessary.

Mapping the HERE Geocoder API with Native Go Data Structures

Before we start using the HERE Geocoder API, we need to add our boilerplate Golang code and create the data structures that will hold the results to our requests. Within your $GOPATH, create a main.go file and include the following:

In the above code you’ll notice we have our import packages, a main function, and three data structures. The GeoData data structure is probably the most important for this project. Essentially what I’ve done with GeoData is model the REST response defined in the HERE documentation. If you look closely, you’ll notice that I haven’t mapped the response property for property. In fact, I left half of the response out. I did this because I only really care about the address information. However, the format of the response and model must match. You’ll also notice the JSON annotations in the Go data structure. These JSON annotations match the property names in the response.

The Position data structure will allow us to keep track of latitude and longitude coordinates, and the Geocoder data structure will allow us to keep track of our API token information. The AppId and AppCode can both be obtained after creating a free account with HERE.

Now that we have a means to store our data in our application, we can start making requests against the REST API that HERE provides.

Using REST to Convert Between Positions and Addresses

To use the HERE Geocoder API, all we really need to do is create an HTTP request that includes our token information, along with coordinate information to be converted into addresses. The response will be stored as a GeoData object to be used however we wish.

The function is quite large, but most of it is around formatting a request. The function will use the Geocoder information as well as passed Position information. When creating a request, we provide the correct URL, and construct a query string with the appropriate information. After we execute the request, we check to see if there was an error or success. If there was success, we can read the response, which is byte data, and convert it into GeoData to be returned. The conversions happen based on our JSON annotation mapping.

Building a CLI for Accepting Data Defined by the User

With the REST function in place, we can bring our application together so it can be used like a command line interface (CLI). The goal here is to just take user defined information and pass it into our function.

In the main function we are anticipating two flags to be passed when launching the application. If these flags are not passed, the default value will be used. Once we parse the flags, we can set our app id and app code information and then call our REST function. Upon a successful request, if there is data found, we will display only the first result which is the nearest result to our coordinates. There can potentially be other results returned in the request.

Conclusion

You just saw how to use Golang to make requests against the HERE Reverse Geocoder API to convert latitude and longitude coordinate information into address information that is much easier to read.

A next step to this might include taking a CSV file of data and converting it into addresses.

by Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.