├── .gitignore
├── README.md
├── plugin.xml
├── src
└── android
│ ├── BaiduGeolocation.java
│ ├── BaiduLBS_Android.jar
│ └── liblocSDK4d.so
└── www
└── BaiduGeolocation.js
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
3 | # Mobile Tools for Java (J2ME)
4 | .mtj.tmp/
5 |
6 | # Package Files #
7 | *.war
8 | *.ear
9 |
10 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
11 | hs_err_pid*
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Phonegap-BaiduGeolocation-Plugin
2 | ================================
3 |
4 | 这是一个采用百度定位SDK 4.2 版的Cordova/Phonegap地理定位插件,它是在 [andybuit](https://github.com/andybuit/phonegap-baidu-location) 和 [DoubleSpout](https://github.com/DoubleSpout/phonegap_baidu_sdk_location) 的成果的基础上所做的开发。非常感谢两位作者!
5 |
6 | 由于 DoubleSpout 的插件基于百度地图定位SDK 4.0 版,而新版本是4.2,因此本插件更新了SDK的版本,并对Java端的JS端做了相应的修改。亲测可以在cordova 4.1.2上运行。
7 |
8 | ## 安装方法
9 |
10 | ```shell
11 | cordova plugin add https://github.com/darkgeek/Phonegap-BaiduGeolocation-Plugin.git
12 | ```
13 |
14 | ## 使用方法
15 |
16 | 1) 配置您项目目录下`plugins/edu.hdu.darkgeek.baidu_geolocation/plugin.xml`文件,修改[ak密钥](http://lbsyun.baidu.com/apiconsole/key?application=key)为您自己的密钥值:
17 |
18 | ```xml
19 |
20 |
21 |
22 | ```
23 |
24 | 2) 接下来,您可以通过JavaScript调用`getCurrentPosition`方法来获取经纬度信息:
25 |
26 | ``` javascript
27 | window.BaiduGeolocation.getCurrentPosition(successCallback, errorCallback);
28 | ```
29 |
30 | 其中,`successCallback`是在成功获取经纬度后触发的回调方法,该方法使用一个包含位置信息的对象作为输入参数;`errorCallback`是在获取失败后触发的回调方法,其参数为一个包含错误信息的对象。
31 |
32 | ## 使用举例
33 |
34 | ```javascript
35 | var onSuccess = function(position) {
36 | console.log("Get location: " + position.coords.latitude + "," + position.coords.longitude);
37 | };
38 |
39 | var onError = function(err) {
40 | console.error("Failed to get location: " + err.message);
41 | };
42 |
43 | window.BaiduGeolocation.getCurrentPosition(onSuccess, onError);
44 | ```
45 |
46 | ## 疑难问题
47 |
48 | Q: 我修改了`plugin.xml`,在其中添加了我的密钥值,为什么通过`cordova build`后,`AndroidManifest.xml`文件里的密钥值仍是旧的?
49 |
50 | A: 您可以修改工程目录下`plugins\android.json` 文件:
51 |
52 | ```xml
53 | ...skip...
54 | "AndroidManifest.xml": {
55 | "parents": {
56 | "/*": [
57 | ...skip...
58 | {
59 | "xml": "",
60 | "count": 1
61 | }
62 | ],
63 | "/manifest/application": [
64 | {
65 | "xml": "",
66 | "count": 1
67 | },
68 | {
69 | "xml": "",
70 | "count": 1
71 | }
72 | ]
73 | }
74 | }
75 | ...skip...
76 | ```
77 |
78 | 修改`com.baidu.lbsapi.API_KEY`所对应的`android:value`为您的密钥值。参考 [issue #2](../../issues/2)。
79 |
80 | ## TODO
81 |
82 | 1. 加入测试用例,确保插件的可靠性。
83 | 2. 进一步将此API向W3C规定的geolocation接口靠拢。
84 |
85 | ## 百度地图API使用条款
86 |
87 | 详见[使用条款](http://developer.baidu.com/map/index.php?title=open/law)。
88 |
--------------------------------------------------------------------------------
/plugin.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | BaiduGeolocation
6 | Get your location via Baidu Map SDK
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/src/android/BaiduGeolocation.java:
--------------------------------------------------------------------------------
1 | package edu.hdu.darkgeek.baidu_geolocation;
2 |
3 | import org.apache.cordova.CallbackContext;
4 | import org.apache.cordova.CordovaPlugin;
5 | import org.apache.cordova.PluginResult;
6 |
7 | import org.json.JSONArray;
8 | import org.json.JSONException;
9 | import org.json.JSONObject;
10 |
11 | import com.baidu.location.BDLocation;
12 | import com.baidu.location.BDLocationListener;
13 | import com.baidu.location.LocationClient;
14 | import com.baidu.location.LocationClientOption;
15 |
16 | import java.util.HashMap;
17 | import java.util.Map;
18 |
19 | import android.util.Log;
20 |
21 | /**
22 | * A phonegap plugin to get the geolocation from Baidu Map SDK.
23 | * Adapted from https://github.com/DoubleSpout/phonegap_baidu_sdk_location.
24 | *
25 | * @Author https://github.com/DoubleSpout (Original version using Baidu Map SDK 4.0, Thanks!)
26 | * @Author https://github.com/darkgeek (Bump the Baidu Map SDK to 4.2 and fix bugs)
27 | */
28 | public class BaiduGeolocation extends CordovaPlugin {
29 |
30 | private static final String GET_GEOLOCATION_ACTION = "getCurrentPosition";
31 | public CallbackContext callbackContext;
32 | public LocationClient locationClient = null;
33 | public BDLocationListener myListener = null;
34 | public JSONObject jsonObj = new JSONObject();
35 | private static final Map ERROR_MESSAGE_MAP = new HashMap();
36 | private static final String DEFAULT_ERROR_MESSAGE = "服务端定位失败";
37 |
38 | static {
39 | ERROR_MESSAGE_MAP.put(61, "GPS定位结果");
40 | ERROR_MESSAGE_MAP.put(62, "扫描整合定位依据失败。此时定位结果无效");
41 | ERROR_MESSAGE_MAP.put(63, "网络异常,没有成功向服务器发起请求。此时定位结果无效");
42 | ERROR_MESSAGE_MAP.put(65, "定位缓存的结果");
43 | ERROR_MESSAGE_MAP.put(66, "离线定位结果。通过requestOfflineLocaiton调用时对应的返回结果");
44 | ERROR_MESSAGE_MAP.put(67, "离线定位失败。通过requestOfflineLocaiton调用时对应的返回结果");
45 | ERROR_MESSAGE_MAP.put(68, "网络连接失败时,查找本地离线定位时对应的返回结果。");
46 | ERROR_MESSAGE_MAP.put(161, "表示网络定位结果");
47 | };
48 |
49 | public String getErrorMessage(int locationType) {
50 | String result = ERROR_MESSAGE_MAP.get(locationType);
51 | if (result == null) {
52 | result = DEFAULT_ERROR_MESSAGE;
53 | }
54 | return result;
55 | }
56 |
57 | @Override
58 | public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) {
59 | boolean result = false;
60 | setCallbackContext(callbackContext);
61 | if (GET_GEOLOCATION_ACTION.equals(action)) {
62 | locationClient = new LocationClient(cordova.getActivity());
63 | myListener = new MyLocationListener();
64 | locationClient.registerLocationListener(myListener);
65 | LocationClientOption option = new LocationClientOption();
66 |
67 | option.setOpenGps(true); // 打开GPS定位
68 | option.setCoorType("bd09ll");// 设置返回百度经纬度
69 | //option.setCoorType("gcj02");// 设置返回国家测绘局加密后的坐标
70 | option.setProdName("BaiduLoc");
71 | locationClient.setLocOption(option);
72 |
73 | Log.d("BaiduGeolocation", "Start Geolocation service.");
74 | locationClient.start(); // 开启百度地图SDK
75 | locationClient.requestLocation();// 发起请求
76 |
77 | result = true;
78 | }
79 | else {
80 | callbackContext.error(PluginResult.Status.INVALID_ACTION.toString());
81 | }
82 |
83 | return result;
84 | }
85 |
86 | public class MyLocationListener implements BDLocationListener {
87 | @Override
88 | public void onReceiveLocation(BDLocation location) {
89 | if (location == null)
90 | return;
91 | try {
92 | JSONObject coords = new JSONObject();
93 | coords.put("latitude", location.getLatitude());
94 | coords.put("longitude", location.getLongitude());
95 | coords.put("radius", location.getRadius());
96 | jsonObj.put("coords", coords);
97 | int locationType = location.getLocType();
98 | jsonObj.put("locationType", locationType);
99 | jsonObj.put("code", locationType);
100 | jsonObj.put("message", getErrorMessage(locationType));
101 | switch (location.getLocType()) {
102 | case BDLocation.TypeGpsLocation:
103 | coords.put("speed", location.getSpeed());
104 | coords.put("altitude", location.getAltitude());
105 | jsonObj.put("SatelliteNumber",
106 | location.getSatelliteNumber());
107 | break;
108 | case BDLocation.TypeNetWorkLocation:
109 | jsonObj.put("addr", location.getAddrStr());
110 | break;
111 | }
112 | Log.d("BaiduGeolocation", "run: " + jsonObj.toString());
113 | callbackContext.success(jsonObj);
114 | } catch (JSONException e) {
115 | callbackContext.error(e.getMessage());
116 | }
117 |
118 | locationClient.stop();
119 | Log.d("BaiduGeolocation", "Stop Geolocation service.");
120 | }
121 |
122 | public void onReceivePoi(BDLocation poiLocation) {
123 | // TODO
124 | }
125 | }
126 |
127 | @Override
128 | public void onDestroy() {
129 | if (locationClient != null && locationClient.isStarted()) {
130 | locationClient.stop();
131 | locationClient = null;
132 | Log.d("BaiduGeolocation", "Stop Geolocation service.");
133 | }
134 | super.onDestroy();
135 | }
136 |
137 | public CallbackContext getCallbackContext() {
138 | return callbackContext;
139 | }
140 |
141 | public void setCallbackContext(CallbackContext callbackContext) {
142 | this.callbackContext = callbackContext;
143 | }
144 | }
145 |
--------------------------------------------------------------------------------
/src/android/BaiduLBS_Android.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darkgeek/Phonegap-BaiduGeolocation-Plugin/b178b02881b5855cb5c777e43875e19e1c685f6b/src/android/BaiduLBS_Android.jar
--------------------------------------------------------------------------------
/src/android/liblocSDK4d.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darkgeek/Phonegap-BaiduGeolocation-Plugin/b178b02881b5855cb5c777e43875e19e1c685f6b/src/android/liblocSDK4d.so
--------------------------------------------------------------------------------
/www/BaiduGeolocation.js:
--------------------------------------------------------------------------------
1 | window.BaiduGeolocation = {
2 | getCurrentPosition: function(success, error) {
3 | cordova.exec(success, error, "BaiduGeolocation", "getCurrentPosition",[]);
4 | },
5 | }
6 |
7 | module.exports = BaiduGeolocation;
8 |
--------------------------------------------------------------------------------