capacitor-baidu-location 0.0.6 → 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.
Files changed (38) hide show
  1. package/README.md +73 -52
  2. package/android/build.gradle +14 -7
  3. package/android/src/main/kotlin/com/hybrid/baidu/location/CPBaiduLocation.kt +92 -70
  4. package/android/src/main/kotlin/com/hybrid/baidu/location/CPBaiduLocationPlugin.kt +49 -42
  5. package/ios/Sources/CPBaiduLocationPlugin/CPBaiduLocation.swift +3 -3
  6. package/package.json +1 -4
  7. package/Package.swift +0 -44
  8. package/android/libs/BaiduLBS_Android.jar +0 -0
  9. package/android/libs/arm64-v8a/liblocSDK8b.so +0 -0
  10. package/android/libs/x86_64/liblocSDK8b.so +0 -0
  11. package/ios/Frameworks/BMKLocationKit.xcframework/Info.plist +0 -43
  12. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/BMKLocationKit +0 -0
  13. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/BMKLocationKit.xcprivacy +0 -64
  14. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKGeoFenceManager.h +0 -168
  15. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKGeoFenceRegion.h +0 -114
  16. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocation.h +0 -112
  17. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationAuth.h +0 -71
  18. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationComponent.h +0 -16
  19. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationKitVersion.h +0 -30
  20. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationManager.h +0 -303
  21. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationPoi.h +0 -42
  22. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationPoiRegion.h +0 -35
  23. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Headers/BMKLocationReGeocode.h +0 -69
  24. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Info.plist +0 -36
  25. package/ios/Frameworks/BMKLocationKit.xcframework/ios-arm64/BMKLocationKit.framework/Modules/module.modulemap +0 -6
  26. package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/BMKLocationKit +0 -0
  27. package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKGeoFenceManager.h +0 -168
  28. package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKGeoFenceRegion.h +0 -114
  29. package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocation.h +0 -112
  30. package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationAuth.h +0 -71
  31. package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationComponent.h +0 -16
  32. package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationKitVersion.h +0 -30
  33. package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationManager.h +0 -303
  34. package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationPoi.h +0 -42
  35. package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationPoiRegion.h +0 -35
  36. package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Headers/BMKLocationReGeocode.h +0 -69
  37. package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Info.plist +0 -36
  38. package/ios/Frameworks/BMKLocationKit.xcframework/ios-x86_64-simulator/BMKLocationKit.framework/Modules/module.modulemap +0 -6
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Capacitor Baidu Location Plugin
2
2
 
3
- 一个支持 Capacitor 7 的百度地图定位插件,提供全平台百度定位的功能。
3
+ 一个支持 Capacitor 7 的百度地图定位插件,iOS 使用 cocoapods 集成,Android 使用 gradle 集成。
4
4
 
5
5
  ## 功能特性
6
6
 
@@ -20,6 +20,7 @@ npx cap sync
20
20
  ## 开发依赖
21
21
 
22
22
  ### Android 开发依赖
23
+
23
24
  - Java版本 : 21
24
25
  - Kotlin版本 : 1.9.25
25
26
  - Gradle版本 : 8.7.2
@@ -27,11 +28,13 @@ npx cap sync
27
28
  - 第三方库 : OkHttp3 3.14.9
28
29
 
29
30
  ### iOS 开发依赖
31
+
30
32
  - Xcode 16 +
31
- - Swift 5.9 +
33
+ - Swift 5.1 +
32
34
  - iOS 14 +
33
35
 
34
36
  ### Capacitor
37
+
35
38
  - TypeScript 5.1+
36
39
  - Capacitor 7.+
37
40
  - NodeJS 20+
@@ -43,6 +46,7 @@ npx cap sync
43
46
  插件支持读取配置,这是推荐的配置方式,便于集中管理和构建时配置。web端则直接通过api设置,因为无法在web端读取capacitor配置的内容。
44
47
 
45
48
  **capacitor.config.ts**
