zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    一个简单的链表结构 分类: C/C++ 数据结构与算法 2015-06-14 16:39 129人阅读 评论(1) 收藏
    整数与浮点数的二进制表示方式 分类: C/C++ 2015-06-13 15:45 54人阅读 评论(0) 收藏
    对string的一些扩展函数 分类: C/C++ 2015-06-12 15:43 170人阅读 评论(1) 收藏
    SQL常用操作 2015-06-12 12:43 20人阅读 评论(0) 收藏
    Google C++编程风格 2015-06-11 11:25 36人阅读 评论(0) 收藏
    C++头文件编译问题 分类: C/C++ 2015-06-10 15:48 32人阅读 评论(0) 收藏
    服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
    解决time_wait过多的问题
    Gdb调试多进程程序
    linux查看硬件配置命令
  • 原文地址:https://www.cnblogs.com/qiufeng2014/p/3698926.html
Copyright © 2011-2022 走看看