Azure Maps: Batch-Geocode Addresses
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.
Use a free-form query or structured address fields in each item, not both. Multiple candidate matches may need application or user review.
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoConfig
Handle hoHttp
Variant vResponse
Handle hoResponse
Handle hoResult
Handle hoQuery
Variant vBody
Handle hoBody
Variant vItems
Handle hoItems
Variant vFeatures
Handle hoFeatures
Variant vItem
Handle hoItem
Variant vFeature
Handle hoFeature
Integer i
Integer j
String sTemp1
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global.UnlockBundle for sample code.
Get Create (RefClass(cComChilkatJsonObject)) To hoConfig
If (Not(IsComObjectCreated(hoConfig))) Begin
Send CreateComObject of hoConfig
End
Set ComStrict Of hoConfig To True
// Private file: {"subscription_key":"YOUR_AZURE_MAPS_KEY"}.
Get ComLoadFile Of hoConfig "qa_data/azure/maps.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private Azure Maps configuration."
Procedure_Return
End
Get ComJsonTypeOf Of hoConfig "subscription_key" To iTemp1
If (iTemp1 <> 1) Begin
Showln "Missing or invalid subscription_key."
Procedure_Return
End
Get ComStringOfEquals Of hoConfig "subscription_key" "" True To bTemp1
If (bTemp1 = True) Begin
Showln "A nonempty subscription_key is required."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Set ComFollowRedirects Of hoHttp To False
Set ComConnectTimeout Of hoHttp To 30
Set ComReadTimeout Of hoHttp To 90
Set ComAccept Of hoHttp To "application/json"
Get Create (RefClass(cComChilkatHttpResponse)) To hoResponse
If (Not(IsComObjectCreated(hoResponse))) Begin
Send CreateComObject of hoResponse
End
Get Create (RefClass(cComChilkatJsonObject)) To hoResult
If (Not(IsComObjectCreated(hoResult))) Begin
Send CreateComObject of hoResult
End
Set ComStrict Of hoResult To True
Get Create (RefClass(cComChilkatJsonObject)) To hoQuery
If (Not(IsComObjectCreated(hoQuery))) Begin
Send CreateComObject of hoQuery
End
// Current Azure Maps operations accept the key as a header.
Get ComStringOf Of hoConfig "subscription_key" To sTemp1
Send ComSetRequestHeader To hoHttp "subscription-key" sTemp1
// Submit between 1 and 100 queries. Split larger input into bounded batches.
Get Create (RefClass(cComChilkatJsonObject)) To hoBody
If (Not(IsComObjectCreated(hoBody))) Begin
Send CreateComObject of hoBody
End
Get ComUpdateString Of hoBody "batchItems[0].query" "1 Microsoft Way, Redmond, WA" To iSuccess
Get ComUpdateInt Of hoBody "batchItems[0].top" 3 To iSuccess
Get ComUpdateString Of hoBody "batchItems[1].query" "Pike Place, Seattle, WA" To iSuccess
Get ComUpdateInt Of hoBody "batchItems[1].top" 3 To iSuccess
Get pvComObject of hoBody to vBody
Get pvComObject of hoResponse to vResponse
Get ComHttpJson Of hoHttp "POST" "https://atlas.microsoft.com/geocode:batch?api-version=2026-01-01" vBody "application/json" vResponse To iSuccess
// Http methods return false for HTTP errors as well as transport failures.
Get ComStatusCode Of hoResponse To iTemp1
If (iTemp1 = 0) Begin
Showln "No HTTP response. Inspect diagnostics privately; they can contain credentials."
Procedure_Return
End
Get ComStatusCode Of hoResponse To iTemp1
If (iTemp1 <> 200) Begin
Get ComStatusCode Of hoResponse To iTemp1
Showln "HTTP status: " iTemp1
Get ComGetHeaderField Of hoResponse "x-ms-error-code" To sTemp1
Showln "Error code: " sTemp1
Get ComGetHeaderField Of hoResponse "Retry-After" To sTemp1
Showln "Retry-After: " sTemp1
// Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
// Keep response bodies and detailed diagnostics out of public logs.
Procedure_Return
End
If (iSuccess <> True) Begin
Showln "The HTTP response was incomplete."
Procedure_Return
End
Get ComBodyStr Of hoResponse To sTemp1
Get ComLoad Of hoResult sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a well-formed JSON response."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonArray)) To hoItems
If (Not(IsComObjectCreated(hoItems))) Begin
Send CreateComObject of hoItems
End
Get Create (RefClass(cComChilkatJsonArray)) To hoFeatures
If (Not(IsComObjectCreated(hoFeatures))) Begin
Send CreateComObject of hoFeatures
End
Get Create (RefClass(cComChilkatJsonObject)) To hoItem
If (Not(IsComObjectCreated(hoItem))) Begin
Send CreateComObject of hoItem
End
Get Create (RefClass(cComChilkatJsonObject)) To hoFeature
If (Not(IsComObjectCreated(hoFeature))) Begin
Send CreateComObject of hoFeature
End
Get pvComObject of hoItems to vItems
Get ComArrayOf2 Of hoResult "batchItems" vItems To iSuccess
If (iSuccess <> True) Begin
Showln "Expected batchItems array."
Procedure_Return
End
Get ComSize Of hoItems To iTemp1
Get ComSizeOfArray Of hoBody "batchItems" To iTemp2
If (iTemp1 <> iTemp2) Begin
Showln "Batch result count does not match the request."
Procedure_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.
Move 0 To i
While (i < (ComSize(hoItems)))
Get pvComObject of hoItem to vItem
Get ComObjectAt2 Of hoItems i vItem To iSuccess
If (iSuccess <> True) Begin
Showln "Invalid batch item."
Procedure_Return
End
Showln "Input index: " i
Get ComJsonTypeOf Of hoItem "error" To iTemp1
If (iTemp1 = 3) Begin
Get ComStringOf Of hoItem "error.code" To sTemp1
Showln "Item error code: " sTemp1
// Keep the error message private; it can echo the original input.
End
Else Begin
Get ComStringOfEquals Of hoItem "type" "FeatureCollection" True To bTemp1
If (bTemp1 <> True) Begin
Showln "Expected a GeoJSON FeatureCollection."
Procedure_Return
End
Get pvComObject of hoFeatures to vFeatures
Get ComArrayOf2 Of hoItem "features" vFeatures To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a features array."
Procedure_Return
End
Get ComSize Of hoFeatures To iTemp1
If (iTemp1 = 0) Begin
Showln "No matching locations for this input."
End
Move 0 To j
While (j < (ComSize(hoFeatures)))
Get pvComObject of hoFeature to vFeature
Get ComObjectAt2 Of hoFeatures j vFeature To iSuccess
If (iSuccess <> True) Begin
Showln "Invalid feature."
Procedure_Return
End
Get ComJsonTypeOf Of hoFeature "properties.address.formattedAddress" To iTemp1
If (iTemp1 <> 1) Begin
Showln "Missing or invalid properties.address.formattedAddress."
Procedure_Return
End
Get ComJsonTypeOf Of hoFeature "geometry.coordinates[0]" To iTemp1
If (iTemp1 <> 2) Begin
Showln "Missing or invalid geometry.coordinates[0]."
Procedure_Return
End
Get ComJsonTypeOf Of hoFeature "geometry.coordinates[1]" To iTemp1
If (iTemp1 <> 2) Begin
Showln "Missing or invalid geometry.coordinates[1]."
Procedure_Return
End
Get ComStringOf Of hoFeature "properties.address.formattedAddress" To sTemp1
Showln "Address: " sTemp1
Get ComStringOf Of hoFeature "geometry.coordinates[0]" To sTemp1
Showln "Longitude: " sTemp1
Get ComStringOf Of hoFeature "geometry.coordinates[1]" To sTemp1
Showln "Latitude: " sTemp1
// Review confidence/matchCodes before accepting a forward-geocoding candidate.
Move (j + 1) To j
Loop
End
Move (i + 1) To i
Loop
End_Procedure