Cloudmersive

Jun 18, 2021

2 min read

How to Reverse Geocode a Location in Go

Reverse geocoding is an excellent method to ensure that accurate location information is provided via your programs and applications. In this tutorial, we will demonstrate how you can use an API in Go to automatically convert latitude and longitude coordinates to a normalized street address; this will include the city, state/province, postal code, country name, and country code.

All we need to perform the operation are the location coordinates and your API key, which you can input into the following example code:

package mainimport (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/validate/address/geocode/reverse"
method := "POST"
payload := strings.NewReader(`{
"Latitude": "<double>",
"Longitude": "<double>"
}`)
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

Utilizing this API will enable a better user experience and more efficient flow for any applicable location service. If you need to obtain an API key, visit the Cloudmersive website to register for a free account; this will provide 800 monthly calls across our library of APIs.