如果用xe6自帶的LocationSensor控件,默認優(yōu)先使用網(wǎng)絡(luò )位置,網(wǎng)絡(luò )位置定位精度不準確,不能滿(mǎn)足高精度定位的要求。但xe6自帶的LocationSensor控件不能指定網(wǎng)絡(luò )定位優(yōu)先還是GPS定位優(yōu)先,如果調用java API是可以指定優(yōu)先使用GPS定位,以下代碼經(jīng)實(shí)測證實(shí)是可以直接指定GPS定位的。
uses Androidapi.JNI.Location, Androidapi.JNIBridge, Androidapi.JNI.JavaTypes,
Androidapi.JNI.Os,FMX.Helpers.Android,Androidapi.JNI.GraphicsContentViewText;
type
TLocationListener = class;
TForm1 = class(TForm)
. . . . . .
private
{ Private declarations }
FLocationManager : JLocationManager;
locationListener : TLocationListener;
public
destructor Destroy; override;
{ Public declarations }
procedure onLocationChanged(location: JLocation);
end;
//Sensore GPS
TLocationListener = class(TJavaLocal, JLocationListener)
private
[weak]
FParent : TForm1;
public
constructor Create(AParent : TForm1);
procedure onLocationChanged(location: JLocation); cdecl;
procedure onProviderDisabled(provider: JString); cdecl;
procedure onProviderEnabled(provider: JString); cdecl;
procedure onStatusChanged(provider: JString; status: Integer; extras: JBundle); cdecl;
end;
. . . . .
constructor TLocationListener.Create(AParent: TForm1);
begin
inherited Create;
FParent := AParent;
end;
procedure TLocationListener.onLocationChanged(location: JLocation);
begin
FParent.onLocationChanged(location);
end;
procedure TLocationListener.onProviderDisabled(provider: JString);
begin
end;
procedure TLocationListener.onProviderEnabled(provider: JString);
begin
end;
procedure TLocationListener.onStatusChanged(provider: JString; status: Integer; extras: JBundle);
begin
end;
destructor TForm1.Destroy;
begin
if Assigned(locationListener) then
FLocationManager.removeUpdates(locationListener);
inherited;
end;
procedure TForm1.onLocationChanged(location: JLocation);//位置發(fā)生變化時(shí),顯示經(jīng)、緯度
begin
if Assigned(location) then
begin
//variabili da recuperare dal sensore
Label4.Text := location.getLatitude.ToString;
Label5.Text := location.getLongitude.ToString;
Label6.Text := location.getAltitude.ToString;
Label8.Text := location.getSpeed.ToString;
Label10.Text := location.getTime.ToString;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var LocationManagerService: JObject;
location : JLocation;
begin
if not Assigned(FLocationManager) then
begin
LocationManagerService := SharedActivityContext.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
FLocationManager := TJLocationManager.Wrap((LocationManagerService as ILocalObject).GetObjectID);
if not Assigned(locationListener) then locationListener := TLocationListener.Create(self);
FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER, 1000, 0, locationListener, TJLooper.JavaClass.getMainLooper);
// 監聽(tīng)狀態(tài)
// 綁定監聽(tīng),有4個(gè)參數
// 參數1,設備:有GPS_PROVIDER和NETWORK_PROVIDER兩種
// 參數2,位置信息更新周期,單位毫秒
// 參數3,位置變化最小距離:當位置距離變化超過(guò)此值時(shí),將更新位置信息
// 參數4,監聽(tīng)
// 備注:參數2和3,如果參數3不為0,則以參數3為準;參數3為0,則通過(guò)時(shí)間來(lái)定時(shí)更新;兩者為0,則隨時(shí)刷新
// 1秒更新一次,或最小位移變化超過(guò)1米更新一次;
// 注意:此處更新準確度非常低,推薦在service里面啟動(dòng)一個(gè)Thread,在run中sleep(10000);然后執行handler.sendMessage(),更新位置
end;
FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER);
FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.NETWORK_PROVIDER);
onLocationChanged(location);
end