Android实例-LocationSensor位置传感器(XE8+小米2)

时间:2022-12-20 17:23:32

Android实例-LocationSensor位置传感器(XE8+小米2)

结果:

1.启动后有时会闪退,后来重新做的工程就好了。原因不明(可能与地理反码有关)。

2.原文是用的GOOGLE地图显示位置,但在咱们这里好像不行,改为百度,但百度用的是HTML文件。太麻烦了,大家自己看百度API吧。

3.打开二个权限,不过我看了一下工程自动就是打上对号的(Access coarse location、Access fine location)。

相关资料:

官网地址  http://docwiki.embarcadero.com/RADStudio/XE5/en/Mobile_Tutorial:_Using_Location_Sensors_(iOS_and_Android)

实例代码:

 unit Unit1;

 interface

 uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Sensors,
System.Sensors.Components, FMX.WebBrowser, FMX.Controls.Presentation,
FMX.StdCtrls; type
TForm1 = class(TForm)
Button1: TButton;
WebBrowser1: TWebBrowser;
LocationSensor1: TLocationSensor;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
Label12: TLabel;
Label13: TLabel;
procedure Button1Click(Sender: TObject);
procedure LocationSensor1LocationChanged(Sender: TObject; const OldLocation,
NewLocation: TLocationCoord2D);
private
{ Private declarations }
FGeocoder: TGeocoder;//定义地理编码对象
procedure OnGeocodeReverseEvent(const Address: TCivicAddress);//定义地理编码事件
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID} procedure TForm1.Button1Click(Sender: TObject);
begin
LocationSensor1.Active := True;
end; procedure TForm1.LocationSensor1LocationChanged(Sender: TObject;
const OldLocation, NewLocation: TLocationCoord2D);
var
URLString: String;
begin
//显示经纬度
Label2.Text := Format('纬度:%2.6f', [NewLocation.Latitude]);
Label3.Text := Format('经度:%2.6f', [NewLocation.Longitude]);
//地图显示位置
// URLString := Format('https://maps.google.com/maps?q=%s,%s',
// [Format('%2.6f', [NewLocation.Latitude]), Format('%2.6f', [NewLocation.Longitude])]);
// WebBrowser1.Navigate(URLString);
//地理编码功能
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(NewLocation);
end; //地理编码事件具体实现
procedure TForm1.OnGeocodeReverseEvent(const Address: TCivicAddress);
begin
label4.Text := Format('行政区:%s', [Address.AdminArea]);
label5.Text := Format('国家代码:%s', [Address.CountryCode]);
label6.Text := Format('国家名称:%s', [Address.CountryName]);
label7.Text := Format('特征名称:%s', [Address.FeatureName]);
label8.Text := Format('地区:%s', [Address.Locality]);
label9.Text := Format('邮政编码:%s', [Address.PostalCode]);
label10.Text := Format('分行政区:%s', [Address.SubAdminArea]);
label11.Text := Format('分地区:%s', [Address.SubLocality]);
label12.Text := Format('分大街:%s', [Address.SubThoroughfare]);
label13.Text := Format('大街:%s', [Address.Thoroughfare]);
end; end.