Mapview - Reverse geocoding

Hi !

Is reverse geocoding by the Mapview possible ?

The function provided by embarcadero is buggy and causes my app to
crash often. This is also mentioned on the embarcadero forum by other
people.

(https://forums.embarcadero.com/thread.jspa?messageID=566473&#566473)

Maybe you can provide a more stable aproach.

Cheers,

Paul

Hi, this is experimental code, but uses the CLGeoCoder class directly, and only works on the device:


unit Unit1038;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, MacApi.ObjectiveC,
  iOSApi.CoreLocation, iOSApi.Foundation, FMX.StdCtrls;

type
  TForm1038 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    cl: CLGeocoder;
  public
    { Public declarations }
    procedure GeoCodeLocationResult(const placemarks: NSArray; const error: NSError);
    procedure GeoCodeAddressResult(const placemarks: NSArray; const error: NSError);
  end;

var
  Form1038: TForm1038;

implementation

{$R *.fmx}

procedure TForm1038.Button1Click(Sender: TObject);
begin
  cl := TCLGeocoder.Wrap(TCLGeocoder.Wrap(TCLGeocoder.OCClass.alloc).init);
  cl.geocodeAddressString(NSStr('100 California Street, San Francisco, United States'), GeoCodeAddressResult);
end;

procedure TForm1038.Button2Click(Sender: TObject);
var
  loc: CLLocation;
begin
  loc := TCLLocation.Wrap(TCLLocation.Wrap(TCLLocation.OCClass.alloc).initWithLatitude(37.7935885541, -122.397990315));
  cl := TCLGeocoder.Wrap(TCLGeocoder.Wrap(TCLGeocoder.OCClass.alloc).init);
  cl.reverseGeocodeLocation(loc, GeoCodeLocationResult);
  loc.release;
end;

procedure TForm1038.GeoCodeAddressResult(const placemarks: NSArray;
  const error: NSError);
var
  placemark: CLPlacemark;
  location: CLLocation;
  coordinate: CLLocationCoordinate2D;
begin
  if placemarks.count > 0 then
  begin
    placemark := TCLPlacemark.Wrap(placemarks.objectAtIndex(0));
    if Assigned(placemark) then
    begin
      location := placemark.location;
      coordinate := location.coordinate;
      label1.Text := floattostr(coordinate.latitude) + ' : ' + FloatToStr(coordinate.longitude);
    end;
  end;

  cl.release;
end;

procedure TForm1038.GeoCodeLocationResult(const placemarks: NSArray;
  const error: NSError);
var
  placemark: CLPlacemark;
  address: NSString;
begin
  if placemarks.count > 0 then
  begin
    placemark := TCLPlacemark.Wrap(placemarks.objectAtIndex(0));
    if Assigned(placemark) then
    begin
      address := placemark.addressDictionary.description;
      label2.text := UTF8ToString(address.UTF8String);
    end;
  end;

  cl.release;
end;

end.


Hi Pieter,

Thanks alot for this, will try this.

Cheers,

Paul

Hi Pieter,

Works nice !

I can use this.

Cheers.

Paul