delphi Android / iOS应用程序中使用TGeocoder类进行反向地理编码(从位置信息中获取地址)  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi Android / iOS应用程序中使用TGeocoder类进行反向地理编码(从位置信息中获取地址)


Delphi有一个称为TGeocoder的类,用于处理地理编码和反向地理编码。
它可能没有引起注意,因为它没有在工具选项板中注册。

介绍如何使用“ TGeocoder ”从位置信息获取地址。

使用“ TLocationSensor ”获得位置信息。
如果TLocationSensor的Active属性设置为True,则可以通过OnLocationChanged事件获取位置信息。

procedure TForm1.Button1Click(Sender: TObject);
begin
  LocationSensor1.Active := True;
end;

procedure TForm1.LocationSensor1LocationChanged(Sender: TObject;
  const OldLocation, NewLocation: TLocationCoord2D);
begin
end;
将TGeocoder变量添加到私有字段。

private
  FGeocoder: TGeocoder;
反向地理编码过程完成后,TGeocoder会引发 OnGeocodeReverse事件。

添加一个方法来处理OnGeocodeReverse事件。

procedure OnGeocodeReverseEvent(const Address: TCivicAddress);
在窗体的构造函数中执行初始设置。

procedure TForm1.FormCreate(Sender: TObject);
begin
  FGeocoder := TGeocoder.Current.Create;
  FGeocoder.OnGeocodeReverse := OnGeocodeReverseEvent;
end;
TLocationSensor在OnLocationChanged事件获取的位置信息TGeocoder并与反向地理编码。

procedure TForm1.LocationSensor1LocationChanged(Sender: TObject;
  const OldLocation, NewLocation: TLocationCoord2D);
begin
  LocationSensor1.Active := False;
  FGeocoder.GeocodeReverse(NewLocation);
end;
显示通过反向地理编码获得的地址。

procedure TForm1.OnGeocodeReverseEvent(const Address: TCivicAddress);
begin
  TThread.Synchronize(TThread.CurrentThread,
    procedure
    begin
      Memo1.Lines.Clear;
      Memo1.Lines.Add('国家代码:' + Address.CountryCode);
      Memo1.Lines.Add('国名:' + Address.CountryName);
      Memo1.Lines.Add('管理領域:' + Address.AdminArea);
      Memo1.Lines.Add('管理区域:' + Address.SubAdminArea);
      Memo1.Lines.Add('区域名:' + Address.Locality);
      Memo1.Lines.Add('子区域:' + Address.SubLocality);
      Memo1.Lines.Add('街道:' + Address.SubThoroughfare);
      Memo1.Lines.Add('街道:' + Address.Thoroughfare);
    end);
end;

//上面得翻译是用百度翻译得,将就下吧。

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.15983104705811 seconds