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 Ruby Downloads
require 'chilkat'
success = false
# This example requires the Chilkat API to have been previously unlocked.
# See Global.UnlockBundle for sample code.
config = Chilkat::CkJsonObject.new()
config.put_Strict(true)
# Private file: {"subscription_key":"YOUR_AZURE_MAPS_KEY"}.
success = config.LoadFile("qa_data/azure/maps.json")
if (success != true)
print "Could not load private Azure Maps configuration." + "\n";
exit
end
if (config.JsonTypeOf("subscription_key") != 1)
print "Missing or invalid subscription_key." + "\n";
exit
end
if (config.StringOfEquals("subscription_key","",true) == true)
print "A nonempty subscription_key is required." + "\n";
exit
end
http = Chilkat::CkHttp.new()
http.put_RequireSslCertVerify(true)
http.put_FollowRedirects(false)
http.put_ConnectTimeout(30)
http.put_ReadTimeout(90)
http.put_Accept("application/json")
response = Chilkat::CkHttpResponse.new()
result = Chilkat::CkJsonObject.new()
result.put_Strict(true)
query = Chilkat::CkJsonObject.new()
# 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.
body = Chilkat::CkJsonObject.new()
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.get_StatusCode() == 0)
print "No HTTP response. Inspect diagnostics privately; they can contain credentials." + "\n";
exit
end
if (response.get_StatusCode() != 200)
print "HTTP status: " + response.get_StatusCode().to_s() + "\n";
print "Error code: " + response.getHeaderField("x-ms-error-code") + "\n";
print "Retry-After: " + response.getHeaderField("Retry-After") + "\n";
# Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
# Keep response bodies and detailed diagnostics out of public logs.
exit
end
if (success != true)
print "The HTTP response was incomplete." + "\n";
exit
end
success = result.Load(response.bodyStr())
if (success != true)
print "Expected a well-formed JSON response." + "\n";
exit
end
items = Chilkat::CkJsonArray.new()
features = Chilkat::CkJsonArray.new()
item = Chilkat::CkJsonObject.new()
feature = Chilkat::CkJsonObject.new()
success = result.ArrayOf2("batchItems",items)
if (success != true)
print "Expected batchItems array." + "\n";
exit
end
if (items.get_Size() != body.SizeOfArray("batchItems"))
print "Batch result count does not match the request." + "\n";
exit
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.
i = 0
while (i < items.get_Size())
success = items.ObjectAt2(i,item)
if (success != true)
print "Invalid batch item." + "\n";
exit
end
print "Input index: " + i.to_s() + "\n";
if (item.JsonTypeOf("error") == 3)
print "Item error code: " + item.stringOf("error.code") + "\n";
# Keep the error message private; it can echo the original input.
else
if (item.StringOfEquals("type","FeatureCollection",true) != true)
print "Expected a GeoJSON FeatureCollection." + "\n";
exit
end
success = item.ArrayOf2("features",features)
if (success != true)
print "Expected a features array." + "\n";
exit
end
if (features.get_Size() == 0)
print "No matching locations for this input." + "\n";
end
j = 0
while (j < features.get_Size())
success = features.ObjectAt2(j,feature)
if (success != true)
print "Invalid feature." + "\n";
exit
end
if (feature.JsonTypeOf("properties.address.formattedAddress") != 1)
print "Missing or invalid properties.address.formattedAddress." + "\n";
exit
end
if (feature.JsonTypeOf("geometry.coordinates[0]") != 2)
print "Missing or invalid geometry.coordinates[0]." + "\n";
exit
end
if (feature.JsonTypeOf("geometry.coordinates[1]") != 2)
print "Missing or invalid geometry.coordinates[1]." + "\n";
exit
end
print "Address: " + feature.stringOf("properties.address.formattedAddress") + "\n";
print "Longitude: " + feature.stringOf("geometry.coordinates[0]") + "\n";
print "Latitude: " + feature.stringOf("geometry.coordinates[1]") + "\n";
# Review confidence/matchCodes before accepting a forward-geocoding candidate.
j = j + 1
end
end
i = i + 1
end