Azure Maps: Get Current Weather Conditions
See more Azure Maps Examples
Retrieves current weather observations using Weather 1.1. Coordinates are latitude, longitude. Requests metric units and the compact set of fields; change unit to imperial or details to true as needed.
Displays the observation timestamp and reads the units returned with each value. Missing or null temperature is reported as unavailable, not zero. Coverage varies by location; an observation timestamp is not a forecast time or a guarantee of real-time conditions.
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.
Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @sTmp1 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
-- Weather 1.1 uses LATITUDE,LONGITUDE. Request the latest observation.
EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'api-version', '1.1'
EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'query', '47.642282,-122.136833'
EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'unit', 'metric'
EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'details', 'false'
EXEC sp_OAMethod @query, 'UpdateInt', @success OUT, 'duration', 0
EXEC sp_OAMethod @http, 'HttpParams', @success OUT, 'GET', 'https://atlas.microsoft.com/weather/currentConditions/json', @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
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 @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
RETURN
END
DECLARE @observations int
EXEC @hr = sp_OACreate 'Chilkat.JsonArray', @observations OUT
DECLARE @observation int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @observation OUT
EXEC sp_OAMethod @result, 'ArrayOf2', @success OUT, 'results', @observations
IF @success <> 1
BEGIN
PRINT 'Expected weather results array.'
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 @observations
EXEC @hr = sp_OADestroy @observation
RETURN
END
EXEC sp_OAGetProperty @observations, 'Size', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'No weather observations were returned.'
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 @observations
EXEC @hr = sp_OADestroy @observation
RETURN
END
DECLARE @i int
SELECT @i = 0
EXEC sp_OAGetProperty @observations, 'Size', @iTmp0 OUT
WHILE (@i < @iTmp0)
BEGIN
EXEC sp_OAMethod @observations, 'ObjectAt2', @success OUT, @i, @observation
IF @success <> 1
BEGIN
PRINT 'Invalid weather observation.'
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 @observations
EXEC @hr = sp_OADestroy @observation
RETURN
END
EXEC sp_OAMethod @observation, 'StringOf', @sTmp0 OUT, 'dateTime'
PRINT 'Observed at: ' + @sTmp0
EXEC sp_OAMethod @observation, 'StringOf', @sTmp0 OUT, 'phrase'
PRINT 'Conditions: ' + @sTmp0
EXEC sp_OAMethod @observation, 'JsonTypeOf', @iTmp0 OUT, 'temperature.value'
EXEC sp_OAMethod @observation, 'JsonTypeOf', @iTmp1 OUT, 'temperature.unit'
IF (@iTmp0 = 2) and (@iTmp1 = 1)
BEGIN
EXEC sp_OAMethod @observation, 'StringOf', @sTmp0 OUT, 'temperature.value'
EXEC sp_OAMethod @observation, 'StringOf', @sTmp1 OUT, 'temperature.unit'
PRINT 'Temperature: ' + @sTmp0 + ' ' + @sTmp1
END
ELSE
BEGIN
PRINT 'Temperature unavailable.'
END
EXEC sp_OAMethod @observation, 'JsonTypeOf', @iTmp0 OUT, 'hasPrecipitation'
IF @iTmp0 = 5
BEGIN
EXEC sp_OAMethod @observation, 'BoolOf', @iTmp0 OUT, 'hasPrecipitation'
PRINT 'Precipitation: ' + @iTmp0
END
SELECT @i = @i + 1
END
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 @observations
EXEC @hr = sp_OADestroy @observation
END
GO