Sample code for 30+ languages & platforms
Delphi ActiveX 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 Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
config: TChilkatJsonObject;
http: TChilkatHttp;
query: TChilkatJsonObject;
response: TChilkatHttpResponse;
result: TChilkatJsonObject;
features: TChilkatJsonArray;
feature: TChilkatJsonObject;
i: Integer;

begin
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.
config := TChilkatJsonObject.Create(Self);
config.Strict := 1;
success := config.LoadFile('qa_data/azure/maps.json');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not load the private Azure Maps configuration.');
    Exit;
  end;
if ((config.JsonTypeOf('subscription_key') <> 1) or (config.StringOfEquals('subscription_key','',1) = 1)) then
  begin
    Memo1.Lines.Add('A nonempty subscription_key string is required.');
    Exit;
  end;

http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
http.FollowRedirects := 0;
http.ConnectTimeout := 30;
http.ReadTimeout := 60;
http.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 := TChilkatJsonObject.Create(Self);
query.UpdateString('api-version','2026-01-01');
query.UpdateString('query','15127 NE 24th Street, Redmond, WA 98052');
query.UpdateInt('top',5);

response := TChilkatHttpResponse.Create(Self);
success := http.HttpParams('GET','https://atlas.microsoft.com/geocode',query.ControlInterface,response.ControlInterface);
//  HttpParams also returns false for HTTP errors. Status 0 means no HTTP response.
if (response.StatusCode = 0) then
  begin
    Memo1.Lines.Add('Azure Maps request did not receive an HTTP response.');
    //  Inspect LastErrorText privately; it can contain credentials or address data.
    Exit;
  end;
if (response.StatusCode <> 200) then
  begin
    Memo1.Lines.Add('Azure Maps HTTP status: ' + IntToStr(response.StatusCode));
    Memo1.Lines.Add('Error code: ' + response.GetHeaderField('x-ms-error-code'));
    if (response.StatusCode = 429) then
      begin
        Memo1.Lines.Add('Retry-After: ' + response.GetHeaderField('Retry-After'));
      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 <> 1) then
  begin
    Memo1.Lines.Add('The HTTP response could not be read completely.');
    Exit;
  end;

result := TChilkatJsonObject.Create(Self);
result.Strict := 1;
success := result.Load(response.BodyStr);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Expected a well-formed JSON response.');
    Exit;
  end;
if ((result.StringOfEquals('type','FeatureCollection',1) <> 1) or (result.JsonTypeOf('features') <> 4)) then
  begin
    Memo1.Lines.Add('Expected a GeoJSON FeatureCollection with a features array.');
    Exit;
  end;
features := TChilkatJsonArray.Create(Self);
success := result.ArrayOf2('features',features.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not read the geocoding candidates.');
    Exit;
  end;
if (features.Size = 0) then
  begin
    Memo1.Lines.Add('No address matches. Check the spelling or provide more address information.');
    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 := TChilkatJsonObject.Create(Self);
i := 0;
while (i < features.Size) do
  begin
    success := features.ObjectAt2(i,feature.ControlInterface);
    if (success <> 1) then
      begin
        Memo1.Lines.Add('Invalid geocoding candidate.');
        Exit;
      end;
    if ((feature.StringOfEquals('geometry.type','Point',1) <> 1) or (feature.JsonTypeOf('geometry.coordinates[0]') <> 2) or (feature.JsonTypeOf('geometry.coordinates[1]') <> 2)) then
      begin
        Memo1.Lines.Add('Expected a point with numeric longitude and latitude.');
        Exit;
      end;
    if ((feature.JsonTypeOf('properties.address.formattedAddress') <> 1) or (feature.JsonTypeOf('properties.confidence') <> 1)) then
      begin
        Memo1.Lines.Add('Expected an address and match confidence.');
        Exit;
      end;
    Memo1.Lines.Add('Candidate ' + IntToStr(i + 1));
    Memo1.Lines.Add('Address: ' + feature.StringOf('properties.address.formattedAddress'));
    Memo1.Lines.Add('Confidence: ' + feature.StringOf('properties.confidence'));
    //  GeoJSON uses LONGITUDE first, LATITUDE second. StringOf preserves numeric text.
    Memo1.Lines.Add('Longitude: ' + feature.StringOf('geometry.coordinates[0]'));
    Memo1.Lines.Add('Latitude: ' + feature.StringOf('geometry.coordinates[1]'));
    if (feature.JsonTypeOf('properties.matchCodes') = 4) then
      begin
        Memo1.Lines.Add('Match codes: ' + feature.StringOf('properties.matchCodes'));
      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;