首页 > 代码库 > delphi xe6 调用java原生GPS的方法
delphi xe6 调用java原生GPS的方法
如果用xe6自带的LocationSensor控件,默认优先使用网络位置,为了直接使用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);
end;
FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER);
FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.NETWORK_PROVIDER);
onLocationChanged(location);
end
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。