GOOGLE定位
/// <author>cxg 2018-3-27</author>
unit umap;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
FRM_BASS_MAIN, FMX.Effects, FMX.Controls.Presentation, FMX.Objects, System.Sensors,
FMX.ListBox, FMX.Layouts, System.Sensors.Components, FMX.WebBrowser;
type
Tfmap = class(TFrame_Base_Main)
ToolBar1: TLayout;
Button1: TButton;
WebBrowser1: TWebBrowser;
Layout1: TLayout;
ListBox1: TListBox;
ListBoxItemLatitude: TListBoxItem;
ListBoxItemLongitude: TListBoxItem;
ListBox2: TListBox;
ListBoxItemAdminArea: TListBoxItem;
ListBoxItemCountryCode: TListBoxItem;
ListBoxItemCountryName: TListBoxItem;
ListBoxItemFeatureName: TListBoxItem;
ListBoxItemLocality: TListBoxItem;
ListBoxItemPostalCode: TListBoxItem;
ListBoxItemSubAdminArea: TListBoxItem;
ListBoxItemSubLocality: TListBoxItem;
ListBoxItemSubThoroughfare: TListBoxItem;
ListBoxItemThoroughfare: TListBoxItem;
LocationSensor1: TLocationSensor;
procedure Button1Click(Sender: TObject);
procedure LocationSensor1LocationChanged(Sender: TObject; const OldLocation, NewLocation: TLocationCoord2D);
private
{ Private declarations }
FGeocoder: TGeocoder;
procedure OnGeocodeReverseEvent(const Address: TCivicAddress);
procedure Geocoder(location: TLocationCoord2D);
public
{ Public declarations }
Procedure Unitialize; override;
end;
var
fmap: Tfmap;
implementation
{$R *.fmx}
{ TFrame_Base_Main2 }
procedure Tfmap.Button1Click(Sender: TObject);
begin
LocationSensor1.Active := False;
LocationSensor1.Active := True;
end;
procedure Tfmap.Geocoder(location: TLocationCoord2D);
begin
// 利用 TGeocoder进行地名解析
//建立TGeocode实像,并为之设置事件代码
if not Assigned(FGeocoder) then
begin
if Assigned(TGeocoder.Current) then
FGeocoder := TGeocoder.Current.Create;
if Assigned(FGeocoder) then
FGeocoder.OnGeocodeReverse := OnGeocodeReverseEvent;
end;
//转换成地名
if Assigned(FGeocoder) and not FGeocoder.Geocoding then
FGeocoder.GeocodeReverse(Location);
end;
procedure Tfmap.LocationSensor1LocationChanged(Sender: TObject; const OldLocation, NewLocation: TLocationCoord2D);
var
URLString: string;
LSettings: TFormatSettings;
LDecSeparator: Char;
begin
LDecSeparator := FormatSettings.DecimalSeparator;
LSettings := FormatSettings;
try
FormatSettings.DecimalSeparator := '.';
// 显示当前位置
ListBoxItemLatitude.ItemData.Text := Format('纬度: %2.6f', [NewLocation.Latitude]);
ListBoxItemLongitude.ItemData.Text := Format('经度: %2.6f', [NewLocation.Longitude]);
// 用 Google Maps显示
URLString := Format('https://maps.google.com/maps?q=%2.6f,%2.6f', [NewLocation.Latitude, NewLocation.Longitude]);
finally
FormatSettings.DecimalSeparator := LDecSeparator;
end;
{ and track the location via Google Maps }
WebBrowser1.Navigate(URLString);
Geocoder(NewLocation);
end;
procedure Tfmap.OnGeocodeReverseEvent(const Address: TCivicAddress);
begin
ListBoxItemSubAdminArea.ItemData.Text := Format('子行政区: %s', [Address.SubAdminArea]);
ListBoxItemAdminArea.ItemData.Text := Format('省州: %s', [Address.AdminArea]);
ListBoxItemCountryCode.ItemData.Text := Format('国家编码: %s', [Address.CountryCode]);
ListBoxItemCountryName.ItemData.Text := Format('国家名称: %s', [Address.CountryName]);
ListBoxItemFeatureName.ItemData.Text := Format('详细地址: %s', [Address.FeatureName]);
ListBoxItemSubLocality.ItemData.Text := Format('区县: %s', [Address.SubLocality]);
ListBoxItemLocality.ItemData.Text := Format('城市: %s', [Address.Locality]);
ListBoxItemPostalCode.ItemData.Text := Format('邮政编码: %s', [Address.PostalCode]);
ListBoxItemSubThoroughfare.ItemData.Text := Format('子街道: %s', [Address.SubThoroughfare]);
ListBoxItemThoroughfare.ItemData.Text := Format('街道: %s', [Address.Thoroughfare]);
end;
procedure Tfmap.Unitialize;
begin
inherited;
FGeocoder.DisposeOf;
end;
end.