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 Lua Downloads
local chilkat = require("chilkat")
-- 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.
local config = chilkat.JsonObject.new()
config.strict = true
local success = config:load_file("qa_data/azure/maps.json")
if not success then
print("Could not load the private Azure Maps configuration.")
return
end
if (config:json_type_of("subscription_key") ~= 1) or (config:string_of_equals("subscription_key", "", true)) then
print("A nonempty subscription_key string is required.")
return
end
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.follow_redirects = false
http.connect_timeout = 30
http.read_timeout = 60
http.accept = "application/geo+json"
-- The current Geocoding API accepts this key as a header, avoiding a key in the URL.
http:set_request_header("subscription-key", config:string_of("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.
local query = chilkat.JsonObject.new()
query:update_string("api-version", "2026-01-01")
query:update_string("query", "15127 NE 24th Street, Redmond, WA 98052")
query:update_int("top", 5)
local response = chilkat.HttpResponse.new()
success = http:http_params("GET", "https://atlas.microsoft.com/geocode", query, response)
-- HttpParams also returns false for HTTP errors. Status 0 means no HTTP response.
if response.status_code == 0 then
print("Azure Maps request did not receive an HTTP response.")
-- Inspect LastErrorText privately; it can contain credentials or address data.
return
end
if response.status_code ~= 200 then
print("Azure Maps HTTP status: " .. response.status_code)
print("Error code: " .. response:get_header_field("x-ms-error-code"))
if response.status_code == 429 then
print("Retry-After: " .. response:get_header_field("Retry-After"))
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.
return
end
if not success then
print("The HTTP response could not be read completely.")
return
end
local result = chilkat.JsonObject.new()
result.strict = true
success = result:load(response.body_str)
if not success then
print("Expected a well-formed JSON response.")
return
end
if (not result:string_of_equals("type", "FeatureCollection", true)) or (result:json_type_of("features") ~= 4) then
print("Expected a GeoJSON FeatureCollection with a features array.")
return
end
local features = chilkat.JsonArray.new()
success = result:array_of2("features", features)
if not success then
print("Could not read the geocoding candidates.")
return
end
if features.size == 0 then
print("No address matches. Check the spelling or provide more address information.")
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.
local feature = chilkat.JsonObject.new()
local i = 0
while i < features.size do
success = features:object_at2(i, feature)
if not success then
print("Invalid geocoding candidate.")
return
end
if (not feature:string_of_equals("geometry.type", "Point", true)) or ((feature:json_type_of("geometry.coordinates[0]") ~= 2) or (feature:json_type_of("geometry.coordinates[1]") ~= 2)) then
print("Expected a point with numeric longitude and latitude.")
return
end
if (feature:json_type_of("properties.address.formattedAddress") ~= 1) or (feature:json_type_of("properties.confidence") ~= 1) then
print("Expected an address and match confidence.")
return
end
print("Candidate " .. (i + 1))
print("Address: " .. feature:string_of("properties.address.formattedAddress"))
print("Confidence: " .. feature:string_of("properties.confidence"))
-- GeoJSON uses LONGITUDE first, LATITUDE second. StringOf preserves numeric text.
print("Longitude: " .. feature:string_of("geometry.coordinates[0]"))
print("Latitude: " .. feature:string_of("geometry.coordinates[1]"))
if feature:json_type_of("properties.matchCodes") == 4 then
print("Match codes: " .. feature:string_of("properties.matchCodes"))
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.
i = i + 1
end