capacitor-baidu-location 0.0.5 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -64
- package/android/build.gradle +14 -7
- package/android/src/main/kotlin/com/hybrid/baidu/location/CPBaiduLocation.kt +92 -70
- package/android/src/main/kotlin/com/hybrid/baidu/location/CPBaiduLocationPlugin.kt +49 -42
- package/dist/docs.json +9 -0
- package/dist/esm/definitions.d.ts +4 -0
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.d.ts +12 -3
- package/dist/esm/web.js +69 -19
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +69 -19
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +69 -19
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CPBaiduLocationPlugin/CPBaiduLocation.swift +3 -3
- package/package.json +1 -4
- package/Package.swift +0 -44
- package/android/libs/BaiduLBS_Android.jar +0 -0
- package/android/libs/arm64-v8a/liblocSDK8b.so +0 -0
- package/android/libs/x86_64/liblocSDK8b.so +0 -0
- package/ios/Frameworks/BMKLocationKit.xcframework/Info.plist +0 -43
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/BMKLocationKit +0 -0
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/BMKLocationKit.xcprivacy +0 -64
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKGeoFenceManager.h +0 -168
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKGeoFenceRegion.h +0 -114
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocation.h +0 -112
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationAuth.h +0 -71
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationComponent.h +0 -16
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationKitVersion.h +0 -30
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationManager.h +0 -303
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationPoi.h +0 -42
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationPoiRegion.h +0 -35
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationReGeocode.h +0 -69
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Info.plist +0 -36
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Modules/module.modulemap +0 -6
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/BMKLocationKit +0 -0
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKGeoFenceManager.h +0 -168
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKGeoFenceRegion.h +0 -114
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocation.h +0 -112
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationAuth.h +0 -71
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationComponent.h +0 -16
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationKitVersion.h +0 -30
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationManager.h +0 -303
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationPoi.h +0 -42
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationPoiRegion.h +0 -35
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationReGeocode.h +0 -69
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Info.plist +0 -36
- package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Modules/module.modulemap +0 -6
package/README.md
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
# Capacitor Baidu Location Plugin
|
|
2
2
|
|
|
3
|
-
一个支持 Capacitor 7
|
|
3
|
+
一个支持 Capacitor 7 的百度地图定位插件,iOS 使用 cocoapods 集成,Android 使用 gradle 集成。
|
|
4
4
|
|
|
5
5
|
## 功能特性
|
|
6
6
|
|
|
7
|
-
- ✅
|
|
8
|
-
- ✅
|
|
9
|
-
- ✅
|
|
10
|
-
- ✅ 支持动态设置 API Key
|
|
11
|
-
- ✅ 支持定位权限检查和请求
|
|
7
|
+
- ✅ 支持单次定位, 可通过配置获取地址信息和位置描述
|
|
8
|
+
- ✅ 直接配置或动态设置 API Key
|
|
9
|
+
- ✅ 定位权限检查和请求
|
|
12
10
|
- ✅ 支持坐标类型设置和获取
|
|
13
|
-
- ✅ 支持 AK 验证状态检查
|
|
14
11
|
- ✅ 基于 Capacitor 7
|
|
15
12
|
|
|
16
13
|
## 安装
|
|
@@ -23,6 +20,7 @@ npx cap sync
|
|
|
23
20
|
## 开发依赖
|
|
24
21
|
|
|
25
22
|
### Android 开发依赖
|
|
23
|
+
|
|
26
24
|
- Java版本 : 21
|
|
27
25
|
- Kotlin版本 : 1.9.25
|
|
28
26
|
- Gradle版本 : 8.7.2
|
|
@@ -30,11 +28,13 @@ npx cap sync
|
|
|
30
28
|
- 第三方库 : OkHttp3 3.14.9
|
|
31
29
|
|
|
32
30
|
### iOS 开发依赖
|
|
31
|
+
|
|
33
32
|
- Xcode 16 +
|
|
34
|
-
- Swift 5.
|
|
33
|
+
- Swift 5.1 +
|
|
35
34
|
- iOS 14 +
|
|
36
35
|
|
|
37
36
|
### Capacitor
|
|
37
|
+
|
|
38
38
|
- TypeScript 5.1+
|
|
39
39
|
- Capacitor 7.+
|
|
40
40
|
- NodeJS 20+
|
|
@@ -43,16 +43,14 @@ npx cap sync
|
|
|
43
43
|
|
|
44
44
|
### 配置文件设置(推荐)
|
|
45
45
|
|
|
46
|
-
插件支持读取配置,这是推荐的配置方式,便于集中管理和构建时配置。
|
|
46
|
+
插件支持读取配置,这是推荐的配置方式,便于集中管理和构建时配置。web端则直接通过api设置,因为无法在web端读取capacitor配置的内容。
|
|
47
47
|
|
|
48
48
|
**capacitor.config.ts**
|
|
49
|
+
|
|
49
50
|
```typescript
|
|
50
51
|
import { CapacitorConfig } from '@capacitor/cli';
|
|
51
52
|
|
|
52
53
|
const config: CapacitorConfig = {
|
|
53
|
-
appId: 'com.example.app',
|
|
54
|
-
appName: 'Example App',
|
|
55
|
-
webDir: 'dist',
|
|
56
54
|
plugins: {
|
|
57
55
|
CPBaiduLocation: {
|
|
58
56
|
iOSAK: 'your-ios-api-key',
|
|
@@ -72,6 +70,7 @@ export default config;
|
|
|
72
70
|
- **方式二:配置文件设置**:在 `capacitor.config.ts` 中配置 `androidAK` ,之后一样要调用 `setAK` 方法,只是不需要传参.
|
|
73
71
|
|
|
74
72
|
**注意:AndroidManifest.xml 需要配置占位值**:
|
|
73
|
+
|
|
75
74
|
```xml
|
|
76
75
|
<application>
|
|
77
76
|
<meta-data
|
|
@@ -80,8 +79,9 @@ export default config;
|
|
|
80
79
|
</application>
|
|
81
80
|
```
|
|
82
81
|
|
|
83
|
-
|
|
84
|
-
使用定位SDK,需在AndroidManifest.xml文件中Application标签中声明service组件,每个App拥有自己单独的定位service,代码如下:
|
|
82
|
+
1. **权限配置**
|
|
83
|
+
使用定位SDK,需在AndroidManifest.xml文件中Application标签中声明service组件,每个App拥有自己单独的定位service,代码如下:
|
|
84
|
+
|
|
85
85
|
```xml
|
|
86
86
|
<application>
|
|
87
87
|
<service android:name="com.baidu.location.f"
|
|
@@ -91,6 +91,7 @@ export default config;
|
|
|
91
91
|
```
|
|
92
92
|
|
|
93
93
|
除添加service组件外,使用定位SDK还需添加如下权限:
|
|
94
|
+
|
|
94
95
|
```xml
|
|
95
96
|
<!-- 这个权限用于进行网络定位-->
|
|
96
97
|
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
|
|
@@ -114,9 +115,9 @@ export default config;
|
|
|
114
115
|
- 百度地图 API Key 可以通过2种方式配置(优先级从高到低):
|
|
115
116
|
- **方式一:动态设置**:在应用代码中调用 `setAK` 方法设置
|
|
116
117
|
- **方式二:配置文件设置**:在 `capacitor.config.ts` 中配置 `iOSAK` ,之后一样要调用 `setAK` 方法,只是不需要传参.
|
|
117
|
-
|
|
118
118
|
2. **权限配置**
|
|
119
|
-
在 `Info.plist` 文件中添加以下权限描述:
|
|
119
|
+
在 `Info.plist` 文件中添加以下权限描述:
|
|
120
|
+
|
|
120
121
|
```xml
|
|
121
122
|
<key>NSLocationWhenInUseUsageDescription</key>
|
|
122
123
|
<string>需要获取您的位置信息</string>
|
|
@@ -128,7 +129,7 @@ export default config;
|
|
|
128
129
|
|
|
129
130
|
### Web 配置
|
|
130
131
|
|
|
131
|
-
仅支持 setAK 方法传参,web端使用百度的JavaScript API GL
|
|
132
|
+
仅支持 setAK 方法传参,web端使用百度的 JavaScript API GL ,
|
|
132
133
|
如果没传AK,默认使用浏览器内置的地理定位 API。
|
|
133
134
|
|
|
134
135
|
## API
|
|
@@ -138,17 +139,20 @@ export default config;
|
|
|
138
139
|
设置百度地图 API Key。可以覆盖配置文件中的设置,优先级最高。
|
|
139
140
|
|
|
140
141
|
**参数**
|
|
141
|
-
|
|
142
|
-
|
|
142
|
+
|
|
143
|
+
| 参数 | 类型 | 描述 |
|
|
144
|
+
| ---- | -------- | ---------------- |
|
|
143
145
|
| `ak` | `string` | 百度定位的各平台 API Key |
|
|
144
146
|
|
|
145
147
|
**返回值**
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
|
149
|
-
| `
|
|
148
|
+
|
|
149
|
+
| 字段 | 类型 | 描述 |
|
|
150
|
+
| -------------- | --------- | ---------- |
|
|
151
|
+
| `success` | `boolean` | 设置是否成功 |
|
|
152
|
+
| `errorMessage` | `string` | 错误信息(如果失败) |
|
|
150
153
|
|
|
151
154
|
**说明**
|
|
155
|
+
|
|
152
156
|
- 调用此方法会覆盖配置文件中的 AK 设置
|
|
153
157
|
- 可以在应用运行时动态切换 AK
|
|
154
158
|
|
|
@@ -157,18 +161,20 @@ export default config;
|
|
|
157
161
|
检查 API Key 验证状态。
|
|
158
162
|
|
|
159
163
|
**返回值**
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
|
163
|
-
| `
|
|
164
|
+
|
|
165
|
+
| 字段 | 类型 | 描述 |
|
|
166
|
+
| -------------- | --------- | ---------- |
|
|
167
|
+
| `valid` | `boolean` | AK 是否有效 |
|
|
168
|
+
| `errorMessage` | `string` | 错误信息(如果无效) |
|
|
164
169
|
|
|
165
170
|
### `checkPermission()`
|
|
166
171
|
|
|
167
172
|
检查定位权限状态。
|
|
168
173
|
|
|
169
174
|
**返回值**
|
|
170
|
-
|
|
171
|
-
|
|
175
|
+
|
|
176
|
+
| 字段 | 类型 | 描述 |
|
|
177
|
+
| --------- | --------- | ------- |
|
|
172
178
|
| `granted` | `boolean` | 权限是否已授予 |
|
|
173
179
|
|
|
174
180
|
### `requestPermission()`
|
|
@@ -176,8 +182,9 @@ export default config;
|
|
|
176
182
|
请求定位权限。
|
|
177
183
|
|
|
178
184
|
**返回值**
|
|
179
|
-
|
|
180
|
-
|
|
185
|
+
|
|
186
|
+
| 字段 | 类型 | 描述 |
|
|
187
|
+
| --------- | --------- | ------- |
|
|
181
188
|
| `granted` | `boolean` | 权限是否已授予 |
|
|
182
189
|
|
|
183
190
|
### `setCoordinateType(options)`
|
|
@@ -185,13 +192,15 @@ export default config;
|
|
|
185
192
|
设置坐标类型。
|
|
186
193
|
|
|
187
194
|
**参数**
|
|
188
|
-
|
|
189
|
-
|
|
195
|
+
|
|
196
|
+
| 参数 | 类型 | 描述 |
|
|
197
|
+
| ------ | ---------------- | ---- |
|
|
190
198
|
| `type` | `CoordinateType` | 坐标类型 |
|
|
191
199
|
|
|
192
200
|
**返回值**
|
|
193
|
-
|
|
194
|
-
|
|
201
|
+
|
|
202
|
+
| 字段 | 类型 | 描述 |
|
|
203
|
+
| --------- | --------- | ------ |
|
|
195
204
|
| `success` | `boolean` | 设置是否成功 |
|
|
196
205
|
|
|
197
206
|
### `getCoordinateType()`
|
|
@@ -199,8 +208,9 @@ export default config;
|
|
|
199
208
|
获取当前坐标类型。
|
|
200
209
|
|
|
201
210
|
**返回值**
|
|
202
|
-
|
|
203
|
-
|
|
211
|
+
|
|
212
|
+
| 字段 | 类型 | 描述 |
|
|
213
|
+
| ------ | ---------------- | ------ |
|
|
204
214
|
| `type` | `CoordinateType` | 当前坐标类型 |
|
|
205
215
|
|
|
206
216
|
### `getCurrentPosition(options?)`
|
|
@@ -208,37 +218,39 @@ export default config;
|
|
|
208
218
|
获取当前位置信息。
|
|
209
219
|
|
|
210
220
|
**参数**
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
|
221
|
+
|
|
222
|
+
| 参数 | 类型 | 描述 | 默认值 |
|
|
223
|
+
| ---------------------- | --------- | -------- | ------- |
|
|
224
|
+
| `needAddress` | `boolean` | 是否获取地址信息 | `false` |
|
|
214
225
|
| `needLocationDescribe` | `boolean` | 是否获取位置描述 | `false` |
|
|
215
226
|
|
|
216
227
|
**返回值**
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
|
220
|
-
| `
|
|
221
|
-
| `
|
|
222
|
-
| `
|
|
223
|
-
| `
|
|
224
|
-
| `
|
|
225
|
-
| `
|
|
226
|
-
| `
|
|
227
|
-
| `
|
|
228
|
-
| `
|
|
229
|
-
| `
|
|
230
|
-
| `
|
|
231
|
-
| `
|
|
232
|
-
| `
|
|
228
|
+
|
|
229
|
+
| 字段 | 类型 | 描述 |
|
|
230
|
+
| ------------------ | -------- | ------- |
|
|
231
|
+
| `latitude` | `number` | 纬度 |
|
|
232
|
+
| `longitude` | `number` | 经度 |
|
|
233
|
+
| `accuracy` | `number` | 定位精度(米) |
|
|
234
|
+
| `address` | `string` | 完整地址 |
|
|
235
|
+
| `country` | `string` | 国家 |
|
|
236
|
+
| `province` | `string` | 省份 |
|
|
237
|
+
| `city` | `string` | 城市 |
|
|
238
|
+
| `district` | `string` | 区县 |
|
|
239
|
+
| `street` | `string` | 街道 |
|
|
240
|
+
| `adcode` | `string` | 行政区划代码 |
|
|
241
|
+
| `town` | `string` | 乡镇 |
|
|
242
|
+
| `locationDescribe` | `string` | 位置描述 |
|
|
243
|
+
| `errorCode` | `number` | 错误码 |
|
|
244
|
+
| `errorMessage` | `string` | 错误信息 |
|
|
233
245
|
|
|
234
246
|
### `CoordinateType` 枚举
|
|
235
247
|
|
|
236
|
-
| 枚举值
|
|
237
|
-
|
|
238
|
-
| `BD09LL` | 百度经纬度坐标
|
|
248
|
+
| 枚举值 | 描述 |
|
|
249
|
+
| -------- | --------- |
|
|
250
|
+
| `BD09LL` | 百度经纬度坐标 |
|
|
239
251
|
| `BD09MC` | 百度墨卡托米制坐标 |
|
|
240
|
-
| `GCJ02`
|
|
241
|
-
| `WGS84`
|
|
252
|
+
| `GCJ02` | 国测局坐标 |
|
|
253
|
+
| `WGS84` | WGS84坐标 |
|
|
242
254
|
|
|
243
255
|
## 使用示例
|
|
244
256
|
|
|
@@ -334,13 +346,11 @@ const getCurrentCoordinateType = async () => {
|
|
|
334
346
|
|
|
335
347
|
1. **API Key 申请**:
|
|
336
348
|
- 请确保在 [百度地图开放平台](https://lbs.baidu.com/) 申请了正确的 API Key
|
|
337
|
-
- Android、iOS、
|
|
349
|
+
- Android、iOS、web 端需要分别申请不同的 API Key
|
|
338
350
|
- 确保 API Key 已启用定位服务
|
|
339
|
-
|
|
340
351
|
2. **权限管理**:
|
|
341
352
|
- 定位功能需要用户授权
|
|
342
353
|
- 请在适当的时机检查和请求权限
|
|
343
|
-
|
|
344
354
|
3. **网络连接**:
|
|
345
355
|
- 获取地址信息和位置描述需要网络连接
|
|
346
356
|
- 基础定位功能可以在离线状态下使用
|
|
@@ -358,15 +368,19 @@ const getCurrentCoordinateType = async () => {
|
|
|
358
368
|
## 故障排除
|
|
359
369
|
|
|
360
370
|
### API Key 设置失败
|
|
371
|
+
|
|
361
372
|
**问题**:调用 `setAK` 方法返回失败
|
|
362
373
|
**解决方案**:
|
|
374
|
+
|
|
363
375
|
- 检查 API Key 是否正确
|
|
364
376
|
- 检查网络连接是否正常
|
|
365
377
|
- 确保 API Key 已在百度地图开放平台启用
|
|
366
378
|
|
|
367
379
|
### 权限请求失败
|
|
380
|
+
|
|
368
381
|
**问题**:用户拒绝了定位权限
|
|
369
382
|
**解决方案**:
|
|
383
|
+
|
|
370
384
|
- 提示用户定位权限的重要性
|
|
371
385
|
- 引导用户在系统设置中手动开启权限
|
|
372
386
|
- 优雅处理无权限的情况
|
|
@@ -377,3 +391,4 @@ const getCurrentCoordinateType = async () => {
|
|
|
377
391
|
- [百度地图 Android 定位 SDK 文档](https://lbs.baidu.com/faq/api?title=android-locsdk/guide/get-location/latlng)
|
|
378
392
|
- [百度地图 iOS 定位 SDK 文档](https://lbs.baidu.com/faq/api?title=ios-locsdk/guide/get-location/once)
|
|
379
393
|
- [Capacitor 插件开发指南](https://capacitorjs.com/docs/plugins)
|
|
394
|
+
|
package/android/build.gradle
CHANGED
|
@@ -14,6 +14,8 @@ buildscript {
|
|
|
14
14
|
repositories {
|
|
15
15
|
google()
|
|
16
16
|
mavenCentral()
|
|
17
|
+
maven { url "https://maven.aliyun.com/repository/google" }
|
|
18
|
+
maven { url "https://maven.aliyun.com/repository/jcenter" }
|
|
17
19
|
}
|
|
18
20
|
dependencies {
|
|
19
21
|
classpath 'com.android.tools.build:gradle:8.7.2'
|
|
@@ -21,6 +23,16 @@ buildscript {
|
|
|
21
23
|
}
|
|
22
24
|
}
|
|
23
25
|
|
|
26
|
+
// 配置所有项目的仓库
|
|
27
|
+
allprojects {
|
|
28
|
+
repositories {
|
|
29
|
+
google()
|
|
30
|
+
mavenCentral()
|
|
31
|
+
maven { url "https://maven.aliyun.com/repository/google" }
|
|
32
|
+
maven { url "https://maven.aliyun.com/repository/jcenter" }
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
24
36
|
apply plugin: 'com.android.library'
|
|
25
37
|
apply plugin: 'org.jetbrains.kotlin.android'
|
|
26
38
|
|
|
@@ -54,16 +66,11 @@ android {
|
|
|
54
66
|
kotlinOptions {
|
|
55
67
|
jvmTarget = "21"
|
|
56
68
|
}
|
|
57
|
-
sourceSets {
|
|
58
|
-
main {
|
|
59
|
-
jniLibs.srcDirs = ['libs']
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
69
|
}
|
|
63
70
|
|
|
64
71
|
dependencies {
|
|
65
|
-
//
|
|
66
|
-
implementation
|
|
72
|
+
// 百度定位SDK - 通过Gradle集成
|
|
73
|
+
implementation 'com.baidu.lbsyun:BaiduMapSDK_Location_All:9.6.4'
|
|
67
74
|
implementation project(':capacitor-android')
|
|
68
75
|
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
69
76
|
testImplementation "junit:junit:$junitVersion"
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
package com.hybrid.baidu.location
|
|
2
2
|
|
|
3
3
|
import android.content.Context
|
|
4
|
+
import android.os.Handler
|
|
5
|
+
import android.os.Looper
|
|
4
6
|
import android.util.Log
|
|
5
7
|
import androidx.annotation.NonNull
|
|
6
8
|
import com.baidu.location.BDAbstractLocationListener
|
|
@@ -8,8 +10,6 @@ import com.baidu.location.BDLocation
|
|
|
8
10
|
import com.baidu.location.LocationClient
|
|
9
11
|
import com.baidu.location.LocationClientOption
|
|
10
12
|
import com.getcapacitor.Logger
|
|
11
|
-
import java.util.concurrent.CountDownLatch
|
|
12
|
-
import java.util.concurrent.TimeUnit
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* 百度地图定位插件Android端实现类
|
|
@@ -33,11 +33,6 @@ class CPBaiduLocation(private val context: Context, private val config: CPBaiduL
|
|
|
33
33
|
*/
|
|
34
34
|
private var mLocation: BDLocation? = null
|
|
35
35
|
|
|
36
|
-
/**
|
|
37
|
-
* 用于等待定位结果的同步锁
|
|
38
|
-
*/
|
|
39
|
-
private var latch: CountDownLatch? = null
|
|
40
|
-
|
|
41
36
|
/**
|
|
42
37
|
* 坐标类型
|
|
43
38
|
*/
|
|
@@ -61,6 +56,24 @@ class CPBaiduLocation(private val context: Context, private val config: CPBaiduL
|
|
|
61
56
|
fun onValidationResult(success: Boolean, errorCode: Int, errorMessage: String)
|
|
62
57
|
}
|
|
63
58
|
|
|
59
|
+
/**
|
|
60
|
+
* 定位结果回调接口
|
|
61
|
+
*/
|
|
62
|
+
interface LocationCallback {
|
|
63
|
+
/**
|
|
64
|
+
* 定位成功回调
|
|
65
|
+
* @param location 定位结果
|
|
66
|
+
*/
|
|
67
|
+
fun onSuccess(location: BDLocation)
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 定位失败回调
|
|
71
|
+
* @param errorCode 错误码
|
|
72
|
+
* @param errorMessage 错误信息
|
|
73
|
+
*/
|
|
74
|
+
fun onError(errorCode: Int, errorMessage: String)
|
|
75
|
+
}
|
|
76
|
+
|
|
64
77
|
init {
|
|
65
78
|
Log.i(TAG, "getLocationClient: " + getLocationClient())
|
|
66
79
|
}
|
|
@@ -85,8 +98,6 @@ class CPBaiduLocation(private val context: Context, private val config: CPBaiduL
|
|
|
85
98
|
}
|
|
86
99
|
// 存储API Key
|
|
87
100
|
this.ak = realAk
|
|
88
|
-
// 自动设置同意隐私协议
|
|
89
|
-
LocationClient.setAgreePrivacy(true)
|
|
90
101
|
// 设置AK
|
|
91
102
|
LocationClient.setKey(realAk)
|
|
92
103
|
isAKValidated = true
|
|
@@ -129,8 +140,12 @@ class CPBaiduLocation(private val context: Context, private val config: CPBaiduL
|
|
|
129
140
|
if (mLocationClient == null) {
|
|
130
141
|
// 初始化定位客户端
|
|
131
142
|
try {
|
|
143
|
+
// 必须在实例化之前同意隐私
|
|
144
|
+
LocationClient.setAgreePrivacy(true);
|
|
132
145
|
mLocationClient = LocationClient(mContext)
|
|
133
|
-
mLocationClient?.let {
|
|
146
|
+
mLocationClient?.let {
|
|
147
|
+
it.locOption = getLocationClientOption()
|
|
148
|
+
}
|
|
134
149
|
} catch (e: Exception) {
|
|
135
150
|
Logger.error(TAG, "Error initializing LocationClient", e)
|
|
136
151
|
return null
|
|
@@ -143,68 +158,52 @@ class CPBaiduLocation(private val context: Context, private val config: CPBaiduL
|
|
|
143
158
|
* 获取当前位置信息
|
|
144
159
|
* @param needAddress 是否需要地址信息
|
|
145
160
|
* @param needLocationDescribe 是否需要位置描述
|
|
146
|
-
* @
|
|
161
|
+
* @param callback 定位结果回调
|
|
147
162
|
*/
|
|
148
|
-
fun getCurrentPosition(
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
163
|
+
fun getCurrentPosition(
|
|
164
|
+
needAddress: Boolean,
|
|
165
|
+
needLocationDescribe: Boolean,
|
|
166
|
+
callback: LocationCallback
|
|
167
|
+
) {
|
|
168
|
+
// 检查AK是否已验证
|
|
169
|
+
if (!isAKValidated) {
|
|
170
|
+
Log.e(TAG, "AK not validated")
|
|
171
|
+
callback.onError(-1, "AK not validated")
|
|
172
|
+
return
|
|
173
|
+
}
|
|
153
174
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
175
|
+
// 获取定位客户端(懒加载)
|
|
176
|
+
val locationClient = getLocationClient() ?: run {
|
|
177
|
+
Log.e(TAG, "Failed to get location client")
|
|
178
|
+
callback.onError(-2, "Failed to get location client")
|
|
179
|
+
return
|
|
180
|
+
}
|
|
160
181
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
182
|
+
try {
|
|
183
|
+
// 10秒超时检查
|
|
184
|
+
val handler = Handler(Looper.getMainLooper())
|
|
185
|
+
val timeoutRunnable = Runnable {
|
|
186
|
+
callback.onError(-3, "Location timeout")
|
|
165
187
|
}
|
|
188
|
+
handler.postDelayed(timeoutRunnable, 10000)
|
|
166
189
|
|
|
167
|
-
//
|
|
168
|
-
val
|
|
169
|
-
// 注册定位监听器
|
|
170
|
-
locationClient.registerLocationListener(myListener)
|
|
190
|
+
// 注册新监听器
|
|
191
|
+
val listener = LocationListener(handler, timeoutRunnable, callback, locationClient)
|
|
171
192
|
|
|
172
|
-
//
|
|
173
|
-
|
|
174
|
-
options.setIsNeedAddress(needAddress) // 设置是否需要地址信息
|
|
175
|
-
options.setIsNeedLocationDescribe(needLocationDescribe) // 设置是否需要位置描述
|
|
176
|
-
locationClient.locOption = options
|
|
193
|
+
// 注册监听器
|
|
194
|
+
locationClient.registerLocationListener(listener)
|
|
177
195
|
|
|
178
196
|
// 开始定位
|
|
179
|
-
locationClient.start()
|
|
180
|
-
|
|
181
|
-
// 等待定位结果,最多10秒
|
|
182
|
-
try {
|
|
183
|
-
latch?.await(10, TimeUnit.SECONDS)
|
|
184
|
-
} catch (e: InterruptedException) {
|
|
185
|
-
Logger.error(TAG, "Interrupted while waiting for location", e)
|
|
186
|
-
// 确保定位客户端被正确关闭
|
|
187
|
-
if (locationClient.isStarted) {
|
|
188
|
-
locationClient.stop()
|
|
189
|
-
locationClient.unRegisterLocationListener(myListener)
|
|
190
|
-
}
|
|
191
|
-
return null
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// 定位完成后停止定位客户端,释放资源
|
|
195
197
|
if (locationClient.isStarted) {
|
|
196
|
-
locationClient.
|
|
197
|
-
|
|
198
|
+
locationClient.requestLocation()
|
|
199
|
+
} else {
|
|
200
|
+
locationClient.start()
|
|
198
201
|
}
|
|
202
|
+
|
|
199
203
|
} catch (e: Exception) {
|
|
200
204
|
Logger.error(TAG, "Error getting location", e)
|
|
201
|
-
|
|
202
|
-
if (mLocationClient?.isStarted == true) {
|
|
203
|
-
mLocationClient?.stop()
|
|
204
|
-
}
|
|
205
|
+
callback.onError(-5, "Error: ${e.message}")
|
|
205
206
|
}
|
|
206
|
-
|
|
207
|
-
return mLocation
|
|
208
207
|
}
|
|
209
208
|
|
|
210
209
|
@NonNull
|
|
@@ -216,31 +215,54 @@ class CPBaiduLocation(private val context: Context, private val config: CPBaiduL
|
|
|
216
215
|
option.scanSpan = 0 // 单次定位
|
|
217
216
|
option.isOpenGnss = true // 启用GPS
|
|
218
217
|
option.isLocationNotify = true // 当位置变化时通知
|
|
219
|
-
option.setIgnoreKillProcess(
|
|
220
|
-
option.SetIgnoreCacheException(
|
|
218
|
+
option.setIgnoreKillProcess(true) // 定位服务不被杀死
|
|
219
|
+
option.SetIgnoreCacheException(true) // 忽略缓存异常
|
|
221
220
|
option.wifiCacheTimeOut = 5 * 60 * 1000 // WiFi缓存超时时间
|
|
222
221
|
option.enableSimulateGps = false // 不启用模拟GPS
|
|
222
|
+
option.setIsNeedAddress(true) // 设置是否需要地址信息
|
|
223
|
+
option.setIsNeedLocationDescribe(true) // 设置是否需要位置描述
|
|
223
224
|
option.isNeedNewVersionRgc = true // 使用新版本的逆地理编码
|
|
224
225
|
return option
|
|
225
226
|
}
|
|
226
227
|
|
|
227
228
|
/**
|
|
228
|
-
*
|
|
229
|
+
* 定位监听器
|
|
229
230
|
*/
|
|
230
|
-
private inner class
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
231
|
+
private inner class LocationListener(
|
|
232
|
+
private val handler: Handler,
|
|
233
|
+
private val timeoutRunnable: Runnable,
|
|
234
|
+
private val callback: LocationCallback,
|
|
235
|
+
private val locationClient: LocationClient
|
|
236
|
+
) : BDAbstractLocationListener() {
|
|
236
237
|
override fun onReceiveLocation(location: BDLocation?) {
|
|
238
|
+
// 取消超时检查
|
|
239
|
+
handler.removeCallbacks(timeoutRunnable)
|
|
240
|
+
|
|
237
241
|
if (location != null) {
|
|
238
242
|
mLocation = location
|
|
239
243
|
Log.i(TAG, "坐标类型 = " + location.coorType)
|
|
240
244
|
Log.i(TAG, "经纬度(lng, lat) = " + location.longitude.toString() + "," + location.latitude)
|
|
245
|
+
|
|
246
|
+
// 检查定位类型
|
|
247
|
+
if (location.locType == BDLocation.TypeGpsLocation ||
|
|
248
|
+
location.locType == BDLocation.TypeNetWorkLocation ||
|
|
249
|
+
location.locType == BDLocation.TypeOffLineLocation) {
|
|
250
|
+
// 定位成功
|
|
251
|
+
callback.onSuccess(location)
|
|
252
|
+
} else {
|
|
253
|
+
// 定位失败
|
|
254
|
+
callback.onError(location.locType, location.locTypeDescription ?: "定位失败")
|
|
255
|
+
}
|
|
256
|
+
} else {
|
|
257
|
+
// 定位失败
|
|
258
|
+
callback.onError(-4, "定位结果为空")
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// 停止定位
|
|
262
|
+
if (locationClient.isStarted) {
|
|
263
|
+
locationClient.stop()
|
|
241
264
|
}
|
|
242
|
-
|
|
243
|
-
latch?.countDown()
|
|
265
|
+
locationClient.unRegisterLocationListener(this)
|
|
244
266
|
}
|
|
245
267
|
}
|
|
246
268
|
}
|