Blog

All Blog Posts  |  Next Post  |  Previous Post

Directions & Geocoding with GeoApify in TMS FNC Maps

Bookmarks: 

Thursday, January 27, 2022

TMS FNC Maps v2.4 adds support for a new geocoding and directions service in addition to the existing supported directions services from AzureBingGoogleHere, MapBox and OpenRouteService.


GeoApify

TMS Software Delphi  Components

The GeoApify API service provides geocoding, reverse geocoding and directions. This service can be used in combination with all supported mapping services in TMS FNC Maps. Specifically in combination with the OpenLayers mapping service this is a worthwhile free alternative to comparable services.

Geocoding

A geocoding request converts a text address to a geographic latitude & longitude coordinate. The coordinate can then be used to display a location on the map.

TMS Software Delphi  Components

With the following code we are going to look up the location of "Baker Street, London" and display it on the center of the map with a marker and a title that contains the address.
Note that this example uses the asynchronous method with a callback.

procedure TForm1.Button1Click(Sender: TObject);
var
  Item: TTMSFNCGeocodingItem;
begin
  TMSFNCGeocoding1.APIKey := 'abc123';
  TMSFNCGeocoding1.Service := gsGeoApify;
  TMSFNCGeocoding1.GetGeocoding('Baker Street, London',
    procedure(const ARequest: TTMSFNCGeocodingRequest; const ARequestResult: TTMSFNCCloudBaseRequestResult)
    begin
      if (ARequestResult.Success) and (ARequest.Items.Count > 0) then
      begin
        Item := ARequest.Items[0];

        TMSFNCMaps1.BeginUpdate;
        TMSFNCMaps1.SetCenterCoordinate(Item.Coordinate.ToRec);
        TMSFNCMaps1.AddMarker(Item.Coordinate.ToRec, Item.Address);
        TMSFNCMaps1.EndUpdate;
      end;
    end
  );
end;


Reverse Geocoding

A reverse geocoding request converts a latitude & longitude coordinate to a text address. The text address can then be used to display an address associated with a location on the map.

TMS Software Delphi  Components

With the following code we are going to track a click on the map, retrieve the address for that location and display it on the map with a marker and a title that contains the address 
Note that this example uses the synchronous method that provides an immediate result without a callback.

procedure TForm1.TMSFNCMaps1MapClick(Sender: TObject; AEventData: TTMSFNCMapsEventData);
var
  Address: string;
begin
  TMSFNCGeocoding1.APIKey := 'abc123';
  TMSFNCGeocoding1.Service := gsOpenRouteService;
  Address := TMSFNCGeocoding1.GetReverseGeocodingSync(AEventData.Coordinate.ToRec);

  TMSFNCMaps1.BeginUpdate;
  TMSFNCMaps1.SetCenterCoordinate(AEventData.Coordinate.ToRec);
  TMSFNCMaps1.AddMarker(AEventData.Coordinate.ToRec, Address);
  TMSFNCMaps1.EndUpdate;
end;


Directions

A directions request provides step by step directions between a start and end location. The directions data can then be used to display a route on the map. Additional options can be configured for the directions request, including: waypoints, alternative routes and language.

TMS Software Delphi  Components

With the following code we are going to retrieve driving directions between New York and Philadelphia. On the map, a marker is displayed at both the start and end location, a polyline displays the route and a label displays the distance and duration of the route.
In the TListBox on the step by step directions are displayed for each step in the route directions.
Note that this example uses the asynchronous method with a callback.

procedure TForm1.Button1Click(Sender: TObject);
var
  cStart, cEnd: TTMSFNCMapsCoordinateRec;
begin
  cStart.Latitude := 40.68295;
  cStart.Longitude := -73.9708;
  cEnd.Latitude := 39.990821;
  cEnd.Longitude := -75.168428;

  TMSFNCDirections1.APIKey := 'abc123';
  TMSFNCDirections1.Service := dsGeoApify;
  TMSFNCDirections1.GetDirections(cStart, cEnd,
    procedure(const ARequest: TTMSFNCDirectionsRequest; const ARequestResult: TTMSFNCCloudBaseRequestResult)
    var
      it: TTMSFNCDirectionsItem;
      I: Integer;
      p: TTMSFNCOpenLayersPolyline;
      Hours, Minutes: string;
    begin
      TMSFNCOpenLayers1.ClearPolylines;
      ListBox1.Clear;

      if ARequestResult.Success then
      begin
        if ARequest.Items.Count > 0 then
        begin
          TMSFNCOpenLayers1.BeginUpdate;
          it := ARequest.Items[0];

          TMSFNCOpenLayers1.AddMarker(it.Legs[0].StartLocation.ToRec, 'New York', DEFAULTWAYPOINTMARKER);
          TMSFNCOpenLayers1.AddMarker(it.Legs[0].EndLocation.ToRec, 'Philadelphia', DEFAULTENDMARKER);

          Hours := IntToStr(Round(it.Duration / 60) div 60);
          Minutes := IntToStr(Round(it.Duration / 60) mod 60);

          p := TTMSFNCOpenLayersPolyline(TMSFNCOpenLayers1.AddPolyline(it.Coordinates.ToArray));
          p.StrokeColor := gcBlue;
          p.StrokeOpacity := 0.5;
          p.StrokeWidth := 5;
          p.&Label.Text := FloatToStr(Round(it.Distance / 1000)) + ' km, ' + Hours + ' h ' + Minutes + ' min';

          TMSFNCOpenLayers1.ZoomToBounds(it.Coordinates.Bounds.ToRec);
          TMSFNCOpenLayers1.EndUpdate;

          ListBox1.Clear;
          for I := 0 to it.Steps.Count - 1 do
          begin
            ListBox1.Items.Add(it.Steps[I].Instructions)
          end;
        end;
      end;
    end
  );
end;


Available Now

The TMS FNC Maps v2.4 update is available now for Delphi & Visual Studio Code (with TMS WEB Core). You can download the latest version and start using the new features right away!



Bart Holvoet


Bookmarks: 

This blog post has not received any comments yet.



Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post