capacitor-baidu-location 0.0.1

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.
@@ -0,0 +1,23 @@
1
+ require 'json'
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = 'CapacitorBaiduLocation'
7
+ s.version = package['version']
8
+ s.summary = package['description']
9
+ s.license = package['license']
10
+ s.homepage = package['repository']['url']
11
+ s.author = package['author']
12
+ s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
13
+ s.source_files = 'ios/Sources/**/*.{swift,h,m,c,cc,mm,cpp}'
14
+ s.ios.deployment_target = '14.0'
15
+ s.dependency 'Capacitor'
16
+ s.swift_version = '5.1'
17
+
18
+ # 百度地图定位SDK依赖
19
+ s.vendored_frameworks = 'ios/Frameworks/BMKLocationKit.xcframework'
20
+ s.frameworks = 'CoreLocation', 'UIKit', 'Foundation', 'CoreTelephony', 'SystemConfiguration'
21
+ s.libraries = 'z', 'stdc++', 'sqlite3'
22
+ s.weak_frameworks = 'CoreMotion'
23
+ end
package/Package.swift ADDED
@@ -0,0 +1,44 @@
1
+ // swift-tools-version: 5.9
2
+ import PackageDescription
3
+
4
+ let package = Package(
5
+ name: "CapacitorBaiduLocation",
6
+ platforms: [.iOS(.v14)],
7
+ products: [
8
+ .library(
9
+ name: "CapacitorBaiduLocation",
10
+ targets: ["CPBaiduLocationPlugin"])
11
+ ],
12
+ dependencies: [
13
+ .package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "7.0.0")
14
+ ],
15
+ targets: [
16
+ .binaryTarget(
17
+ name: "BMKLocationKit",
18
+ path: "ios/Frameworks/BMKLocationKit.xcframework"
19
+ ),
20
+ .target(
21
+ name: "CPBaiduLocationPlugin",
22
+ dependencies: [
23
+ .product(name: "Capacitor", package: "capacitor-swift-pm"),
24
+ .product(name: "Cordova", package: "capacitor-swift-pm"),
25
+ .target(name: "BMKLocationKit")
26
+ ],
27
+ path: "ios/Sources/CPBaiduLocationPlugin",
28
+ linkerSettings: [
29
+ .linkedFramework("CoreTelephony"),
30
+ .linkedFramework("CoreLocation"),
31
+ .linkedFramework("SystemConfiguration"),
32
+ .linkedFramework("Security"),
33
+ .linkedFramework("CFNetwork"),
34
+ .linkedFramework("CoreMotion"),
35
+ .linkedFramework("AdSupport"),
36
+ .linkedLibrary("sqlite3"),
37
+ .linkedLibrary("c++")
38
+ ]),
39
+ .testTarget(
40
+ name: "CPBaiduLocationPluginTests",
41
+ dependencies: ["CPBaiduLocationPlugin"],
42
+ path: "ios/Tests/CPBaiduLocationPluginTests")
43
+ ]
44
+ )
package/README.md ADDED
@@ -0,0 +1,376 @@
1
+ # Capacitor Baidu Location Plugin
2
+
3
+ 一个支持 Capacitor 7 的百度地图定位插件,提供全面的定位功能,包括单次定位、权限管理、API Key 设置等。
4
+
5
+ ## 功能特性
6
+
7
+ - ✅ 支持单次定位
8
+ - ✅ 支持配置是否获取地址信息
9
+ - ✅ 支持配置是否获取位置描述
10
+ - ✅ 支持动态设置 API Key
11
+ - ✅ 支持定位权限检查和请求
12
+ - ✅ 支持坐标类型设置和获取
13
+ - ✅ 支持 AK 验证状态检查
14
+ - ✅ 基于 Capacitor 7
15
+
16
+ ## 安装
17
+
18
+ ```bash
19
+ npm install capacitor-baidu-location
20
+ npx cap sync
21
+ ```
22
+
23
+ ## 开发依赖
24
+
25
+ ### Android 开发依赖
26
+ - Java版本 : 21
27
+ - Kotlin版本 : 1.9.25
28
+ - Gradle版本 : 8.7.2
29
+ - gradle wrapper : 8.11.1
30
+ - 第三方库 : OkHttp3 3.14.9
31
+
32
+ ### iOS 开发依赖
33
+ - Xcode 16 +
34
+ - Swift 5.9 +
35
+ - iOS 14 +
36
+
37
+ ### Capacitor
38
+ - TypeScript 5.1+
39
+ - Capacitor 7.+
40
+ - NodeJS 20+
41
+
42
+ ## 配置
43
+
44
+ ### 配置文件设置(推荐)
45
+
46
+ 插件支持读取配置,这是推荐的配置方式,便于集中管理和构建时配置。
47
+
48
+ **capacitor.config.ts**
49
+ ```typescript
50
+ import { CapacitorConfig } from '@capacitor/cli';
51
+
52
+ const config: CapacitorConfig = {
53
+ appId: 'com.example.app',
54
+ appName: 'Example App',
55
+ webDir: 'dist',
56
+ plugins: {
57
+ CPBaiduLocation: {
58
+ iOSAK: 'your-ios-api-key',
59
+ androidAK: 'your-android-api-key',
60
+ }
61
+ }
62
+ };
63
+
64
+ export default config;
65
+ ```
66
+
67
+ ### Android 配置
68
+
69
+ 1. **API Key 配置**
70
+ - 百度地图 API Key 可以通过三种方式配置(优先级从高到低):
71
+ - **方式一:动态设置**:在应用代码中调用 `setAK` 方法设置
72
+ - **方式二:配置文件设置**:在 `capacitor.config.ts` 中配置
73
+
74
+ **注意:AndroidManifest.xml 需要配置占位值**:
75
+ ```xml
76
+ <application>
77
+ <meta-data
78
+ android:name="com.baidu.lbsapi.API_KEY"
79
+ android:value="" />
80
+ </application>
81
+ ```
82
+
83
+ 2. **权限配置**
84
+ 使用定位SDK,需在AndroidManifest.xml文件中Application标签中声明service组件,每个App拥有自己单独的定位service,代码如下:
85
+ ```xml
86
+ <application>
87
+ <service android:name="com.baidu.location.f"
88
+ android:enabled="true"
89
+ android:process=":remote" />
90
+ </application>
91
+ ```
92
+
93
+ 除添加service组件外,使用定位SDK还需添加如下权限:
94
+ ```xml
95
+ <!-- 这个权限用于进行网络定位-->
96
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
97
+ <!-- 这个权限用于访问系统接口提供的卫星定位信息-->
98
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
99
+ <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
100
+ <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
101
+ <!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
102
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
103
+ <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
104
+ <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
105
+ <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据-->
106
+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
107
+ <!-- 访问网络,网络定位需要上网-->
108
+ <uses-permission android:name="android.permission.INTERNET"></uses-permission>
109
+ ```
110
+
111
+ ### iOS 配置
112
+
113
+ 1. **API Key 配置**
114
+ - 百度地图 API Key 可以通过三种方式配置(优先级从高到低):
115
+ - **方式一:动态设置**:在应用代码中调用 `setAK` 方法设置
116
+ - **方式二:配置文件设置**:在 `capacitor.config.ts` 中配置
117
+
118
+ 2. **权限配置**
119
+ 在 `Info.plist` 文件中添加以下权限描述:
120
+ ```xml
121
+ <key>NSLocationWhenInUseUsageDescription</key>
122
+ <string>需要获取您的位置信息</string>
123
+ <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
124
+ <string>需要获取您的位置信息</string>
125
+ <key>NSLocationAlwaysUsageDescription</key>
126
+ <string>需要获取您的位置信息</string>
127
+ ```
128
+
129
+ ### Web 配置
130
+
131
+ Web 平台使用浏览器内置的地理定位 API,无需额外配置 API Key。
132
+
133
+ ## API
134
+
135
+ ### `setAK(options)`
136
+
137
+ 设置百度地图 API Key。可以覆盖配置文件中的设置,优先级最高。
138
+
139
+ **参数**
140
+ | 参数 | 类型 | 描述 |
141
+ |------|------|------|
142
+ | `ak` | `string` | 百度地图 API Key |
143
+
144
+ **返回值**
145
+ | 字段 | 类型 | 描述 |
146
+ |------|------|------|
147
+ | `success` | `boolean` | 设置是否成功 |
148
+ | `errorMessage` | `string` | 错误信息(如果失败) |
149
+
150
+ **说明**
151
+ - 调用此方法会覆盖配置文件中的 AK 设置
152
+ - 可以在应用运行时动态切换 AK
153
+
154
+ ### `checkAKValidation()`
155
+
156
+ 检查 API Key 验证状态。
157
+
158
+ **返回值**
159
+ | 字段 | 类型 | 描述 |
160
+ |------|------|------|
161
+ | `valid` | `boolean` | AK 是否有效 |
162
+ | `errorMessage` | `string` | 错误信息(如果无效) |
163
+
164
+ ### `checkPermission()`
165
+
166
+ 检查定位权限状态。
167
+
168
+ **返回值**
169
+ | 字段 | 类型 | 描述 |
170
+ |------|------|------|
171
+ | `granted` | `boolean` | 权限是否已授予 |
172
+
173
+ ### `requestPermission()`
174
+
175
+ 请求定位权限。
176
+
177
+ **返回值**
178
+ | 字段 | 类型 | 描述 |
179
+ |------|------|------|
180
+ | `granted` | `boolean` | 权限是否已授予 |
181
+
182
+ ### `setCoordinateType(options)`
183
+
184
+ 设置坐标类型。
185
+
186
+ **参数**
187
+ | 参数 | 类型 | 描述 |
188
+ |------|------|------|
189
+ | `type` | `CoordinateType` | 坐标类型 |
190
+
191
+ **返回值**
192
+ | 字段 | 类型 | 描述 |
193
+ |------|------|------|
194
+ | `success` | `boolean` | 设置是否成功 |
195
+
196
+ ### `getCoordinateType()`
197
+
198
+ 获取当前坐标类型。
199
+
200
+ **返回值**
201
+ | 字段 | 类型 | 描述 |
202
+ |------|------|------|
203
+ | `type` | `CoordinateType` | 当前坐标类型 |
204
+
205
+ ### `getCurrentPosition(options?)`
206
+
207
+ 获取当前位置信息。
208
+
209
+ **参数**
210
+ | 参数 | 类型 | 描述 | 默认值 |
211
+ |------|------|------|--------|
212
+ | `needAddress` | `boolean` | 是否获取地址信息 | `false` |
213
+ | `needLocationDescribe` | `boolean` | 是否获取位置描述 | `false` |
214
+
215
+ **返回值**
216
+ | 字段 | 类型 | 描述 |
217
+ |------|------|------|
218
+ | `latitude` | `number` | 纬度 |
219
+ | `longitude` | `number` | 经度 |
220
+ | `accuracy` | `number` | 定位精度(米) |
221
+ | `address` | `string` | 完整地址 |
222
+ | `country` | `string` | 国家 |
223
+ | `province` | `string` | 省份 |
224
+ | `city` | `string` | 城市 |
225
+ | `district` | `string` | 区县 |
226
+ | `street` | `string` | 街道 |
227
+ | `adcode` | `string` | 行政区划代码 |
228
+ | `town` | `string` | 乡镇 |
229
+ | `locationDescribe` | `string` | 位置描述 |
230
+ | `errorCode` | `number` | 错误码 |
231
+ | `errorMessage` | `string` | 错误信息 |
232
+
233
+ ### `CoordinateType` 枚举
234
+
235
+ | 枚举值 | 描述 |
236
+ |--------|------|
237
+ | `BD09LL` | 百度经纬度坐标 |
238
+ | `BD09MC` | 百度墨卡托米制坐标 |
239
+ | `GCJ02` | 国测局坐标 |
240
+ | `WGS84` | WGS84坐标 |
241
+
242
+ ## 使用示例
243
+
244
+ ### 基本使用流程
245
+
246
+ ```typescript
247
+ import { CPBaiduLocation, CoordinateType } from 'capacitor-baidu-location';
248
+
249
+ // 1. 初始化 - 设置 API Key
250
+ const initLocation = async () => {
251
+ const result = await CPBaiduLocation.setAK({
252
+ ak: '你的百度地图 API Key'
253
+ });
254
+
255
+ if (result.success) {
256
+ console.log('API Key 设置成功');
257
+ } else {
258
+ console.error('API Key 设置失败:', result.errorMessage);
259
+ }
260
+ };
261
+
262
+ // 2. 检查并请求权限
263
+ const checkAndRequestPermission = async () => {
264
+ const permissionStatus = await CPBaiduLocation.checkPermission();
265
+
266
+ if (!permissionStatus.granted) {
267
+ const requestResult = await CPBaiduLocation.requestPermission();
268
+ if (!requestResult.granted) {
269
+ console.error('定位权限被拒绝');
270
+ return false;
271
+ }
272
+ }
273
+
274
+ return true;
275
+ };
276
+
277
+ // 3. (可选)设置坐标类型
278
+ const setCoordinateType = async () => {
279
+ await CPBaiduLocation.setCoordinateType({
280
+ type: CoordinateType.BD09LL
281
+ });
282
+ };
283
+
284
+ // 4. 获取当前位置
285
+ const getLocation = async () => {
286
+ const hasPermission = await checkAndRequestPermission();
287
+
288
+ if (!hasPermission) {
289
+ return;
290
+ }
291
+
292
+ try {
293
+ const result = await CPBaiduLocation.getCurrentPosition({
294
+ needAddress: true,
295
+ needLocationDescribe: true
296
+ });
297
+
298
+ if (result.errorCode) {
299
+ console.error('定位失败:', result.errorCode, result.errorMessage);
300
+ } else {
301
+ console.log('定位成功:', result);
302
+ }
303
+ } catch (error) {
304
+ console.error('定位异常:', error);
305
+ }
306
+ };
307
+
308
+ // 组合使用
309
+ const fullExample = async () => {
310
+ await initLocation();
311
+ await setCoordinateType();
312
+ await getLocation();
313
+ };
314
+ ```
315
+
316
+ ### 其他使用示例
317
+
318
+ ```typescript
319
+ // 检查 AK 验证状态
320
+ const checkAK = async () => {
321
+ const result = await CPBaiduLocation.checkAKValidation();
322
+ console.log('AK 验证状态:', result.valid ? '有效' : '无效');
323
+ };
324
+
325
+ // 获取当前坐标类型
326
+ const getCurrentCoordinateType = async () => {
327
+ const result = await CPBaiduLocation.getCoordinateType();
328
+ console.log('当前坐标类型:', result.type);
329
+ };
330
+
331
+
332
+ ```
333
+
334
+ ## 注意事项
335
+
336
+ 1. **API Key 申请**:
337
+ - 请确保在 [百度地图开放平台](https://lbs.baidu.com/) 申请了正确的 API Key
338
+ - Android、iOS、JSSDK 需要分别申请不同的 API Key
339
+ - 确保 API Key 已启用定位服务
340
+
341
+ 2. **权限管理**:
342
+ - 定位功能需要用户授权
343
+ - 请在适当的时机检查和请求权限
344
+
345
+ 3. **网络连接**:
346
+ - 获取地址信息和位置描述需要网络连接
347
+ - 基础定位功能可以在离线状态下使用
348
+
349
+ ## 错误码说明
350
+
351
+ [Android 错误码](https://lbs.baidu.com/faq/api?title=android-locsdk/guide/addition-func/error-code)
352
+
353
+ [iOS 错误码](https://lbs.baidu.com/faq/api?title=ios-locsdk/guide/addition-func/error-code)
354
+
355
+ ## 故障排除
356
+
357
+ ### API Key 设置失败
358
+ **问题**:调用 `setAK` 方法返回失败
359
+ **解决方案**:
360
+ - 检查 API Key 是否正确
361
+ - 检查网络连接是否正常
362
+ - 确保 API Key 已在百度地图开放平台启用
363
+
364
+ ### 权限请求失败
365
+ **问题**:用户拒绝了定位权限
366
+ **解决方案**:
367
+ - 提示用户定位权限的重要性
368
+ - 引导用户在系统设置中手动开启权限
369
+ - 优雅处理无权限的情况
370
+
371
+ ## 相关文档
372
+
373
+ - [百度地图开放平台](https://lbs.baidu.com/)
374
+ - [百度地图 Android 定位 SDK 文档](https://lbs.baidu.com/faq/api?title=android-locsdk/guide/get-location/latlng)
375
+ - [百度地图 iOS 定位 SDK 文档](https://lbs.baidu.com/faq/api?title=ios-locsdk/guide/get-location/once)
376
+ - [Capacitor 插件开发指南](https://capacitorjs.com/docs/plugins)
@@ -0,0 +1,71 @@
1
+ ext {
2
+ junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
3
+ androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.0'
4
+ androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.2.1'
5
+ androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.6.1'
6
+ }
7
+
8
+ buildscript {
9
+ repositories {
10
+ google()
11
+ mavenCentral()
12
+ }
13
+ dependencies {
14
+ classpath 'com.android.tools.build:gradle:8.7.2'
15
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.25"
16
+ }
17
+ }
18
+
19
+ apply plugin: 'com.android.library'
20
+ apply plugin: 'org.jetbrains.kotlin.android'
21
+
22
+ android {
23
+ namespace "com.hybrid.baidu.location"
24
+ compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 35
25
+ defaultConfig {
26
+ minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 23
27
+ targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 35
28
+ versionCode 1
29
+ versionName "1.0"
30
+ testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
31
+ // 指定支持的SO架构
32
+ ndk {
33
+ abiFilters 'arm64-v8a', 'x86_64'
34
+ }
35
+ }
36
+ buildTypes {
37
+ release {
38
+ minifyEnabled false
39
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
40
+ }
41
+ }
42
+ lintOptions {
43
+ abortOnError false
44
+ }
45
+ compileOptions {
46
+ sourceCompatibility JavaVersion.VERSION_21
47
+ targetCompatibility JavaVersion.VERSION_21
48
+ }
49
+ kotlinOptions {
50
+ jvmTarget = "21"
51
+ }
52
+ sourceSets {
53
+ main {
54
+ jniLibs.srcDirs = ['libs']
55
+ }
56
+ }
57
+ }
58
+
59
+ dependencies {
60
+ // 本地AAR依赖(必须用implementation,确保传递到宿主)
61
+ implementation fileTree(dir: 'libs', include: ['*.jar'])
62
+ implementation project(':capacitor-android')
63
+ implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
64
+ testImplementation "junit:junit:$junitVersion"
65
+ androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
66
+ androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
67
+ // 引入 OkHttp3 核心库
68
+ implementation 'com.squareup.okhttp3:okhttp:3.14.9'
69
+ // Kotlin标准库
70
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.25"
71
+ }
@@ -0,0 +1,32 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ <!-- 这个权限用于进行网络定位-->
3
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
4
+ <!-- 这个权限用于访问系统接口提供的卫星定位信息-->
5
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
6
+ <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
7
+ <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
8
+ <!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
9
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
10
+ <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
11
+ <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
12
+ <!-- 用于读取手机当前的状态-->
13
+ <uses-permission android:name="android.permission.READ_PHONE_STATE" />
14
+ <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据-->
15
+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
16
+ <!-- 访问网络,网络定位需要上网-->
17
+ <uses-permission android:name="android.permission.INTERNET" />
18
+ <!-- Android 10.0+ 定位需要的权限-->
19
+ <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
20
+
21
+ <application>
22
+ <!-- 百度地图AK配置 -->
23
+ <meta-data
24
+ android:name="com.baidu.lbsapi.API_KEY"
25
+ android:value="" />
26
+ <!-- 百度定位服务组件 -->
27
+ <service
28
+ android:name="com.baidu.location.f"
29
+ android:enabled="true"
30
+ android:process=":remote" />
31
+ </application>
32
+ </manifest>