Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.6.0+

Azure Maps: Download a Static Map with a Marker

See more Azure Maps Examples

Downloads a PNG map using Render 2024-04-01. Sets center, zoom, image dimensions, road tileset, and a numbered marker. Chilkat encodes the marker syntax as a query value.

The response is saved as binary only after checking HTTP status and image content type. The file azure-maps-static.png is written in the current directory, replacing an existing file. Preserve attribution and follow Azure Maps display terms when embedding the image. Static maps do not require an interactive map SDK.

Uses direct Chilkat.Http calls to the Azure public-cloud endpoint. Supply a nonempty subscription_key string in private qa_data/azure/maps.json. Shared-key authentication must be enabled on the Maps account. Keep this file outside source control and public web directories, and do not embed account keys in publicly distributed applications. Microsoft Entra ID is an alternative.

Query parameters are supplied as raw text for Chilkat to encode. The code checks transport status, HTTP status, and JSON parsing separately. For throttling or temporary service failures, use bounded retries and honor Retry-After. HTTP diagnostics can contain credentials and location data; inspect them privately. Calls use verified HTTPS and do not follow redirects.

Microsoft API reference

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 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.

    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
    --  Private file: {"subscription_key":"YOUR_AZURE_MAPS_KEY"}.
    EXEC sp_OAMethod @config, 'LoadFile', @success OUT, 'qa_data/azure/maps.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load private Azure Maps configuration.'
        EXEC @hr = sp_OADestroy @config
        RETURN
      END
    EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'subscription_key'
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'Missing or invalid subscription_key.'
        EXEC @hr = sp_OADestroy @config
        RETURN
      END
    EXEC sp_OAMethod @config, 'StringOfEquals', @iTmp0 OUT, 'subscription_key', '', 1
    IF @iTmp0 = 1
      BEGIN

        PRINT 'A nonempty subscription_key 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', 90
    EXEC sp_OASetProperty @http, 'Accept', 'application/json'
    DECLARE @response int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @response OUT

    DECLARE @result int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT

    EXEC sp_OASetProperty @result, 'Strict', 1
    DECLARE @query int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @query OUT

    --  Current Azure Maps operations accept the key as a header.
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'subscription_key'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'subscription-key', @sTmp0
    EXEC sp_OASetProperty @http, 'Accept', 'image/png'
    --  Static map center and pin coordinates use longitude, latitude.
    EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'api-version', '2024-04-01'
    EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'tilesetId', 'microsoft.base.road'
    EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'center', '-122.136833,47.642282'
    EXEC sp_OAMethod @query, 'UpdateInt', @success OUT, 'zoom', 14
    EXEC sp_OAMethod @query, 'UpdateInt', @success OUT, 'width', 800
    EXEC sp_OAMethod @query, 'UpdateInt', @success OUT, 'height', 500
    EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'pins', 'default||''1''-122.136833 47.642282'
    EXEC sp_OAMethod @http, 'HttpParams', @success OUT, 'GET', 'https://atlas.microsoft.com/map/static', @query, @response
    --  Http methods return false for HTTP errors as well as transport failures.
    EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN

        PRINT 'No HTTP response. Inspect diagnostics privately; they can contain credentials.'
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @response
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @query
        RETURN
      END
    EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
        PRINT 'HTTP status: ' + @iTmp0

        EXEC sp_OAMethod @response, 'GetHeaderField', @sTmp0 OUT, 'x-ms-error-code'
        PRINT 'Error code: ' + @sTmp0

        EXEC sp_OAMethod @response, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
        PRINT 'Retry-After: ' + @sTmp0
        --  Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
        --  Keep response bodies and detailed diagnostics out of public logs.
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @response
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @query
        RETURN
      END
    IF @success <> 1
      BEGIN

        PRINT 'The HTTP response was incomplete.'
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @response
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @query
        RETURN
      END
    DECLARE @contentType int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @contentType OUT

    EXEC sp_OAMethod @response, 'GetHeaderField', @sTmp0 OUT, 'Content-Type'
    EXEC sp_OAMethod @contentType, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @contentType, 'StartsWith', @iTmp0 OUT, 'image/png', 0
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'Expected a PNG image; the output file was not written.'
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @response
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @query
        EXEC @hr = sp_OADestroy @contentType
        RETURN
      END
    EXEC sp_OAMethod @response, 'SaveBodyBinary', @success OUT, 'azure-maps-static.png'
    IF @success <> 1
      BEGIN

        PRINT 'Could not save the map image.'
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @response
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @query
        EXEC @hr = sp_OADestroy @contentType
        RETURN
      END

    PRINT 'Saved azure-maps-static.png'
    --  Retain map attribution when displaying or embedding the image.

    EXEC @hr = sp_OADestroy @config
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @response
    EXEC @hr = sp_OADestroy @result
    EXEC @hr = sp_OADestroy @query
    EXEC @hr = sp_OADestroy @contentType


END
GO