天天看点

Delphi XE8安卓(android)定位纠偏、地图简单调用、检测开启系统GPS



该Demo是为后期开发做的功能测试,功能代码也很简单,尽量做到一看即懂!

感谢Q群【①FireMonkey[移动开发]】165232328 里的网友们交流与指导,如果你有更好的实现方式或代码优化欢迎你进入群内或留言交流!

 已实现功能

1.获取手机GPS定位得到的坐标

2.纠偏系统获取的坐标为腾讯地图坐标

3.用纠偏后的坐标简单调用腾讯地图并定位

4.判断开启系统是否打开定位,如未打开则弹出系统位置服务设置界面

 存在的问题

获取到坐标之后WebBrowser中的地图会重新打开一次

定位开关按钮不会根据系统GPS是否开启而开启、关闭

下面贴出的为主要功能代码,如有需要可以通过文章后面的云盘地址下载。

// 开发调试环境: win8.1+Delphi XE8+TCL M3GS(android 5.0.2 X64)
// 已实现功能
// 1.获取手机GPS定位得到的坐标
// 2.纠偏系统获取的坐标为腾讯地图坐标
// 3.用纠偏后的坐标简单调用腾讯地图并定位
// 4.判断开启系统是否打开定位,如未打开则弹出系统位置服务设置界面
// 分享人:[绵阳]炒蛋
// 感谢: [深圳]/tp机器猫(5909386) 提供的GLGSPCorrect翻译并提供的【GPS 纠偏及逆转】单元文件
// 感谢: [鞍山]古飞(8444243) 提供的【弹出系统位置服务设置界面】代码
unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Sensors,
  System.Sensors.Components, FMX.WebBrowser, FMX.StdCtrls, FMX.ListBox,
  FMX.Controls.Presentation, FMX.Layouts, FMX.ScrollBox, FMX.Memo;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    ListBoxHeader1: TListBoxHeader;
    Label1: TLabel;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ListBoxItem3: TListBoxItem;
    Switch1: TSwitch;
    Label2: TLabel;
    Label3: TLabel;
    WebBrowser1: TWebBrowser;
    LocationSensor1: TLocationSensor;
    Memo1: TMemo;
    procedure Switch1Switch(Sender: TObject);
    procedure LocationSensor1LocationChanged(Sender: TObject;
      const OldLocation, NewLocation: TLocationCoord2D);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

uses GLGSPCorrect, // GPS 纠偏及逆转单元文件
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.Helpers,
  Androidapi.JNI.Provider;

procedure TForm1.LocationSensor1LocationChanged(Sender: TObject;
  const OldLocation, NewLocation: TLocationCoord2D);
// 申明腾讯地图地址为常量
const
  sTencentMapURL: String = 'http://apis.map.qq.com/uri/v1/geocoder?coord=%s,%s';
var
  lat: Double;
  lon: Double;
begin
  // 标签控件显示经度和纬度
  Label2.Text := NewLocation.Latitude.ToString;
  Label3.Text := NewLocation.Longitude.ToString;
  // 赋值给经度变量和纬度变量
  lat := NewLocation.Latitude;
  lon := NewLocation.Longitude;
  // 纠偏GPS坐标为腾讯坐标
  GLGSPCorrect.TGLGPSCorrect.GPS_to_Google(lat, lon);
  // web控件定位并显示纠偏后的地图
  WebBrowser1.Navigate(Format(sTencentMapURL, [lat.ToString, lon.ToString]));
end;

procedure TForm1.Switch1Switch(Sender: TObject);
var
  Provider: string;
  Settings_secure: TJSettings_Secure;
  Intent: JIntent;
begin
  // 获取已开启的定位方式(gps,network)
  Provider := JStringToString(Settings_secure.JavaClass.getString
    (SharedActivityContext.getContentResolver,
    TJSettings_system.JavaClass.LOCATION_PROVIDERS_ALLOWED));
  // 输出获取到的定位方式
  Memo1.Lines.Add(Provider);
  // 判断返回的定位方式中是否有GPS或network定位
  if pos('gps', Provider) or pos('network', Provider) = 0 then
  begin
    // 如无定位则弹出位置设置界面
    Intent := TJIntent.Create;
    Intent.setAction(TJSettings.JavaClass.ACTION_LOCATION_SOURCE_SETTINGS);
    SharedActivity.startActivity(Intent);
  end;
  // 通过滑动按钮控制GPS控件
  LocationSensor1.Active := Switch1.IsChecked;
end;

end.
           
Delphi XE8安卓(android)定位纠偏、地图简单调用、检测开启系统GPS

源代码下载:

链接:腾讯微云(密码:vr9x)

APP下载:

链接:腾讯微云(密码:8B2Z)

PS:地图纠偏单元为防止查水表请下载后查阅。如果以上链接失效请进入群共享下载。