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.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global.UnlockBundle for sample code.
local config = chilkat.JsonObject.new()
config.strict = true
-- Private file: {"subscription_key":"YOUR_AZURE_MAPS_KEY"}.
local success = config:load_file("qa_data/azure/maps.json")
if not success then
print("Could not load private Azure Maps configuration.")
return
end
if config:json_type_of("subscription_key") ~= 1 then
print("Missing or invalid subscription_key.")
return
end
if config:string_of_equals("subscription_key", "", true) then
print("A nonempty subscription_key is required.")
return
end
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.follow_redirects = false
http.connect_timeout = 30
http.read_timeout = 90
http.accept = "application/json"
local response = chilkat.HttpResponse.new()
local result = chilkat.JsonObject.new()
result.strict = true
local query = chilkat.JsonObject.new()
-- Current Azure Maps operations accept the key as a header.
http:set_request_header("subscription-key", config:string_of("subscription_key"))
-- Submit between 1 and 100 queries. Split larger input into bounded batches.
local body = chilkat.JsonObject.new()
body:update_number("batchItems[0].coordinates[0]", "-122.128275")
body:update_number("batchItems[0].coordinates[1]", "47.639429")
body:update_number("batchItems[1].coordinates[0]", "-122.341979")
body:update_number("batchItems[1].coordinates[1]", "47.609525")
body:update_string("batchItems[0].resultTypes[0]", "Address")
success = http:http_json("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.status_code == 0 then
print("No HTTP response. Inspect diagnostics privately; they can contain credentials.")
return
end
if response.status_code ~= 200 then
print("HTTP status: " .. response.status_code)
print("Error code: " .. response:get_header_field("x-ms-error-code"))
print("Retry-After: " .. response:get_header_field("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
end
if not success then
print("The HTTP response was incomplete.")
return
end
success = result:load(response.body_str)
if not success then
print("Expected a well-formed JSON response.")
return
end
local items = chilkat.JsonArray.new()
local features = chilkat.JsonArray.new()
local item = chilkat.JsonObject.new()
local feature = chilkat.JsonObject.new()
success = result:array_of2("batchItems", items)
if not success then
print("Expected batchItems array.")
return
end
if items.size ~= body:size_of_array("batchItems") then
print("Batch result count does not match the request.")
return
end
-- 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.
local i = 0
local j = 0
while i < items.size do
success = items:object_at2(i, item)
if not success then
print("Invalid batch item.")
return
end
print("Input index: " .. i)
if item:json_type_of("error") == 3 then
print("Item error code: " .. item:string_of("error.code"))
-- Keep the error message private; it can echo the original input.
else
if not item:string_of_equals("type", "FeatureCollection", true) then
print("Expected a GeoJSON FeatureCollection.")
return
end
success = item:array_of2("features", features)
if not success then
print("Expected a features array.")
return
end
if features.size == 0 then
print("No matching locations for this input.")
end
j = 0
while j < features.size do
success = features:object_at2(j, feature)
if not success then
print("Invalid feature.")
return
end
if feature:json_type_of("properties.address.formattedAddress") ~= 1 then
print("Missing or invalid properties.address.formattedAddress.")
return
end
if feature:json_type_of("geometry.coordinates[0]") ~= 2 then
print("Missing or invalid geometry.coordinates[0].")
return
end
if feature:json_type_of("geometry.coordinates[1]") ~= 2 then
print("Missing or invalid geometry.coordinates[1].")
return
end
print("Address: " .. feature:string_of("properties.address.formattedAddress"))
print("Longitude: " .. feature:string_of("geometry.coordinates[0]"))
print("Latitude: " .. feature:string_of("geometry.coordinates[1]"))
-- Review confidence/matchCodes before accepting a forward-geocoding candidate.
j = j + 1
end
end
i = i + 1
end