How to Geocode a Street Address in C/C++

Cloudmersive
1 min readApr 5, 2021

To ensure you’re getting the most out of your market research, you need to know the geographic location of your clients and any trends that may be occurring there. If users input their address into your website or application, you can easily leverage this information to geocode their precise latitude and longitude using the following API in C/C++.

For our first step, we will install libcurl into our project:

libcurl/7.75.0

Post-installation, we are ready to input the address info and API key, and call the function with the below code:

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/validate/address/geocode");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
const char *data = "{\n \"StreetAddress\": \"<string>\",\n \"City\": \"<string>\",\n \"StateOrProvince\": \"<string>\",\n \"PostalCode\": \"<string>\",\n \"CountryFullName\": \"<string>\",\n \"CountryCode\": \"<string>\"\n}";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);

And we’re done! The response will indicate if the address is valid, along with the aforementioned coordinates.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.