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

Azure Maps: Batch Reverse-Geocode Coordinates

See more Azure Maps Examples

Submits two queries with a direct JSON POST. The synchronous batch API accepts 1–100 items and returns one result per input, in input order. Handle item errors and empty matches even when the overall HTTP status is 200. Retain your local input-to-index mapping when processing a larger batch.

The service can return HTTP 408 if processing exceeds its synchronous time limit. Split larger workloads into bounded batches and retry only the necessary work. There is no later result-retrieval URL for this synchronous operation.

Coordinates in the JSON arrays use longitude, latitude order.

Uses direct Chilkat.Http calls to the Azure public-cloud endpoint. Supply a nonempty subscription_key string in private qa_data/azure/maps.json. Shared-key authentication must be enabled on the Maps account. Keep this file outside source control and public web directories, and do not embed account keys in publicly distributed applications. Microsoft Entra ID is an alternative.

Query parameters are supplied as raw text for Chilkat to encode. The code checks transport status, HTTP status, and JSON parsing separately. For throttling or temporary service failures, use bounded retries and honor Retry-After. HTTP diagnostics can contain credentials and location data; inspect them privately. Calls use verified HTTPS and do not follow redirects.

Microsoft API reference

Chilkat C# Downloads

C#
bool success = false;

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

Chilkat.JsonObject config = new Chilkat.JsonObject();
config.Strict = true;
//  Private file: {"subscription_key":"YOUR_AZURE_MAPS_KEY"}.
success = config.LoadFile("qa_data/azure/maps.json");
if (success != true) {
    Debug.WriteLine("Could not load private Azure Maps configuration.");
    return;
}

if (config.JsonTypeOf("subscription_key") != 1) {
    Debug.WriteLine("Missing or invalid subscription_key.");
    return;
}

if (config.StringOfEquals("subscription_key","",true) == true) {
    Debug.WriteLine("A nonempty subscription_key is required.");
    return;
}

Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.FollowRedirects = false;
http.ConnectTimeout = 30;
http.ReadTimeout = 90;
http.Accept = "application/json";
Chilkat.HttpResponse response = new Chilkat.HttpResponse();
Chilkat.JsonObject result = new Chilkat.JsonObject();
result.Strict = true;
Chilkat.JsonObject query = new Chilkat.JsonObject();
//  Current Azure Maps operations accept the key as a header.
http.SetRequestHeader("subscription-key",config.StringOf("subscription_key"));
//  Submit between 1 and 100 queries. Split larger input into bounded batches.
Chilkat.JsonObject body = new Chilkat.JsonObject();
body.UpdateNumber("batchItems[0].coordinates[0]","-122.128275");
body.UpdateNumber("batchItems[0].coordinates[1]","47.639429");
body.UpdateNumber("batchItems[1].coordinates[0]","-122.341979");
body.UpdateNumber("batchItems[1].coordinates[1]","47.609525");
body.UpdateString("batchItems[0].resultTypes[0]","Address");
success = http.HttpJson("POST","https://atlas.microsoft.com/reverseGeocode:batch?api-version=2026-01-01",body,"application/json",response);
//  Http methods return false for HTTP errors as well as transport failures.
if (response.StatusCode == 0) {
    Debug.WriteLine("No HTTP response. Inspect diagnostics privately; they can contain credentials.");
    return;
}

if (response.StatusCode != 200) {
    Debug.WriteLine("HTTP status: " + Convert.ToString(response.StatusCode));
    Debug.WriteLine("Error code: " + response.GetHeaderField("x-ms-error-code"));
    Debug.WriteLine("Retry-After: " + response.GetHeaderField("Retry-After"));
    //  Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
    //  Keep response bodies and detailed diagnostics out of public logs.
    return;
}

if (success != true) {
    Debug.WriteLine("The HTTP response was incomplete.");
    return;
}

success = result.Load(response.BodyStr);
if (success != true) {
    Debug.WriteLine("Expected a well-formed JSON response.");
    return;
}

Chilkat.JsonArray items = new Chilkat.JsonArray();
Chilkat.JsonArray features = new Chilkat.JsonArray();
Chilkat.JsonObject item = new Chilkat.JsonObject();
Chilkat.JsonObject feature = new Chilkat.JsonObject();
success = result.ArrayOf2("batchItems",items);
if (success != true) {
    Debug.WriteLine("Expected batchItems array.");
    return;
}

if (items.Size != body.SizeOfArray("batchItems")) {
    Debug.WriteLine("Batch result count does not match the request.");
    return;
}

//  A successful HTTP response can still contain failed individual queries.
//  Match each result to the request at the same index; do not reorder the arrays.
int i = 0;
int j;
while ((i < items.Size)) {
    success = items.ObjectAt2(i,item);
    if (success != true) {
        Debug.WriteLine("Invalid batch item.");
        return;
    }

    Debug.WriteLine("Input index: " + Convert.ToString(i));
    if (item.JsonTypeOf("error") == 3) {
        Debug.WriteLine("Item error code: " + item.StringOf("error.code"));
        //  Keep the error message private; it can echo the original input.
    }
    else {
        if (item.StringOfEquals("type","FeatureCollection",true) != true) {
            Debug.WriteLine("Expected a GeoJSON FeatureCollection.");
            return;
        }

        success = item.ArrayOf2("features",features);
        if (success != true) {
            Debug.WriteLine("Expected a features array.");
            return;
        }

        if (features.Size == 0) {
            Debug.WriteLine("No matching locations for this input.");
        }

        j = 0;
        while ((j < features.Size)) {
            success = features.ObjectAt2(j,feature);
            if (success != true) {
                Debug.WriteLine("Invalid feature.");
                return;
            }

            if (feature.JsonTypeOf("properties.address.formattedAddress") != 1) {
                Debug.WriteLine("Missing or invalid properties.address.formattedAddress.");
                return;
            }

            if (feature.JsonTypeOf("geometry.coordinates[0]") != 2) {
                Debug.WriteLine("Missing or invalid geometry.coordinates[0].");
                return;
            }

            if (feature.JsonTypeOf("geometry.coordinates[1]") != 2) {
                Debug.WriteLine("Missing or invalid geometry.coordinates[1].");
                return;
            }

            Debug.WriteLine("Address: " + feature.StringOf("properties.address.formattedAddress"));
            Debug.WriteLine("Longitude: " + feature.StringOf("geometry.coordinates[0]"));
            Debug.WriteLine("Latitude: " + feature.StringOf("geometry.coordinates[1]"));
            //  Review confidence/matchCodes before accepting a forward-geocoding candidate.
            j = j + 1;
        }

    }

    i = i + 1;
}