49
+
46
50
  ```typescript
47
51
  import { CapacitorConfig } from '@capacitor/cli';
48
52
 
@@ -66,6 +70,7 @@ export default config;
66
70
  - **方式二:配置文件设置**:在 `capacitor.config.ts` 中配置 `androidAK` ,之后一样要调用 `setAK` 方法,只是不需要传参.
67
71
 
68
72
  **注意:AndroidManifest.xml 需要配置占位值**:
73
+
69
74
  ```xml
70
75
  <application>
71
76
  <meta-data
@@ -74,8 +79,9 @@ export default config;
74
79
  </application>
75
80
  ```
76
81
 
77
- 2. **权限配置**
78
- 使用定位SDK,需在AndroidManifest.xml文件中Application标签中声明service组件,每个App拥有自己单独的定位service,代码如下:
82
+ 1. **权限配置**
83
+ 使用定位SDK,需在AndroidManifest.xml文件中Application标签中声明service组件,每个App拥有自己单独的定位service,代码如下:
84
+
79
85
  ```xml
80
86
  <application>
81
87
  <service android:name="com.baidu.location.f"
@@ -85,6 +91,7 @@ export default config;
85
91
  ```
86
92
 
87
93
  除添加service组件外,使用定位SDK还需添加如下权限:
94
+
88
95
  ```xml
89
96
  <!-- 这个权限用于进行网络定位-->
90
97
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
@@ -108,9 +115,9 @@ export default config;
108
115
  - 百度地图 API Key 可以通过2种方式配置(优先级从高到低):
109
116
  - **方式一:动态设置**:在应用代码中调用 `setAK` 方法设置
110
117
  - **方式二:配置文件设置**:在 `capacitor.config.ts` 中配置 `iOSAK` ,之后一样要调用 `setAK` 方法,只是不需要传参.
111
-
112
118
  2. **权限配置**
