Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  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.
set config [new_CkJsonObject]

CkJsonObject_put_Strict $config 1
set success [CkJsonObject_LoadFile $config "qa_data/azure/maps.json"]
if {$success != 1} then {
    puts "Could not load the private Azure Maps configuration."
    delete_CkJsonObject $config
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $config "subscription_key"] != 1]  ||  [[CkJsonObject_StringOfEquals $config "subscription_key" "" 1] == 1]} then {
    puts "A nonempty subscription_key string is required."
    delete_CkJsonObject $config
    exit
}

set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_FollowRedirects $http 0
CkHttp_put_ConnectTimeout $http 30
CkHttp_put_ReadTimeout $http 60
CkHttp_put_Accept $http "application/geo+json"
#  The current Geocoding API accepts this key as a header, avoiding a key in the URL.
CkHttp_SetRequestHeader $http "subscription-key" [CkJsonObject_stringOf $config "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.
set query [new_CkJsonObject]

CkJsonObject_UpdateString $query "api-version" "2026-01-01"
CkJsonObject_UpdateString $query "query" "15127 NE 24th Street, Redmond, WA 98052"
CkJsonObject_UpdateInt $query "top" 5

set response [new_CkHttpResponse]

set success [CkHttp_HttpParams $http "GET" "https://atlas.microsoft.com/geocode" $query $response]
#  HttpParams also returns false for HTTP errors. Status 0 means no HTTP response.
if {[CkHttpResponse_get_StatusCode $response] == 0} then {
    puts "Azure Maps request did not receive an HTTP response."
    #  Inspect LastErrorText privately; it can contain credentials or address data.
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkJsonObject $query
    delete_CkHttpResponse $response
    exit
}

if {[CkHttpResponse_get_StatusCode $response] != 200} then {
    puts "Azure Maps HTTP status: [CkHttpResponse_get_StatusCode $response]"
    puts "Error code: [CkHttpResponse_getHeaderField $response x-ms-error-code]"
    if {[CkHttpResponse_get_StatusCode $response] == 429} then {
        puts "Retry-After: [CkHttpResponse_getHeaderField $response 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.
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkJsonObject $query
    delete_CkHttpResponse $response
    exit
}

if {$success != 1} then {
    puts "The HTTP response could not be read completely."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkJsonObject $query
    delete_CkHttpResponse $response
    exit
}

set result [new_CkJsonObject]

CkJsonObject_put_Strict $result 1
set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $response]]
if {$success != 1} then {
    puts "Expected a well-formed JSON response."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkJsonObject $query
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    exit
}

if {expr [[CkJsonObject_StringOfEquals $result "type" "FeatureCollection" 1] != 1]  ||  [[CkJsonObject_JsonTypeOf $result "features"] != 4]} then {
    puts "Expected a GeoJSON FeatureCollection with a features array."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkJsonObject $query
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    exit
}

set features [new_CkJsonArray]

set success [CkJsonObject_ArrayOf2 $result "features" $features]
if {$success != 1} then {
    puts "Could not read the geocoding candidates."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkJsonObject $query
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonArray $features
    exit
}

if {[CkJsonArray_get_Size $features] == 0} then {
    puts "No address matches. Check the spelling or provide more address information."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkJsonObject $query
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonArray $features
    exit
}

#  Display the candidates instead of assuming that the first one is an exact match.
#  Geocoding finds locations; it does not certify postal deliverability.
set feature [new_CkJsonObject]

set i 0
while {$i < [CkJsonArray_get_Size $features]} {
    set success [CkJsonArray_ObjectAt2 $features $i $feature]
    if {$success != 1} then {
        puts "Invalid geocoding candidate."
        delete_CkJsonObject $config
        delete_CkHttp $http
        delete_CkJsonObject $query
        delete_CkHttpResponse $response
        delete_CkJsonObject $result
        delete_CkJsonArray $features
        delete_CkJsonObject $feature
        exit
    }

    if {expr [[CkJsonObject_StringOfEquals $feature "geometry.type" "Point" 1] != 1]  ||  [expr [[CkJsonObject_JsonTypeOf $feature "geometry.coordinates[0]"] != 2]  ||  [[CkJsonObject_JsonTypeOf $feature "geometry.coordinates[1]"] != 2]]} then {
        puts "Expected a point with numeric longitude and latitude."
        delete_CkJsonObject $config
        delete_CkHttp $http
        delete_CkJsonObject $query
        delete_CkHttpResponse $response
        delete_CkJsonObject $result
        delete_CkJsonArray $features
        delete_CkJsonObject $feature
        exit
    }

    if {expr [[CkJsonObject_JsonTypeOf $feature "properties.address.formattedAddress"] != 1]  ||  [[CkJsonObject_JsonTypeOf $feature "properties.confidence"] != 1]} then {
        puts "Expected an address and match confidence."
        delete_CkJsonObject $config
        delete_CkHttp $http
        delete_CkJsonObject $query
        delete_CkHttpResponse $response
        delete_CkJsonObject $result
        delete_CkJsonArray $features
        delete_CkJsonObject $feature
        exit
    }

    puts "Candidate [expr $i + 1]"
    puts "Address: [CkJsonObject_stringOf $feature properties.address.formattedAddress]"
    puts "Confidence: [CkJsonObject_stringOf $feature properties.confidence]"
    #  GeoJSON uses LONGITUDE first, LATITUDE second. StringOf preserves numeric text.
    puts "Longitude: [CkJsonObject_stringOf $feature {geometry.coordinates[0]}]"
    puts "Latitude: [CkJsonObject_stringOf $feature {geometry.coordinates[1]}]"
    if {[CkJsonObject_JsonTypeOf $feature "properties.matchCodes"] == 4} then {
        puts "Match codes: [CkJsonObject_stringOf $feature 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.
    set i [expr $i + 1]
}

delete_CkJsonObject $config
delete_CkHttp $http
delete_CkJsonObject $query
delete_CkHttpResponse $response
delete_CkJsonObject $result
delete_CkJsonArray $features
delete_CkJsonObject $feature