該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.

源代碼下載下傳:
連結:騰訊微雲(密碼:vr9x)
APP下載下傳:
連結:騰訊微雲(密碼:8B2Z)
PS:地圖糾偏單元為防止查水表請下載下傳後查閱。如果以上連結失效請進入群共享下載下傳。