113
- 在 `Info.plist` 文件中添加以下权限描述:
119
+ 在 `Info.plist` 文件中添加以下权限描述:
120
+
114
121
  ```xml
115
122
  <key>NSLocationWhenInUseUsageDescription</key>
116
123
  <string>需要获取您的位置信息</string>
@@ -132,17 +139,20 @@ export default config;
132
139
  设置百度地图 API Key。可以覆盖配置文件中的设置,优先级最高。
133
140
 
134
141
  **参数**
135
- | 参数 | 类型 | 描述 |
136
- |------|------|------|
142
+
143
+ | 参数 | 类型 | 描述 |
144
+ | ---- | -------- | ---------------- |
137
145
  | `ak` | `string` | 百度定位的各平台 API Key |
138
146
 
139
147
  **返回值**
140
- | 字段 | 类型 | 描述 |
141
- |------|------|------|
142
- | `success` | `boolean` | 设置是否成功 |
143
- | `errorMessage` | `string` | 错误信息(如果失败) |
148
+
149
+ | 字段 | 类型 | 描述 |
150
+ | -------------- | --------- | ---------- |
151
+ | `success` | `boolean` | 设置是否成功 |
152
+ | `errorMessage` | `string` | 错误信息(如果失败) |
144
153
 
145
154
  **说明**
155
+
146
156
  - 调用此方法会覆盖配置文件中的 AK 设置
147
157
  - 可以在应用运行时动态切换 AK
148
158
 
@@ -151,18 +161,20 @@ export default config;
151
161
  检查 API Key 验证状态。
152
162
 
153
163
  **返回值**
154
- | 字段 | 类型 | 描述 |
155
- |------|------|------|
156
- | `valid` | `boolean` | AK 是否有效 |
157
- | `errorMessage` | `string` | 错误信息(如果无效) |
164
+
165
+ | 字段 | 类型 | 描述 |
166
+ | -------------- | --------- | ---------- |
167
+ | `valid` | `boolean` | AK 是否有效 |
168
+ | `errorMessage` | `string` | 错误信息(如果无效) |
158
169
 
159
170
  ### `checkPermission()`
160
171
 
161
172
  检查定位权限状态。
162
173
 
163
174
  **返回值**
164
- | 字段 | 类型 | 描述 |
165
- |------|------|------|
175
+
176
+ | 字段 | 类型 | 描述 |
177
+ | --------- | --------- | ------- |
166
178
  | `granted` | `boolean` | 权限是否已授予 |
167
179
 
168
180
  ### `requestPermission()`
@@ -170,8 +182,9 @@ export default config;
170
182
  请求定位权限。
171
183
 
172
184
  **返回值**
173
- | 字段 | 类型 | 描述 |
174
- |------|------|------|
185
+
186
+ | 字段 | 类型 | 描述 |
187
+ | --------- | --------- | ------- |
175
188
  | `granted` | `boolean` | 权限是否已授予 |
176
189
 
177
190
  ### `setCoordinateType(options)`
@@ -179,13 +192,15 @@ export default config;
179
192
  设置坐标类型。
180
193
 
181
194
  **参数**
182
- | 参数 | 类型 | 描述 |
183
- |------|------|------|
195
+
196
+ | 参数 | 类型 | 描述 |
197
+ | ------ | ---------------- | ---- |
184
198
  | `type` | `CoordinateType` | 坐标类型 |
185
199
 
186
200
  **返回值**
187
- | 字段 | 类型 | 描述 |
188
- |------|------|------|
201
+
202
+ | 字段 | 类型 | 描述 |
203
+ | --------- | --------- | ------ |
189
204
  | `success` | `boolean` | 设置是否成功 |
190
205
 
191
206
  ### `getCoordinateType()`
@@ -193,8 +208,9 @@ export default config;
193
208
  获取当前坐标类型。
194
209
 
195
210
  **返回值**
196
- | 字段 | 类型 | 描述 |
197
- |------|------|------|
211
+
212
+ | 字段 | 类型 | 描述 |
213
+ | ------ | ---------------- | ------ |
198
214
  | `type` | `CoordinateType` | 当前坐标类型 |
199
215
 
200
216
  ### `getCurrentPosition(options?)`
@@ -202,37 +218,39 @@ export default config;
202
218
  获取当前位置信息。
203
219
 
204
220
  **参数**
205
- | 参数 | 类型 | 描述 | 默认值 |
206
- |------|------|------|--------|
207
- | `needAddress` | `boolean` | 是否获取地址信息 | `false` |
221
+
222
+ | 参数 | 类型 | 描述 | 默认值 |
223
+ | ---------------------- | --------- | -------- | ------- |
224
+ | `needAddress` | `boolean` | 是否获取地址信息 | `false` |
208
225
  | `needLocationDescribe` | `boolean` | 是否获取位置描述 | `false` |
209
226
 
210
227
  **返回值**
211
- | 字段 | 类型 | 描述 |
212
- |------|------|------|
213
- | `latitude` | `number` | 纬度 |
214
- | `longitude` | `number` | 经度 |
215
- | `accuracy` | `number` | 定位精度(米) |
216
- | `address` | `string` | 完整地址 |
217
- | `country` | `string` | 国家 |
218
- | `province` | `string` | 省份 |
219
- | `city` | `string` | 城市 |
220
- | `district` | `string` | 区县 |
221
- | `street` | `string` | 街道 |
222
- | `adcode` | `string` | 行政区划代码 |
223
- | `town` | `string` | 乡镇 |
224
- | `locationDescribe` | `string` | 位置描述 |
225
- | `errorCode` | `number` | 错误码 |
226
- | `errorMessage` | `string` | 错误信息 |
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` | 错误信息 |
227
245
 
228
246
  ### `CoordinateType` 枚举
229
247
 
230
- | 枚举值 | 描述 |
231
- |--------|------|
232
- | `BD09LL` | 百度经纬度坐标 |
248
+ | 枚举值 | 描述 |
249
+ | -------- | --------- |
250
+ | `BD09LL` | 百度经纬度坐标 |
233
251
  | `BD09MC` | 百度墨卡托米制坐标 |
