分享

Delphi XE10 使用百度定位SDK Jar包进行定位

 windblast 2018-01-18
                 原创 2017年04月01日 02:36:01




Delphi XE10 在推出了一份代码, 编译后到Android, iOS, windows等环境下后, 从来没接触过Delphi的我, 被赶鸭子上架来做Delphi的开发.

尝试在Delphi XE10中引用第三方Jar包时, 遇到了一些问题, 折腾了2天多才解决, 现总结到这里..

重点(敲黑板): 

使用第三方Jar包的过程其实挺简单, 但是一直遇到"Java class XXX Could not be found"的错误:

这是因为虽然在Delphi工程中包含了第三方Jar包, 但是打成apk包的时候, 并没有把第三方Jar包的内容打入APK包中, 下面是示例调用Sample.jar, 可以看到Sample.Jar中的文件, 并没有被打入包中:



正确的打包后, apk中的结构应该是如下的:



目前没有找到这个问题的具体原因, 怀疑是跟Delphi XE 10编译环境有关. 
目前的解决方法: 卸载XE10, 然后重装, 重新建立新工程, 发现问题消失...

下面是完整步骤

1.  到百度定位SDK的网站上, 注册并下载百度定位SDK:


2. 使用老猫的JarOrClass2Pas工具, 将百度定位SDK的jar文件, 转换成Pas文件...工具自行百度查找
JarOrClass2Pas_FlyingWang 

3.  将转换后的Pas文件以及其他的百度定位SDK文件放在一起(例如BaiduLBS文件夹中), 拷贝到Delphi工程目录:



5. 在Delphi工程中, 加入Androidapi.JNI.BaiduLBS_Android.pas, 并将BaiduLBS_Android.jar 添加到工程中:


4. 在Delphi代码中引入, 完整的示例代码如下:
  1. unit Unit3;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,  
  7.   FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,  
  8.   FMX.Controls.Presentation, FMX.StdCtrls, FMX.ScrollBox, FMX.Memo,  
  9.   
  10.   Androidapi.JNI.JavaTypes,  
  11.   Androidapi.JNIBridge,  
  12.   AndroidApi.JNI.GraphicsContentViewText,  
  13. {$IF CompilerVersion >= 27.0} // >= XE6  
  14.   Androidapi.Helpers,  
  15. {$ENDIF}  
  16. {$IF CompilerVersion < 28.0} // < XE7  
  17.   FMX.Helpers.Android,  
  18. {$ENDIF}  
  19. Androidapi.JNI.BaiduLBS_Android;  
  20.   
  21. type  
  22.   TForm3 = class(TForm)  
  23.     btnBaidu: TButton;  
  24.     memoOutput: TMemo;  
  25.     procedure btnBaiduClick(Sender: TObject);  
  26.   private  
  27.     BDLocationListener: JBDLocationListener;  
  28.   public  
  29.     { Public declarations }  
  30.   end;  
  31.   
  32.   MyLocationListener = class(TJavaLocal, JBDLocationListener)  
  33.   public  
  34.     function equals(o: JObject): boolean; cdecl;  
  35.     function getClass: Jlang_Class; cdecl;  
  36.     function hashCode: integer; cdecl;  
  37.     procedure notify; cdecl;  
  38.     procedure notifyAll; cdecl;  
  39.     function toString: JString; cdecl;  
  40.     procedure wait; overload; cdecl;  
  41.     procedure wait(millis: Int64); overload; cdecl;  
  42.     procedure wait(millis: Int64; nanos: integer); overload; cdecl;  
  43.     procedure onReceiveLocation(P1: JBDLocation); cdecl;  
  44.     procedure onConnectHotSpotMessage(P1: JString; P2: Integer); cdecl;  
  45.   end;  
  46.   
  47. var  
  48.   Form3: TForm3;  
  49.   
  50. implementation  
  51.   
  52. {$R *.fmx}  
  53.   
  54. function MyLocationListener.equals(o: JObject): boolean;  
  55. begin  
  56.   
  57. end;  
  58.   
  59. function MyLocationListener.getClass: Jlang_Class;  
  60. begin  
  61.   
  62. end;  
  63.   
  64. function MyLocationListener.hashCode: integer;  
  65. begin  
  66.   
  67. end;  
  68.   
  69. procedure MyLocationListener.notify;  
  70. begin  
  71.   
  72. end;  
  73.   
  74. procedure MyLocationListener.notifyAll;  
  75. begin  
  76.   
  77. end;  
  78.   
  79. procedure MyLocationListener.onReceiveLocation(P1: JBDLocation);  
  80. begin  
  81.     Form3.memoOutput.Lines.Add('recv: ' + P1.getLongitude().ToString() + ', '  
  82.        + P1.getLatitude().ToString());  
  83. end;  
  84.   
  85. procedure MyLocationListener.onConnectHotSpotMessage(P1: JString; P2: Integer);  
  86. begin  
  87.   
  88. end;  
  89.   
  90. function MyLocationListener.toString: JString;  
  91. begin  
  92.   
  93. end;  
  94.   
  95. procedure MyLocationListener.wait;  
  96. begin  
  97.   
  98. end;  
  99.   
  100. procedure MyLocationListener.wait(millis: Int64);  
  101. begin  
  102.   
  103. end;  
  104.   
  105. procedure MyLocationListener.wait(millis: Int64; nanos: integer);  
  106. begin  
  107.   
  108. end;  
  109.   
  110. procedure TForm3.btnBaiduClick(Sender: TObject);  
  111. var  
  112.   jlc : JLocationClient;  
  113.   locaOpt: JLocationClientOption;  
  114. begin  
  115.   try  
  116.     locaOpt := TJLocationClientOption.JavaClass.init();  
  117.     locaOpt.setCoorType(StringToJString('bd09ll'));  
  118.     locaOpt.setScanSpan(5000);  
  119.     jlc := TJLocationClient.JavaClass.init(TAndroidHelper.Context, locaOpt);  
  120.     BDLocationListener := MyLocationListener.Create;  
  121.     jlc.registerLocationListener(BDLocationListener);  
  122.     jlc.start();  
  123.   except on E:Exception do  
  124.     Self.memoOutput.Lines.Add(E.Message);  
  125.   end;  
  126. end;  
  127. end.  


5. 在Project -> Deployment中, 添加百度地图依赖的so文件:


6. 根据百度定位SDK的使用说明, 以及该android的信息, 在百度定位SDK中, 注册自己的app, 获取到API_KEY


7. 将API_KEY按照AndroidManifest.xml 中的样子, 写入到Delphi项目的AndroidManifest.template.xml文件中:


8. 以示例代码为例, 点击按钮就可以进行定位了:

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章