BlogAll Blog Posts | Next Post | Previous Post
Thursday, October 14, 2021
TMS FNC Maps v2.2 now includes a new free geocoding service in addition to the existing supported geocoding services from Azure, Bing, Google, Here and MapBox.
The OpenStreetMap Nominatim API service provides geocoding and reverse geocoding free of charge and does not require an API key. 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 paying services.
A geocoding request converts a text address to a latitude & longitude coordinate. The coordinate can then be used to display a location on the map.
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.Service := gsOpenStreetMap; 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; TMSFNCMaps1.BeginUpdate; TMSFNCMaps1.SetCenterCoordinate(Item.Coordinate.ToRec); TMSFNCMaps1.AddMarker(Item.Coordinate.ToRec, Item.Address); TMSFNCMaps1.EndUpdate; end; end ); end;
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.
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.Service := gsOpenStreetMap; Address := TMSFNCGeocoding1.GetReverseGeocodingSync(AEventData.Coordinate.ToRec); TMSFNCMaps1.BeginUpdate; TMSFNCMaps1.SetCenterCoordinate(AEventData.Coordinate.ToRec); TMSFNCMaps1.AddMarker(AEventData.Coordinate.ToRec, Address); TMSFNCMaps1.EndUpdate; end;
This blog post has received 4 comments.
All Blog Posts | Next Post | Previous Post