Azure Maps: Look Up an IP Address Country or Region
See more Azure Maps Examples
Uses Geolocation 1.0 to retrieve a two-letter country/region code for an IPv4 or IPv6 address. Handles null country information for special-purpose ranges.
This is coarse IP-based location. A proxy, VPN, mobile network, or shared egress can locate the network rather than the person or device. Do not interpret it as precise location or proof of residency.
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 @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_OAMethod @query, 'UpdateString', @success OUT, 'api-version', '1.0'
EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'ip', '8.8.8.8'
EXEC sp_OAMethod @http, 'HttpParams', @success OUT, 'GET', 'https://atlas.microsoft.com/geolocation/ip/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
-- IPv4 and IPv6 are accepted. Special-purpose IP ranges can have no country.
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'countryRegion.isoCode'
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp1 OUT, 'countryRegion.isoCode', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'No country/region is available for this IP address.'
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_OAMethod @result, 'StringOf', @sTmp0 OUT, 'countryRegion.isoCode'
PRINT 'Country/region ISO code: ' + @sTmp0
-- IP location is not a street address or a device's GPS location.
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
END
GO