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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoConfig
    Handle hoHttp
    Variant vQuery
    Handle hoQuery
    Variant vResponse
    Handle hoResponse
    Handle hoResult
    Variant vFeatures
    Handle hoFeatures
    Variant vFeature
    Handle hoFeature
    Integer i
    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.

    //  Private configuration: {"subscription_key":"YOUR_AZURE_MAPS_KEY"}.
    //  Keep this file outside source control and public web directories.
    Get Create (RefClass(cComChilkatJsonObject)) To hoConfig
    If (Not(IsComObjectCreated(hoConfig))) Begin
        Send CreateComObject of hoConfig
    End
    Set ComStrict Of hoConfig To True
    Get ComLoadFile Of hoConfig "qa_data/azure/maps.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load the private Azure Maps configuration."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoConfig "subscription_key" To iTemp1
    Get ComStringOfEquals Of hoConfig "subscription_key" "" True To bTemp1
    If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
        Showln "A nonempty subscription_key string 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 60
    Set ComAccept Of hoHttp To "application/geo+json"
    //  The current Geocoding API accepts this key as a header, avoiding a key in the URL.
    Get ComStringOf Of hoConfig "subscription_key" To sTemp1
    Send ComSetRequestHeader To hoHttp "subscription-key" sTemp1

    //  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.
    Get Create (RefClass(cComChilkatJsonObject)) To hoQuery
    If (Not(IsComObjectCreated(hoQuery))) Begin
        Send CreateComObject of hoQuery
    End
    Get ComUpdateString Of hoQuery "api-version" "2026-01-01" To iSuccess
    Get ComUpdateString Of hoQuery "query" "15127 NE 24th Street, Redmond, WA 98052" To iSuccess
    Get ComUpdateInt Of hoQuery "top" 5 To iSuccess

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResponse
    If (Not(IsComObjectCreated(hoResponse))) Begin
        Send CreateComObject of hoResponse
    End
    Get pvComObject of hoQuery to vQuery
    Get pvComObject of hoResponse to vResponse
    Get ComHttpParams Of hoHttp "GET" "https://atlas.microsoft.com/geocode" vQuery vResponse To iSuccess
    //  HttpParams also returns false for HTTP errors. Status 0 means no HTTP response.
    Get ComStatusCode Of hoResponse To iTemp1
    If (iTemp1 = 0) Begin
        Showln "Azure Maps request did not receive an HTTP response."
        //  Inspect LastErrorText privately; it can contain credentials or address data.
        Procedure_Return
    End

    Get ComStatusCode Of hoResponse To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComStatusCode Of hoResponse To iTemp1
        Showln "Azure Maps HTTP status: " iTemp1
        Get ComGetHeaderField Of hoResponse "x-ms-error-code" To sTemp1
        Showln "Error code: " sTemp1
        Get ComStatusCode Of hoResponse To iTemp1
        If (iTemp1 = 429) Begin
            Get ComGetHeaderField Of hoResponse "Retry-After" To sTemp1
            Showln "Retry-After: " sTemp1
        End

        //  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.
        Procedure_Return
    End

    If (iSuccess <> True) Begin
        Showln "The HTTP response could not be read completely."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoResult
    If (Not(IsComObjectCreated(hoResult))) Begin
        Send CreateComObject of hoResult
    End
    Set ComStrict Of hoResult To True
    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 ComStringOfEquals Of hoResult "type" "FeatureCollection" True To bTemp1
    Get ComJsonTypeOf Of hoResult "features" To iTemp1
    If ((bTemp1 <> True) Or (iTemp1 <> 4)) Begin
        Showln "Expected a GeoJSON FeatureCollection with a features array."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonArray)) To hoFeatures
    If (Not(IsComObjectCreated(hoFeatures))) Begin
        Send CreateComObject of hoFeatures
    End
    Get pvComObject of hoFeatures to vFeatures
    Get ComArrayOf2 Of hoResult "features" vFeatures To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not read the geocoding candidates."
        Procedure_Return
    End

    Get ComSize Of hoFeatures To iTemp1
    If (iTemp1 = 0) Begin
        Showln "No address matches. Check the spelling or provide more address information."
        Procedure_Return
    End

    //  Display the candidates instead of assuming that the first one is an exact match.
    //  Geocoding finds locations; it does not certify postal deliverability.
    Get Create (RefClass(cComChilkatJsonObject)) To hoFeature
    If (Not(IsComObjectCreated(hoFeature))) Begin
        Send CreateComObject of hoFeature
    End
    Move 0 To i
    While (i < (ComSize(hoFeatures)))
        Get pvComObject of hoFeature to vFeature
        Get ComObjectAt2 Of hoFeatures i vFeature To iSuccess
        If (iSuccess <> True) Begin
            Showln "Invalid geocoding candidate."
            Procedure_Return
        End

        Get ComStringOfEquals Of hoFeature "geometry.type" "Point" True To bTemp1
        Get ComJsonTypeOf Of hoFeature "geometry.coordinates[0]" To iTemp1
        Get ComJsonTypeOf Of hoFeature "geometry.coordinates[1]" To iTemp2
        If ((bTemp1 <> True) Or (iTemp1 <> 2) Or (iTemp2 <> 2)) Begin
            Showln "Expected a point with numeric longitude and latitude."
            Procedure_Return
        End

        Get ComJsonTypeOf Of hoFeature "properties.address.formattedAddress" To iTemp1
        Get ComJsonTypeOf Of hoFeature "properties.confidence" To iTemp2
        If ((iTemp1 <> 1) Or (iTemp2 <> 1)) Begin
            Showln "Expected an address and match confidence."
            Procedure_Return
        End

        Showln "Candidate " (i + 1)
        Get ComStringOf Of hoFeature "properties.address.formattedAddress" To sTemp1
        Showln "Address: " sTemp1
        Get ComStringOf Of hoFeature "properties.confidence" To sTemp1
        Showln "Confidence: " sTemp1
        //  GeoJSON uses LONGITUDE first, LATITUDE second. StringOf preserves numeric text.
        Get ComStringOf Of hoFeature "geometry.coordinates[0]" To sTemp1
        Showln "Longitude: " sTemp1
        Get ComStringOf Of hoFeature "geometry.coordinates[1]" To sTemp1
        Showln "Latitude: " sTemp1
        Get ComJsonTypeOf Of hoFeature "properties.matchCodes" To iTemp1
        If (iTemp1 = 4) Begin
            Get ComStringOf Of hoFeature "properties.matchCodes" To sTemp1
            Showln "Match codes: " sTemp1
        End

        //  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.
        Move (i + 1) To i
    Loop



End_Procedure