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

Visual FoxPro
LOCAL lnSuccess
LOCAL loConfig
LOCAL loHttp
LOCAL loQuery
LOCAL loResponse
LOCAL loResult
LOCAL loFeatures
LOCAL loFeature
LOCAL i

lnSuccess = 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.
loConfig = CreateObject('Chilkat.JsonObject')
loConfig.Strict = 1
lnSuccess = loConfig.LoadFile("qa_data/azure/maps.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load the private Azure Maps configuration."
    RELEASE loConfig
    CANCEL
ENDIF

IF ((loConfig.JsonTypeOf("subscription_key") <> 1) OR (loConfig.StringOfEquals("subscription_key","",1) = 1)) THEN
    ? "A nonempty subscription_key string is required."
    RELEASE loConfig
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
loHttp.ConnectTimeout = 30
loHttp.ReadTimeout = 60
loHttp.Accept = "application/geo+json"
*  The current Geocoding API accepts this key as a header, avoiding a key in the URL.
loHttp.SetRequestHeader("subscription-key",loConfig.StringOf("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.
loQuery = CreateObject('Chilkat.JsonObject')
loQuery.UpdateString("api-version","2026-01-01")
loQuery.UpdateString("query","15127 NE 24th Street, Redmond, WA 98052")
loQuery.UpdateInt("top",5)

loResponse = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpParams("GET","https://atlas.microsoft.com/geocode",loQuery,loResponse)
*  HttpParams also returns false for HTTP errors. Status 0 means no HTTP response.
IF (loResponse.StatusCode = 0) THEN
    ? "Azure Maps request did not receive an HTTP response."
    *  Inspect LastErrorText privately; it can contain credentials or address data.
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loQuery
    RELEASE loResponse
    CANCEL
ENDIF

IF (loResponse.StatusCode <> 200) THEN
    ? "Azure Maps HTTP status: " + STR(loResponse.StatusCode)
    ? "Error code: " + loResponse.GetHeaderField("x-ms-error-code")
    IF (loResponse.StatusCode = 429) THEN
        ? "Retry-After: " + loResponse.GetHeaderField("Retry-After")
    ENDIF

    *  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.
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loQuery
    RELEASE loResponse
    CANCEL
ENDIF

IF (lnSuccess <> 1) THEN
    ? "The HTTP response could not be read completely."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loQuery
    RELEASE loResponse
    CANCEL
ENDIF

loResult = CreateObject('Chilkat.JsonObject')
loResult.Strict = 1
lnSuccess = loResult.Load(loResponse.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a well-formed JSON response."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loQuery
    RELEASE loResponse
    RELEASE loResult
    CANCEL
ENDIF

IF ((loResult.StringOfEquals("type","FeatureCollection",1) <> 1) OR (loResult.JsonTypeOf("features") <> 4)) THEN
    ? "Expected a GeoJSON FeatureCollection with a features array."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loQuery
    RELEASE loResponse
    RELEASE loResult
    CANCEL
ENDIF

loFeatures = CreateObject('Chilkat.JsonArray')
lnSuccess = loResult.ArrayOf2("features",loFeatures)
IF (lnSuccess <> 1) THEN
    ? "Could not read the geocoding candidates."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loQuery
    RELEASE loResponse
    RELEASE loResult
    RELEASE loFeatures
    CANCEL
ENDIF

IF (loFeatures.Size = 0) THEN
    ? "No address matches. Check the spelling or provide more address information."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loQuery
    RELEASE loResponse
    RELEASE loResult
    RELEASE loFeatures
    CANCEL
ENDIF

*  Display the candidates instead of assuming that the first one is an exact match.
*  Geocoding finds locations; it does not certify postal deliverability.
loFeature = CreateObject('Chilkat.JsonObject')
i = 0
DO WHILE (i < loFeatures.Size)
    lnSuccess = loFeatures.ObjectAt2(i,loFeature)
    IF (lnSuccess <> 1) THEN
        ? "Invalid geocoding candidate."
        RELEASE loConfig
        RELEASE loHttp
        RELEASE loQuery
        RELEASE loResponse
        RELEASE loResult
        RELEASE loFeatures
        RELEASE loFeature
        CANCEL
    ENDIF

    IF ((loFeature.StringOfEquals("geometry.type","Point",1) <> 1) OR (loFeature.JsonTypeOf("geometry.coordinates[0]") <> 2) OR (loFeature.JsonTypeOf("geometry.coordinates[1]") <> 2)) THEN
        ? "Expected a point with numeric longitude and latitude."
        RELEASE loConfig
        RELEASE loHttp
        RELEASE loQuery
        RELEASE loResponse
        RELEASE loResult
        RELEASE loFeatures
        RELEASE loFeature
        CANCEL
    ENDIF

    IF ((loFeature.JsonTypeOf("properties.address.formattedAddress") <> 1) OR (loFeature.JsonTypeOf("properties.confidence") <> 1)) THEN
        ? "Expected an address and match confidence."
        RELEASE loConfig
        RELEASE loHttp
        RELEASE loQuery
        RELEASE loResponse
        RELEASE loResult
        RELEASE loFeatures
        RELEASE loFeature
        CANCEL
    ENDIF

    ? "Candidate " + STR(i + 1)
    ? "Address: " + loFeature.StringOf("properties.address.formattedAddress")
    ? "Confidence: " + loFeature.StringOf("properties.confidence")
    *  GeoJSON uses LONGITUDE first, LATITUDE second. StringOf preserves numeric text.
    ? "Longitude: " + loFeature.StringOf("geometry.coordinates[0]")
    ? "Latitude: " + loFeature.StringOf("geometry.coordinates[1]")
    IF (loFeature.JsonTypeOf("properties.matchCodes") = 4) THEN
        ? "Match codes: " + loFeature.StringOf("properties.matchCodes")
    ENDIF

    *  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.
    i = i + 1
ENDDO

RELEASE loConfig
RELEASE loHttp
RELEASE loQuery
RELEASE loResponse
RELEASE loResult
RELEASE loFeatures
RELEASE loFeature