Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.6.0+

Azure Maps: Geocode an Address

See more Azure Maps Examples

Converts an address into location candidates using the current Azure Maps Geocoding API and direct Chilkat.Http calls. Shows formatted addresses, confidence, match codes, and coordinates. An empty result is handled separately from a failed request. Geocoding is not proof that mail can be delivered to an address.

This replaces the older /search/address/json?api-version=1.0 demonstration with /geocode?api-version=2026-01-01. The response is now a GeoJSON FeatureCollection: read features instead of results. Coordinates are ordered longitude, latitude. For routing, an entrance point in properties.geocodePoints can differ from the main display point.

Create an Azure Maps account and supply its key as a nonempty subscription_key string in private qa_data/azure/maps.json. The sample sends it in the subscription-key header. Shared-key authentication must be enabled for the account. Keep the file out of source control and public web directories; do not distribute an account key inside a public application. See Azure Maps authentication for Microsoft Entra ID and SAS alternatives.

HttpParams encodes the address query. Supply ordinary text, not an already escaped string. The free-form query must not be combined with structured address fields. The sample asks for up to five candidates and shows them for review rather than treating the first result as an exact match.

The code distinguishes transport failures, HTTP errors, invalid JSON, and no matches. Retrying transient failures should be bounded and respect Retry-After; fixing a bad request or invalid credentials requires changing the request or configuration. Inspect detailed diagnostics privately because addresses and credentials can appear in them. HTTPS certificate verification remains enabled.

Microsoft Geocoding API reference

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkJsonArrayW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global.UnlockBundle for sample code.

    //  Private configuration: {"subscription_key":"YOUR_AZURE_MAPS_KEY"}.
    //  Keep this file outside source control and public web directories.
    CkJsonObjectW config;
    config.put_Strict(true);
    success = config.LoadFile(L"qa_data/azure/maps.json");
    if (success != true) {
        wprintf(L"Could not load the private Azure Maps configuration.\n");
        return;
    }

    if ((config.JsonTypeOf(L"subscription_key") != 1) || (config.StringOfEquals(L"subscription_key",L"",true) == true)) {
        wprintf(L"A nonempty subscription_key string is required.\n");
        return;
    }

    CkHttpW http;
    http.put_RequireSslCertVerify(true);
    http.put_FollowRedirects(false);
    http.put_ConnectTimeout(30);
    http.put_ReadTimeout(60);
    http.put_Accept(L"application/geo+json");
    //  The current Geocoding API accepts this key as a header, avoiding a key in the URL.
    http.SetRequestHeader(L"subscription-key",config.stringOf(L"subscription_key"));

    //  HttpParams URL-encodes the raw query values. Do not pre-encode the address.
    //  Use query OR structured fields such as addressLine/locality/postalCode, not both.
    CkJsonObjectW query;
    query.UpdateString(L"api-version",L"2026-01-01");
    query.UpdateString(L"query",L"15127 NE 24th Street, Redmond, WA 98052");
    query.UpdateInt(L"top",5);

    CkHttpResponseW response;
    success = http.HttpParams(L"GET",L"https://atlas.microsoft.com/geocode",query,response);
    //  HttpParams also returns false for HTTP errors. Status 0 means no HTTP response.
    if (response.get_StatusCode() == 0) {
        wprintf(L"Azure Maps request did not receive an HTTP response.\n");
        //  Inspect LastErrorText privately; it can contain credentials or address data.
        return;
    }

    if (response.get_StatusCode() != 200) {
        wprintf(L"Azure Maps HTTP status: %d\n",response.get_StatusCode());
        wprintf(L"Error code: %s\n",response.getHeaderField(L"x-ms-error-code"));
        if (response.get_StatusCode() == 429) {
            wprintf(L"Retry-After: %s\n",response.getHeaderField(L"Retry-After"));
        }

        //  Check the API version/parameters for 400, and account authentication for 401/403.
        //  For 429 or transient 5xx errors, use bounded backoff and honor Retry-After.
        //  Do not print the full response or request headers in public logs.
        return;
    }

    if (success != true) {
        wprintf(L"The HTTP response could not be read completely.\n");
        return;
    }

    CkJsonObjectW result;
    result.put_Strict(true);
    success = result.Load(response.bodyStr());
    if (success != true) {
        wprintf(L"Expected a well-formed JSON response.\n");
        return;
    }

    if ((result.StringOfEquals(L"type",L"FeatureCollection",true) != true) || (result.JsonTypeOf(L"features") != 4)) {
        wprintf(L"Expected a GeoJSON FeatureCollection with a features array.\n");
        return;
    }

    CkJsonArrayW features;
    success = result.ArrayOf2(L"features",features);
    if (success != true) {
        wprintf(L"Could not read the geocoding candidates.\n");
        return;
    }

    if (features.get_Size() == 0) {
        wprintf(L"No address matches. Check the spelling or provide more address information.\n");
        return;
    }

    //  Display the candidates instead of assuming that the first one is an exact match.
    //  Geocoding finds locations; it does not certify postal deliverability.
    CkJsonObjectW feature;
    int i = 0;
    while ((i < features.get_Size())) {
        success = features.ObjectAt2(i,feature);
        if (success != true) {
            wprintf(L"Invalid geocoding candidate.\n");
            return;
        }

        if ((feature.StringOfEquals(L"geometry.type",L"Point",true) != true) || (feature.JsonTypeOf(L"geometry.coordinates[0]") != 2) || (feature.JsonTypeOf(L"geometry.coordinates[1]") != 2)) {
            wprintf(L"Expected a point with numeric longitude and latitude.\n");
            return;
        }

        if ((feature.JsonTypeOf(L"properties.address.formattedAddress") != 1) || (feature.JsonTypeOf(L"properties.confidence") != 1)) {
            wprintf(L"Expected an address and match confidence.\n");
            return;
        }

        wprintf(L"Candidate %d\n",i + 1);
        wprintf(L"Address: %s\n",feature.stringOf(L"properties.address.formattedAddress"));
        wprintf(L"Confidence: %s\n",feature.stringOf(L"properties.confidence"));
        //  GeoJSON uses LONGITUDE first, LATITUDE second. StringOf preserves numeric text.
        wprintf(L"Longitude: %s\n",feature.stringOf(L"geometry.coordinates[0]"));
        wprintf(L"Latitude: %s\n",feature.stringOf(L"geometry.coordinates[1]"));
        if (feature.JsonTypeOf(L"properties.matchCodes") == 4) {
            wprintf(L"Match codes: %s\n",feature.stringOf(L"properties.matchCodes"));
        }

        //  Review confidence and matchCodes before accepting a candidate automatically.
        //  For routing, inspect properties.geocodePoints for a point whose usageTypes
        //  includes Route; an entrance can differ from the default display point.
        i = i + 1;
    }
    }