结果:
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)
实例代码:
1 unit Unit1; 2 3 interface 4 5 uses 6 System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 7 FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Sensors, 8 System.Sensors.Components, FMX.WebBrowser, FMX.Controls.Presentation, 9 FMX.StdCtrls; 10 11 type 12 TForm1 = class(TForm) 13 Button1: TButton; 14 WebBrowser1: TWebBrowser; 15 LocationSensor1: TLocationSensor; 16 Label1: TLabel; 17 Label2: TLabel; 18 Label3: TLabel; 19 Label4: TLabel; 20 Label5: TLabel; 21 Label6: TLabel; 22 Label7: TLabel; 23 Label8: TLabel; 24 Label9: TLabel; 25 Label10: TLabel; 26 Label11: TLabel; 27 Label12: TLabel; 28 Label13: TLabel; 29 procedure Button1Click(Sender: TObject); 30 procedure LocationSensor1LocationChanged(Sender: TObject; const OldLocation, 31 NewLocation: TLocationCoord2D); 32 private 33 { Private declarations } 34 FGeocoder: TGeocoder;//定义地理编码对象 35 procedure OnGeocodeReverseEvent(const Address: TCivicAddress);//定义地理编码事件 36 public 37 { Public declarations } 38 end; 39 40 var 41 Form1: TForm1; 42 43 implementation 44 45 {$R *.fmx} 46 {$R *.NmXhdpiPh.fmx ANDROID} 47 48 procedure TForm1.Button1Click(Sender: TObject); 49 begin 50 LocationSensor1.Active := True; 51 end; 52 53 procedure TForm1.LocationSensor1LocationChanged(Sender: TObject; 54 const OldLocation, NewLocation: TLocationCoord2D); 55 var 56 URLString: String; 57 begin 58 //显示经纬度 59 Label2.Text := Format('纬度:%2.6f', [NewLocation.Latitude]); 60 Label3.Text := Format('经度:%2.6f', [NewLocation.Longitude]); 61 //地图显示位置 62 // URLString := Format('https://maps.google.com/maps?q=%s,%s', 63 // [Format('%2.6f', [NewLocation.Latitude]), Format('%2.6f', [NewLocation.Longitude])]); 64 // WebBrowser1.Navigate(URLString); 65 //地理编码功能 66 if not Assigned(FGeocoder) then 67 begin 68 if Assigned(TGeocoder.Current) then 69 FGeocoder := TGeocoder.Current.Create; 70 if Assigned(FGeocoder) then 71 FGeocoder.OnGeocodeReverse := OnGeocodeReverseEvent; 72 end; 73 if Assigned(FGeocoder) and not FGeocoder.Geocoding then 74 FGeocoder.GeocodeReverse(NewLocation); 75 end; 76 77 //地理编码事件具体实现 78 procedure TForm1.OnGeocodeReverseEvent(const Address: TCivicAddress); 79 begin 80 label4.Text := Format('行政区:%s', [Address.AdminArea]); 81 label5.Text := Format('国家代码:%s', [Address.CountryCode]); 82 label6.Text := Format('国家名称:%s', [Address.CountryName]); 83 label7.Text := Format('特征名称:%s', [Address.FeatureName]); 84 label8.Text := Format('地区:%s', [Address.Locality]); 85 label9.Text := Format('邮政编码:%s', [Address.PostalCode]); 86 label10.Text := Format('分行政区:%s', [Address.SubAdminArea]); 87 label11.Text := Format('分地区:%s', [Address.SubLocality]); 88 label12.Text := Format('分大街:%s', [Address.SubThoroughfare]); 89 label13.Text := Format('大街:%s', [Address.Thoroughfare]); 90 end; 91 92 end.