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 SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @iTmp2 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @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.
DECLARE @config int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @config OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @config, 'Strict', 1
EXEC sp_OAMethod @config, 'LoadFile', @success OUT, 'qa_data/azure/maps.json'
IF @success <> 1
BEGIN
PRINT 'Could not load the private Azure Maps configuration.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'subscription_key'
EXEC sp_OAMethod @config, 'StringOfEquals', @iTmp1 OUT, 'subscription_key', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'A nonempty subscription_key string is required.'
EXEC @hr = sp_OADestroy @config
RETURN
END
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'FollowRedirects', 0
EXEC sp_OASetProperty @http, 'ConnectTimeout', 30
EXEC sp_OASetProperty @http, 'ReadTimeout', 60
EXEC sp_OASetProperty @http, 'Accept', 'application/geo+json'
-- The current Geocoding API accepts this key as a header, avoiding a key in the URL.
EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'subscription_key'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'subscription-key', @sTmp0
-- 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.
DECLARE @query int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @query OUT
EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'api-version', '2026-01-01'
EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'query', '15127 NE 24th Street, Redmond, WA 98052'
EXEC sp_OAMethod @query, 'UpdateInt', @success OUT, 'top', 5
DECLARE @response int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @response OUT
EXEC sp_OAMethod @http, 'HttpParams', @success OUT, 'GET', 'https://atlas.microsoft.com/geocode', @query, @response
-- HttpParams also returns false for HTTP errors. Status 0 means no HTTP response.
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'Azure Maps request did not receive an HTTP response.'
-- Inspect LastErrorText privately; it can contain credentials or address data.
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @response
RETURN
END
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
PRINT 'Azure Maps HTTP status: ' + @iTmp0
EXEC sp_OAMethod @response, 'GetHeaderField', @sTmp0 OUT, 'x-ms-error-code'
PRINT 'Error code: ' + @sTmp0
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 429
BEGIN
EXEC sp_OAMethod @response, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
PRINT 'Retry-After: ' + @sTmp0
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.
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @response
RETURN
END
IF @success <> 1
BEGIN
PRINT 'The HTTP response could not be read completely.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @response
RETURN
END
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
EXEC sp_OASetProperty @result, 'Strict', 1
EXEC sp_OAGetProperty @response, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Expected a well-formed JSON response.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp0 OUT, 'type', 'FeatureCollection', 1
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp1 OUT, 'features'
IF (@iTmp0 <> 1) or (@iTmp1 <> 4)
BEGIN
PRINT 'Expected a GeoJSON FeatureCollection with a features array.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
RETURN
END
DECLARE @features int
EXEC @hr = sp_OACreate 'Chilkat.JsonArray', @features OUT
EXEC sp_OAMethod @result, 'ArrayOf2', @success OUT, 'features', @features
IF @success <> 1
BEGIN
PRINT 'Could not read the geocoding candidates.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @features
RETURN
END
EXEC sp_OAGetProperty @features, 'Size', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'No address matches. Check the spelling or provide more address information.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @features
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.
DECLARE @feature int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @feature OUT
DECLARE @i int
SELECT @i = 0
EXEC sp_OAGetProperty @features, 'Size', @iTmp0 OUT
WHILE (@i < @iTmp0)
BEGIN
EXEC sp_OAMethod @features, 'ObjectAt2', @success OUT, @i, @feature
IF @success <> 1
BEGIN
PRINT 'Invalid geocoding candidate.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @features
EXEC @hr = sp_OADestroy @feature
RETURN
END
EXEC sp_OAMethod @feature, 'StringOfEquals', @iTmp0 OUT, 'geometry.type', 'Point', 1
EXEC sp_OAMethod @feature, 'JsonTypeOf', @iTmp1 OUT, 'geometry.coordinates[0]'
EXEC sp_OAMethod @feature, 'JsonTypeOf', @iTmp2 OUT, 'geometry.coordinates[1]'
IF (@iTmp0 <> 1) or (@iTmp1 <> 2) or (@iTmp2 <> 2)
BEGIN
PRINT 'Expected a point with numeric longitude and latitude.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @features
EXEC @hr = sp_OADestroy @feature
RETURN
END
EXEC sp_OAMethod @feature, 'JsonTypeOf', @iTmp0 OUT, 'properties.address.formattedAddress'
EXEC sp_OAMethod @feature, 'JsonTypeOf', @iTmp1 OUT, 'properties.confidence'
IF (@iTmp0 <> 1) or (@iTmp1 <> 1)
BEGIN
PRINT 'Expected an address and match confidence.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @features
EXEC @hr = sp_OADestroy @feature
RETURN
END
PRINT 'Candidate ' + @i + 1
EXEC sp_OAMethod @feature, 'StringOf', @sTmp0 OUT, 'properties.address.formattedAddress'
PRINT 'Address: ' + @sTmp0
EXEC sp_OAMethod @feature, 'StringOf', @sTmp0 OUT, 'properties.confidence'
PRINT 'Confidence: ' + @sTmp0
-- GeoJSON uses LONGITUDE first, LATITUDE second. StringOf preserves numeric text.
EXEC sp_OAMethod @feature, 'StringOf', @sTmp0 OUT, 'geometry.coordinates[0]'
PRINT 'Longitude: ' + @sTmp0
EXEC sp_OAMethod @feature, 'StringOf', @sTmp0 OUT, 'geometry.coordinates[1]'
PRINT 'Latitude: ' + @sTmp0
EXEC sp_OAMethod @feature, 'JsonTypeOf', @iTmp0 OUT, 'properties.matchCodes'
IF @iTmp0 = 4
BEGIN
EXEC sp_OAMethod @feature, 'StringOf', @sTmp0 OUT, 'properties.matchCodes'
PRINT 'Match codes: ' + @sTmp0
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.
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @features
EXEC @hr = sp_OADestroy @feature
END
GO