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,167 @@
1
+ import Foundation
2
+ import Capacitor
3
+
4
+ /**
5
+ * 百度地图定位插件iOS端入口类
6
+ * Please read the Capacitor iOS Plugin Development Guide
7
+ * here: https://capacitorjs.com/docs/plugins/ios
8
+ */
9
+ @objc(CPBaiduLocationPlugin)
10
+ public class CPBaiduLocationPlugin: CAPPlugin, CAPBridgedPlugin {
11
+ public let identifier = "CPBaiduLocationPlugin"
12
+ public let jsName = "CPBaiduLocation"
13
+ public let pluginMethods: [CAPPluginMethod] = [
14
+ CAPPluginMethod(name: "setAK", returnType: CAPPluginReturnPromise),
15
+ CAPPluginMethod(name: "checkAKValidation", returnType: CAPPluginReturnPromise),
16
+ CAPPluginMethod(name: "checkPermission", returnType: CAPPluginReturnPromise),
17
+ CAPPluginMethod(name: "requestPermission", returnType: CAPPluginReturnPromise),
18
+ CAPPluginMethod(name: "getCurrentPosition", returnType: CAPPluginReturnPromise),
19
+ CAPPluginMethod(name: "setCoordinateType", returnType: CAPPluginReturnPromise),
20
+ CAPPluginMethod(name: "getCoordinateType", returnType: CAPPluginReturnPromise)
21
+ ]
22
+
23
+ /**
24
+ * 插件配置对象
25
+ */
26
+ private var pluginConfig: CPBaiduLocationConfig
27
+
28
+ /**
29
+ * 实现实例
30
+ */
31
+ private let implementation: CPBaiduLocation
32
+
33
+ /**
34
+ * 初始化插件
35
+ */
36
+ override init() {
37
+ // 初始化默认配置
38
+ self.pluginConfig = CPBaiduLocationConfig(from: nil)
39
+ // 使用默认配置初始化实现实例
40
+ self.implementation = CPBaiduLocation(config: self.pluginConfig)
41
+ super.init()
42
+ }
43
+
44
+ /**
45
+ * 加载插件,从Capacitor配置更新插件配置
46
+ */
47
+ override public func load() {
48
+ super.load()
49
+
50
+ // 从Capacitor配置更新插件配置
51
+ pluginConfig = CPBaiduLocationConfig(from: self.getConfig())
52
+ }
53
+
54
+ /**
55
+ * 获取当前位置信息
56
+ * @param call 插件调用对象,包含定位参数选项
57
+ * - needAddress: 是否需要地址信息,默认false
58
+ * - needLocationDescribe: 是否需要位置描述,默认false
59
+ * @returns 定位结果,包含经纬度、精度、地址信息等
60
+ */
61
+ @objc func getCurrentPosition(_ call: CAPPluginCall) {
62
+ let needAddress = call.getBool("needAddress") ?? false
63
+ let needLocationDescribe = call.getBool("needLocationDescribe") ?? false
64
+
65
+ implementation.getCurrentPosition(needAddress: needAddress, needLocationDescribe: needLocationDescribe) { result in
66
+ call.resolve(result)
67
+ }
68
+ }
69
+
70
+ /**
71
+ * 设置百度地图API Key
72
+ * @param call 插件调用对象,包含API Key
73
+ * - ak: 百度地图API Key,可选
74
+ * @returns 设置结果,包含success、errorCode和errorMessage
75
+ */
76
+ @objc func setAK(_ call: CAPPluginCall) {
77
+ // 获取传递的AK值
78
+ var key: String? = call.getString("ak")
79
+
80
+ // 如果没有传AK参数,从配置文件获取
81
+ if key == nil || key?.isEmpty == true {
82
+ if let iOSAK = pluginConfig.iOSAK {
83
+ // 优先使用iOSAK,然后是ak
84
+ key = iOSAK
85
+ }
86
+ }
87
+
88
+ // 确保key有值
89
+ guard let validKey = key, !validKey.isEmpty else {
90
+ call.resolve([
91
+ "success": false,
92
+ "errorCode": -1,
93
+ "errorMessage": "API Key不能为空"
94
+ ])
95
+ return
96
+ }
97
+
98
+ implementation.setAK(validKey) { result in
99
+ call.resolve(result)
100
+ }
101
+ }
102
+
103
+ /**
104
+ * 检查AK验证状态
105
+ * @param call 插件调用对象
106
+ * @returns AK验证状态,包含valid和errorMessage
107
+ */
108
+ @objc func checkAKValidation(_ call: CAPPluginCall) {
109
+ // 百度定位SDK没有提供直接检查AK验证状态的方法
110
+ // 这里我们简单返回true,实际验证会在定位时进行
111
+ let ret = [
112
+ "valid": true
113
+ ]
114
+ call.resolve(ret)
115
+ }
116
+
117
+ /**
118
+ * 设置坐标类型
119
+ * @param call 插件调用对象,包含坐标类型
120
+ * - type: 坐标类型,可选值:BD09LL、BD09MC、GCJ02、WGS84
121
+ * @returns 设置结果,包含success和errorMessage
122
+ */
123
+ @objc func setCoordinateType(_ call: CAPPluginCall) {
124
+ guard let type = call.getString("type") else {
125
+ call.resolve([
126
+ "success": false,
127
+ "errorMessage": "Coordinate type cannot be empty"
128
+ ])
129
+ return
130
+ }
131
+
132
+ implementation.setCoordinateType(type) {
133
+ call.resolve(["success": true])
134
+ }
135
+ }
136
+
137
+ /**
138
+ * 获取当前坐标类型
139
+ * @param call 插件调用对象
140
+ * @returns 当前坐标类型,返回值:BD09LL、BD09MC、GCJ02、WGS84
141
+ */
142
+ @objc func getCoordinateType(_ call: CAPPluginCall) {
143
+ let type = implementation.getCoordinateType()
144
+ call.resolve(["type": type])
145
+ }
146
+
147
+ /**
148
+ * 检查定位权限
149
+ * @param call 插件调用对象
150
+ * @returns 权限状态,包含granted字段
151
+ */
152
+ @objc func checkPermission(_ call: CAPPluginCall) {
153
+ let result = implementation.checkPermission()
154
+ call.resolve(result)
155
+ }
156
+
157
+ /**
158
+ * 请求定位权限
159
+ * @param call 插件调用对象
160
+ * @returns 权限请求结果,包含granted字段
161
+ */
162
+ @objc func requestPermission(_ call: CAPPluginCall) {
163
+ implementation.requestPermission { result in
164
+ call.resolve(result)
165
+ }
166
+ }
167
+ }
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "capacitor-baidu-location",
3
+ "version": "0.0.1",
4
+ "description": "baidu location kit for ios and android",
5
+ "main": "dist/plugin.cjs.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/esm/index.d.ts",
8
+ "unpkg": "dist/plugin.js",
9
+ "files": [
10
+ "android/src/main/",
11
+ "android/build.gradle",
12
+ "dist/",
13
+ "ios/Sources",
14
+ "Package.swift",
15
+ "CapacitorBaiduLocation.podspec"
16
+ ],
17
+ "author": "example",
18
+ "license": "MIT",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/example.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/example/issues"
25
+ },
26
+ "keywords": [
27
+ "capacitor",
28
+ "plugin",
29
+ "native",
30
+ "location",
31
+ "baidu"
32
+ ],
33
+ "scripts": {
34
+ "verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
35
+ "verify:ios": "xcodebuild -scheme CapacitorBaiduLocation -destination generic/platform=iOS",
36
+ "verify:android": "cd android && ./gradlew clean build test && cd ..",
37
+ "verify:web": "npm run build",
38
+ "lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
39
+ "fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
40
+ "eslint": "eslint . --ext ts",
41
+ "prettier": "prettier \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
42
+ "swiftlint": "node-swiftlint",
43
+ "docgen": "docgen --api CPBaiduLocationPlugin --output-readme README.md --output-json dist/docs.json",
44
+ "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
45
+ "clean": "rimraf ./dist",
46
+ "watch": "tsc --watch",
47
+ "prepublishOnly": "npm run build"
48
+ },
49
+ "devDependencies": {
50
+ "@capacitor/android": "^7.0.0",
51
+ "@capacitor/cli": "^7.0.0",
52
+ "@capacitor/core": "^7.0.0",
53
+ "@capacitor/docgen": "^0.3.0",
54
+ "@capacitor/ios": "^7.0.0",
55
+ "@ionic/eslint-config": "^0.4.0",
56
+ "@ionic/prettier-config": "^4.0.0",
57
+ "@ionic/swiftlint-config": "^2.0.0",
58
+ "eslint": "^8.57.0",
59
+ "prettier": "^3.4.2",
60
+ "prettier-plugin-java": "^2.6.6",
61
+ "rimraf": "^6.0.1",
62
+ "rollup": "^4.30.1",
63
+ "swiftlint": "^2.0.0",
64
+ "typescript": "5.9.3"
65
+ },
66
+ "peerDependencies": {
67
+ "@capacitor/core": ">=7.0.0"
68
+ },
69
+ "prettier": "@ionic/prettier-config",
70
+ "swiftlint": "@ionic/swiftlint-config",
71
+ "eslintConfig": {
72
+ "extends": "@ionic/eslint-config/recommended"
73
+ },
74
+ "capacitor": {
75
+ "ios": {
76
+ "src": "ios"
77
+ },
78
+ "android": {
79
+ "src": "android"
80
+ }
81
+ }
82
+ }