delphi XE6 手机gps定位  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi XE6 手机gps定位


Delphi提供了LocationSensor,利用手机gps实现定位服务,同时提供了例子:LocationDemoProject,在C:\Users\Public\Documents\Embarcadero\Studio\14.0\Samples\Object Pascal\Mobile Samples\Device Sensors and Services\LocationDemo下可以找到。

//为LocationSensor实现LocationChanged事件,当打开LocationSensor时或位置发生变化,触发该事件,通过该事件,可以取得最新的位置信息,下面的代码是例子实现的:
procedure TForm1.LocationSensor1LocationChanged(Sender: TObject;
  const OldLocation, NewLocation: TLocationCoord2D);
var
  URLString: String;
  LSettings: TFormatSettings;
  LDecSeparator : Char;
begin
  LDecSeparator := FormatSettings.DecimalSeparator;
  LSettings := FormatSettings;
  try
    FormatSettings.DecimalSeparator := '.';
    // 显示当前位置
    ListBoxItemLatitude.ItemData.Detail  := Format('%2.6f', [NewLocation.Latitude]);
    ListBoxItemLongitude.ItemData.Detail := Format('%2.6f', [NewLocation.Longitude]);

    // 用 Google Maps显示
    URLString := Format('https://maps.google.com/maps?q=%2.6f,%2.6f', [ NewLocation.Latitude, NewLocation.Longitude]);
  finally
    FormatSettings.DecimalSeparator := LDecSeparator;
  end;
  WebBrowser1.Navigate(URLString);

  // 利用 TGeocoder进行地名解析
  //建立TGeocode实像,并为之设置事件代码
  if not Assigned(FGeocoder) then
  begin
    if Assigned(TGeocoder.Current) then
      FGeocoder := TGeocoder.Current.Create;
    if Assigned(FGeocoder) then
      FGeocoder.OnGeocodeReverse := OnGeocodeReverseEvent;
  end;

  //转换成地名
  if Assigned(FGeocoder) and not FGeocoder.Geocoding then
    FGeocoder.GeocodeReverse(NewLocation);
end;

//这是GeocodeReverseEvent代码:
procedure TForm1.OnGeocodeReverseEvent(const Address: TCivicAddress);
begin
  ListBoxItemAdminArea.ItemData.Detail       := Address.AdminArea;
  ListBoxItemCountryCode.ItemData.Detail     := Address.CountryCode;
  ListBoxItemCountryName.ItemData.Detail     := Address.CountryName;
  ListBoxItemFeatureName.ItemData.Detail     := Address.FeatureName;
  ListBoxItemLocality.ItemData.Detail        := Address.Locality;
  ListBoxItemPostalCode.ItemData.Detail      := Address.PostalCode;
  ListBoxItemSubAdminArea.ItemData.Detail    := Address.SubAdminArea;
  ListBoxItemSubLocality.ItemData.Detail     := Address.SubLocality;
  ListBoxItemSubThoroughfare.ItemData.Detail := Address.SubThoroughfare;
  ListBoxItemThoroughfare.ItemData.Detail    := Address.Thoroughfare;
end;
通过这个例子,可以看到,利用dephi实现位置及地图服务是件很轻松的事,基本上没有多少代码,就可以取得当前的位置,并利用地图显示及转换地址名称。

这个控制优先使用网络定位,如果优先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

 


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.039880037307739 seconds