delphi xe6 调用java GPS的方法

如果用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);
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);//位置发生变化时,显示经、纬度
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
原文地址:https://www.cnblogs.com/lantianhf/p/4335682.html