Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Azure Maps Get Search Address

See more Azure Maps Examples

Get information for a given address.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.JsonObject,
  Chilkat.Http;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  http: THttp;
  url: string;
  strResp: string;
  jsonResponse: TJsonObject;
  summaryQuery: string;
  summaryQueryType: string;
  summaryQueryTime: Integer;
  summaryNumResults: Integer;
  summaryOffset: Integer;
  summaryTotalResults: Integer;
  summaryFuzzyLevel: Integer;
  i: Integer;
  count_i: Integer;
  v_type: string;
  id: string;
  score: string;
  addressStreetNumber: string;
  addressStreetName: string;
  addressMunicipalitySubdivision: string;
  addressMunicipality: string;
  addressCountrySecondarySubdivision: string;
  addressCountryTertiarySubdivision: string;
  addressCountrySubdivision: string;
  addressPostalCode: string;
  addressExtendedPostalCode: string;
  addressCountryCode: string;
  addressCountry: string;
  addressCountryCodeISO3: string;
  addressFreeformAddress: string;
  addressCountrySubdivisionName: string;
  positionLat: string;
  positionLon: string;
  viewportTopLeftPointLat: string;
  viewportTopLeftPointLon: string;
  viewportBtmRightPointLat: string;
  viewportBtmRightPointLon: string;
  j: Integer;
  count_j: Integer;

begin
  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  http := THttp.Create;

  http.Accept := 'application/json';

  url := 'https://atlas.microsoft.com/search/address/{$format}?subscription-key=[subscription-key]&api-version=1.0&query={$query}';

  http.SetUrlVar('format','json');
  http.SetUrlVar('query','15127 NE 24th Street, Redmond, WA 98052');

  strResp := http.QuickGetStr(url);
  if (http.LastMethodSuccess <> True) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  WriteLn('Response Status Code: ' + http.LastStatus);

  jsonResponse := TJsonObject.Create;
  jsonResponse.Load(strResp);
  jsonResponse.EmitCompact := False;
  WriteLn(jsonResponse.Emit());

  if (http.LastStatus <> 200) then
    begin
      WriteLn('Failed.');
      Exit;
    end;

  //  Sample output...
  //  (See the parsing code below..)
  //  
  //  Use this online tool to generate parsing code from sample JSON: 
  //  Generate Parsing Code from JSON

  //  {
  //    "summary": {
  //      "query": "15127 NE 24th Street, Redmond, WA 98052",
  //      "queryType": "NON_NEAR",
  //      "queryTime": 58,
  //      "numResults": 1,
  //      "offset": 0,
  //      "totalResults": 1,
  //      "fuzzyLevel": 1
  //    },
  //    "results": [
  //      {
  //        "type": "Point Address",
  //        "id": "US/PAD/p0/19173426",
  //        "score": 14.51,
  //        "address": {
  //          "streetNumber": "15127",
  //          "streetName": "NE 24th St",
  //          "municipalitySubdivision": "Redmond",
  //          "municipality": "Redmond, Adelaide, Ames Lake, Avondale, Earlmount",
  //          "countrySecondarySubdivision": "King",
  //          "countryTertiarySubdivision": "Seattle East",
  //          "countrySubdivision": "WA",
  //          "postalCode": "98052",
  //          "extendedPostalCode": "980525544",
  //          "countryCode": "US",
  //          "country": "United States Of America",
  //          "countryCodeISO3": "USA",
  //          "freeformAddress": "15127 NE 24th St, Redmond, WA 980525544",
  //          "countrySubdivisionName": "Washington"
  //        },
  //        "position": {
  //          "lat": 47.6308,
  //          "lon": -122.1385
  //        },
  //        "viewport": {
  //          "topLeftPoint": {
  //            "lat": 47.6317,
  //            "lon": -122.13983
  //          },
  //          "btmRightPoint": {
  //            "lat": 47.6299,
  //            "lon": -122.13717
  //          }
  //        },
  //        "entryPoints": [
  //          {
  //            "type": "main",
  //            "position": {
  //              "lat": 47.6315,
  //              "lon": -122.13852
  //            }
  //          }
  //        ]
  //      }
  //    ]
  //  }
  //  

  summaryQuery := jsonResponse.StringOf('summary.query');
  summaryQueryType := jsonResponse.StringOf('summary.queryType');
  summaryQueryTime := jsonResponse.IntOf('summary.queryTime');
  summaryNumResults := jsonResponse.IntOf('summary.numResults');
  summaryOffset := jsonResponse.IntOf('summary.offset');
  summaryTotalResults := jsonResponse.IntOf('summary.totalResults');
  summaryFuzzyLevel := jsonResponse.IntOf('summary.fuzzyLevel');
  i := 0;
  count_i := jsonResponse.SizeOfArray('results');
  while i < count_i do
    begin
      jsonResponse.I := i;
      v_type := jsonResponse.StringOf('results[i].type');
      id := jsonResponse.StringOf('results[i].id');
      score := jsonResponse.StringOf('results[i].score');
      addressStreetNumber := jsonResponse.StringOf('results[i].address.streetNumber');
      addressStreetName := jsonResponse.StringOf('results[i].address.streetName');
      addressMunicipalitySubdivision := jsonResponse.StringOf('results[i].address.municipalitySubdivision');
      addressMunicipality := jsonResponse.StringOf('results[i].address.municipality');
      addressCountrySecondarySubdivision := jsonResponse.StringOf('results[i].address.countrySecondarySubdivision');
      addressCountryTertiarySubdivision := jsonResponse.StringOf('results[i].address.countryTertiarySubdivision');
      addressCountrySubdivision := jsonResponse.StringOf('results[i].address.countrySubdivision');
      addressPostalCode := jsonResponse.StringOf('results[i].address.postalCode');
      addressExtendedPostalCode := jsonResponse.StringOf('results[i].address.extendedPostalCode');
      addressCountryCode := jsonResponse.StringOf('results[i].address.countryCode');
      addressCountry := jsonResponse.StringOf('results[i].address.country');
      addressCountryCodeISO3 := jsonResponse.StringOf('results[i].address.countryCodeISO3');
      addressFreeformAddress := jsonResponse.StringOf('results[i].address.freeformAddress');
      addressCountrySubdivisionName := jsonResponse.StringOf('results[i].address.countrySubdivisionName');
      positionLat := jsonResponse.StringOf('results[i].position.lat');
      positionLon := jsonResponse.StringOf('results[i].position.lon');
      viewportTopLeftPointLat := jsonResponse.StringOf('results[i].viewport.topLeftPoint.lat');
      viewportTopLeftPointLon := jsonResponse.StringOf('results[i].viewport.topLeftPoint.lon');
      viewportBtmRightPointLat := jsonResponse.StringOf('results[i].viewport.btmRightPoint.lat');
      viewportBtmRightPointLon := jsonResponse.StringOf('results[i].viewport.btmRightPoint.lon');
      j := 0;
      count_j := jsonResponse.SizeOfArray('results[i].entryPoints');
      while j < count_j do
        begin
          jsonResponse.J := j;
          v_type := jsonResponse.StringOf('results[i].entryPoints[j].type');
          positionLat := jsonResponse.StringOf('results[i].entryPoints[j].position.lat');
          positionLon := jsonResponse.StringOf('results[i].entryPoints[j].position.lon');
          j := j + 1;
        end;

      i := i + 1;
    end;



  http.Free;
  jsonResponse.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.