標籤:
如果用xe6內建的LocationSensor控制項,預設優先使用網路位置,網路位置定位精度不準確,不能滿足高精度定位的要求。但xe6內建的LocationSensor控制項不能指定網路定位優先還是GPS定位優先,如果調用java API是可以指定優先使用GPS定位,以下代碼經實測證實是可以直接指定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);beginend; procedure TLocationListener.onProviderEnabled(provider: JString);beginend; procedure TLocationListener.onStatusChanged(provider: JString; status: Integer; extras: JBundle);beginend; destructor TForm1.Destroy;begin if Assigned(locationListener) then FLocationManager.removeUpdates(locationListener); inherited;end; procedure TForm1.onLocationChanged(location: JLocation);//位置發生變化時,顯示經、緯度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); // 監聽狀態 // 綁定監聽,有4個參數 // 參數1,裝置:有GPS_PROVIDER和NETWORK_PROVIDER兩種 // 參數2,位置資訊更新周期,單位毫秒 // 參數3,位置變化最小距離:當位置距離變化超過此值時,將更新位置資訊 // 參數4,監聽 // 備忘:參數2和3,如果參數3不為0,則以參數3為準;參數3為0,則通過時間來定時更新;兩者為0,則隨時重新整理 // 1秒更新一次,或最小位移變化超過1米更新一次; // 注意:此處更新準確度非常低,推薦在service裡面啟動一個Thread,在run中sleep(10000);然後執行handler.sendMessage(),更新位置 end;FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER); FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.NETWORK_PROVIDER); onLocationChanged(location);end
delphi xe6 調用java GPS的方法