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/qiufeng2014/p/3698926.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值