該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:地圖糾偏單元爲防止查水錶請下載後查閱。如果以上鍊接失效請進入羣共享下載。