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

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 Ruby Downloads

Ruby
require 'chilkat'

success = false

#  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.
config = Chilkat::CkJsonObject.new()
config.put_Strict(true)
success = config.LoadFile("qa_data/azure/maps.json")
if (success != true)
    print "Could not load the private Azure Maps configuration." + "\n";
    exit
end

if ((config.JsonTypeOf("subscription_key") != 1) or (config.StringOfEquals("subscription_key","",true) == true))
    print "A nonempty subscription_key string is required." + "\n";
    exit
end

http = Chilkat::CkHttp.new()
http.put_RequireSslCertVerify(true)
http.put_FollowRedirects(false)
http.put_ConnectTimeout(30)
http.put_ReadTimeout(60)
http.put_Accept("application/geo+json")
#  The current Geocoding API accepts this key as a header, avoiding a key in the URL.
http.SetRequestHeader("subscription-key",config.stringOf("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.
query = Chilkat::CkJsonObject.new()
query.UpdateString("api-version","2026-01-01")
query.UpdateString("query","15127 NE 24th Street, Redmond, WA 98052")
query.UpdateInt("top",5)

response = Chilkat::CkHttpResponse.new()
success = http.HttpParams("GET","https://atlas.microsoft.com/geocode",query,response)
#  HttpParams also returns false for HTTP errors. Status 0 means no HTTP response.
if (response.get_StatusCode() == 0)
    print "Azure Maps request did not receive an HTTP response." + "\n";
    #  Inspect LastErrorText privately; it can contain credentials or address data.
    exit
end

if (response.get_StatusCode() != 200)
    print "Azure Maps HTTP status: " + response.get_StatusCode().to_s() + "\n";
    print "Error code: " + response.getHeaderField("x-ms-error-code") + "\n";
    if (response.get_StatusCode() == 429)
        print "Retry-After: " + response.getHeaderField("Retry-After") + "\n";
    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.
    exit
end

if (success != true)
    print "The HTTP response could not be read completely." + "\n";
    exit
end

result = Chilkat::CkJsonObject.new()
result.put_Strict(true)
success = result.Load(response.bodyStr())
if (success != true)
    print "Expected a well-formed JSON response." + "\n";
    exit
end

if ((result.StringOfEquals("type","FeatureCollection",true) != true) or (result.JsonTypeOf("features") != 4))
    print "Expected a GeoJSON FeatureCollection with a features array." + "\n";
    exit
end

features = Chilkat::CkJsonArray.new()
success = result.ArrayOf2("features",features)
if (success != true)
    print "Could not read the geocoding candidates." + "\n";
    exit
end

if (features.get_Size() == 0)
    print "No address matches. Check the spelling or provide more address information." + "\n";
    exit
end

#  Display the candidates instead of assuming that the first one is an exact match.
#  Geocoding finds locations; it does not certify postal deliverability.
feature = Chilkat::CkJsonObject.new()
i = 0
while (i < features.get_Size())
    success = features.ObjectAt2(i,feature)
    if (success != true)
        print "Invalid geocoding candidate." + "\n";
        exit
    end

    if ((feature.StringOfEquals("geometry.type","Point",true) != true) or (feature.JsonTypeOf("geometry.coordinates[0]") != 2) or (feature.JsonTypeOf("geometry.coordinates[1]") != 2))
        print "Expected a point with numeric longitude and latitude." + "\n";
        exit
    end

    if ((feature.JsonTypeOf("properties.address.formattedAddress") != 1) or (feature.JsonTypeOf("properties.confidence") != 1))
        print "Expected an address and match confidence." + "\n";
        exit
    end

    print "Candidate " + (i + 1).to_s() + "\n";
    print "Address: " + feature.stringOf("properties.address.formattedAddress") + "\n";
    print "Confidence: " + feature.stringOf("properties.confidence") + "\n";
    #  GeoJSON uses LONGITUDE first, LATITUDE second. StringOf preserves numeric text.
    print "Longitude: " + feature.stringOf("geometry.coordinates[0]") + "\n";
    print "Latitude: " + feature.stringOf("geometry.coordinates[1]") + "\n";
    if (feature.JsonTypeOf("properties.matchCodes") == 4)
        print "Match codes: " + feature.stringOf("properties.matchCodes") + "\n";
    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