234
- | `GCJ02` | 国测局坐标 |
235
- | `WGS84` | WGS84坐标 |
252
+ | `GCJ02` | 国测局坐标 |
253
+ | `WGS84` | WGS84坐标 |
236
254
 
237
255
  ## 使用示例
238
256
 
@@ -330,11 +348,9 @@ const getCurrentCoordinateType = async () => {
330
348
  - 请确保在 [百度地图开放平台](https://lbs.baidu.com/) 申请了正确的 API Key
331
349
  - Android、iOS、web 端需要分别申请不同的 API Key
332
350
  - 确保 API Key 已启用定位服务
333
-
334
351
  2. **权限管理**:
335
352
  - 定位功能需要用户授权
336
353
  - 请在适当的时机检查和请求权限
337
-
338
354
  3. **网络连接**:
339
355
  - 获取地址信息和位置描述需要网络连接
340
356
  - 基础定位功能可以在离线状态下使用
@@ -352,15 +368,19 @@ const getCurrentCoordinateType = async () => {
352
368
  ## 故障排除
353
369
 
354
370
  ### API Key 设置失败
371
+
355
372
  **问题**:调用 `setAK` 方法返回失败
356
373
  **解决方案**:
374
+
357
375
  - 检查 API Key 是否正确
358
376
  - 检查网络连接是否正常
359
377
  - 确保 API Key 已在百度地图开放平台启用
360
378
 
361
379
  ### 权限请求失败
380
+
362
381
  **问题**:用户拒绝了定位权限
363
382
  **解决方案**:
383
+
364
384
  - 提示用户定位权限的重要性
365
385
  - 引导用户在系统设置中手动开启权限
366
386
  - 优雅处理无权限的情况
@@ -371,3 +391,4 @@ const getCurrentCoordinateType = async () => {
371
391
  - [百度地图 Android 定位 SDK 文档](https://lbs.baidu.com/faq/api?title=android-locsdk/guide/get-location/latlng)
372
392
  - [百度地图 iOS 定位 SDK 文档](https://lbs.baidu.com/faq/api?title=ios-locsdk/guide/get-location/once)
373
393
  - [Capacitor 插件开发指南](https://capacitorjs.com/docs/plugins)
394
+
@@ -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
- // 本地AAR依赖(必须用implementation,确保传递到宿主)
66
- implementation fileTree(dir: 'libs', include: ['*.jar'])
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 { it.locOption = getLocationClientOption() }
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
- * @return 定位结果,包含经纬度、精度、地址信息等
161
+ * @param callback 定位结果回调
147
162
  */
148
- fun getCurrentPosition(needAddress: Boolean, needLocationDescribe: Boolean): BDLocation? {
149
- // 重置定位结果
150
- mLocation = null
151
- // 创建用于等待定位结果的同步锁
152
- latch = CountDownLatch(1)
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
- try {
155
- // 检查AK是否已验证
156
- if (!isAKValidated) {
157
- Log.e(TAG, "AK not validated")
158
- return null
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
- val locationClient = getLocationClient() ?: run {
163
- Log.e(TAG, "Failed to get location client")
164
- return null
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 myListener = MyLocationListener()
169
- // 注册定位监听器
170
- locationClient.registerLocationListener(myListener)
190
+ // 注册新监听器
191
+ val listener = LocationListener(handler, timeoutRunnable, callback, locationClient)
171
192
 
172
- // 配置定位选项
173
- val options = locationClient.locOption
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.stop()
197
- locationClient.unRegisterLocationListener(myListener)
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(false) // 定位服务不被杀死
220
- option.SetIgnoreCacheException(false) // 不忽略缓存异常
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 MyLocationListener : BDAbstractLocationListener() {
231
-
232
- /**
233
- * 接收定位结果
234
- * @param location 定位结果
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
  }
@@ -64,54 +64,61 @@ class CPBaiduLocationPlugin : Plugin() {
64
64
  val needAddress = call.getBoolean("needAddress", false) ?: false
65
65
  val needLocationDescribe = call.getBoolean("needLocationDescribe", false) ?: false
66
66
 
67
- // 调用实现类获取定位结果
68
- val location = implementation.getCurrentPosition(needAddress, needLocationDescribe)
67
+ // 调用实现类获取定位结果(异步)
68
+ implementation.getCurrentPosition(needAddress, needLocationDescribe, object : CPBaiduLocation.LocationCallback {
69
+ override fun onSuccess(location: BDLocation) {
70
+ // 构建返回结果
71
+ val ret = JSObject()
72
+
73
+ // 添加经纬度和精度
74
+ ret.put("latitude", location.latitude)
75
+ ret.put("longitude", location.longitude)
76
+ ret.put("accuracy", location.radius)
77
+
78
+ // 地址信息(如果需要且可用)
79
+ if (needAddress && location.addrStr != null) {
80
+ ret.put("address", location.addrStr)
81
+ ret.put("country", location.country)
82
+ ret.put("province", location.province)
83
+ ret.put("city", location.city)
84
+ ret.put("district", location.district)
85
+ ret.put("street", location.street)
86
+ ret.put("adcode", location.adCode)
87
+ ret.put("town", location.town)
88
+ }
69
89
 
70
- // 构建返回结果
71
- val ret = JSObject()
90
+ // 位置描述(如果需要且可用)
91
+ if (needLocationDescribe && location.locationDescribe != null) {
92
+ ret.put("locationDescribe", location.locationDescribe)
93
+ }
72
94
 
73
- if (location != null) {
74
- // 添加经纬度和精度
75
- ret.put("latitude", location.latitude)
76
- ret.put("longitude", location.longitude)
77
- ret.put("accuracy", location.radius)
78
-
79
- // 地址信息(如果需要且可用)
80
- if (needAddress && location.addrStr != null) {
81
- ret.put("address", location.addrStr)
82
- ret.put("country", location.country)
83
- ret.put("province", location.province)
84
- ret.put("city", location.city)
85
- ret.put("district", location.district)
86
- ret.put("street", location.street)
87
- ret.put("adcode", location.adCode)
88
- ret.put("town", location.town)
89
- }
95
+ // 错误信息(如果定位类型不是GPS、网络或离线定位)
96
+ if (
97
+ location.locType != BDLocation.TypeGpsLocation &&
98
+ location.locType != BDLocation.TypeNetWorkLocation &&
99
+ location.locType != BDLocation.TypeOffLineLocation
100
+ ) {
101
+ ret.put("errorCode", location.locType)
102
+ ret.put("errorMessage", location.locTypeDescription)
103
+ }
90
104
 
91
- // 位置描述(如果需要且可用)
92
- if (needLocationDescribe && location.locationDescribe != null) {
93
- ret.put("locationDescribe", location.locationDescribe)
105
+ // 返回结果
106
+ call.resolve(ret)
94
107
  }
95
108
 
96
- // 错误信息(如果定位类型不是GPS、网络或离线定位)
97
- if (
98
- location.locType != BDLocation.TypeGpsLocation &&
99
- location.locType != BDLocation.TypeNetWorkLocation &&
100
- location.locType != BDLocation.TypeOffLineLocation
101
- ) {
102
- ret.put("errorCode", location.locType)
103
- ret.put("errorMessage", location.locTypeDescription)
109
+ override fun onError(errorCode: Int, errorMessage: String) {
110
+ // 构建错误结果
111
+ val ret = JSObject()
112
+ ret.put("latitude", 0)
113
+ ret.put("longitude", 0)
114
+ ret.put("accuracy", 0)
115
+ ret.put("errorCode", errorCode)
116
+ ret.put("errorMessage", errorMessage)
117
+
118
+ // 返回错误结果
119
+ call.resolve(ret)
104
120
  }
105
- } else {
106
- // 定位失败
107
- ret.put("latitude", 0)
108
- ret.put("longitude", 0)
109
- ret.put("errorCode", -1)
110
- ret.put("errorMessage", "Failed to get location")
111
- }
112
-
113
- // 返回结果
114
- call.resolve(ret)
121
+ })
115
122
  }
116
123
 
117
124
  /**