expo-gaode-map 2.2.26-next.0 → 2.2.26-next.2

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 (70) hide show
  1. package/README.md +47 -1
  2. package/android/build.gradle +28 -1
  3. package/android/src/main/cpp/CMakeLists.txt +2 -0
  4. package/android/src/main/java/expo/modules/gaodemap/ExpoGaodeMapModule.kt +33 -50
  5. package/android/src/main/java/expo/modules/gaodemap/ExpoGaodeMapOfflineModule.kt +116 -54
  6. package/android/src/main/java/expo/modules/gaodemap/ExpoGaodeMapView.kt +36 -63
  7. package/android/src/main/java/expo/modules/gaodemap/ExpoGaodeMapViewModule.kt +13 -3
  8. package/android/src/main/java/expo/modules/gaodemap/modules/SDKInitializer.kt +41 -14
  9. package/build/ExpoGaodeMapModule.d.ts.map +1 -1
  10. package/build/ExpoGaodeMapModule.js +90 -15
  11. package/build/ExpoGaodeMapModule.js.map +1 -1
  12. package/build/ExpoGaodeMapOfflineModule.d.ts.map +1 -1
  13. package/build/ExpoGaodeMapOfflineModule.js +12 -2
  14. package/build/ExpoGaodeMapOfflineModule.js.map +1 -1
  15. package/build/ExpoGaodeMapView.d.ts.map +1 -1
  16. package/build/ExpoGaodeMapView.js +3 -2
  17. package/build/ExpoGaodeMapView.js.map +1 -1
  18. package/build/components/overlays/Circle.d.ts.map +1 -1
  19. package/build/components/overlays/Circle.js +3 -2
  20. package/build/components/overlays/Circle.js.map +1 -1
  21. package/build/components/overlays/Cluster.d.ts.map +1 -1
  22. package/build/components/overlays/Cluster.js +3 -2
  23. package/build/components/overlays/Cluster.js.map +1 -1
  24. package/build/components/overlays/HeatMap.d.ts.map +1 -1
  25. package/build/components/overlays/HeatMap.js +3 -2
  26. package/build/components/overlays/HeatMap.js.map +1 -1
  27. package/build/components/overlays/Marker.d.ts.map +1 -1
  28. package/build/components/overlays/Marker.js +3 -2
  29. package/build/components/overlays/Marker.js.map +1 -1
  30. package/build/components/overlays/MultiPoint.d.ts.map +1 -1
  31. package/build/components/overlays/MultiPoint.js +3 -2
  32. package/build/components/overlays/MultiPoint.js.map +1 -1
  33. package/build/components/overlays/Polygon.d.ts.map +1 -1
  34. package/build/components/overlays/Polygon.js +3 -2
  35. package/build/components/overlays/Polygon.js.map +1 -1
  36. package/build/components/overlays/Polyline.d.ts.map +1 -1
  37. package/build/components/overlays/Polyline.js +3 -2
  38. package/build/components/overlays/Polyline.js.map +1 -1
  39. package/build/index.d.ts +1 -0
  40. package/build/index.d.ts.map +1 -1
  41. package/build/index.js +2 -0
  42. package/build/index.js.map +1 -1
  43. package/build/types/common.types.d.ts +9 -0
  44. package/build/types/common.types.d.ts.map +1 -1
  45. package/build/types/common.types.js.map +1 -1
  46. package/build/types/native-module.types.d.ts +17 -1
  47. package/build/types/native-module.types.d.ts.map +1 -1
  48. package/build/types/native-module.types.js.map +1 -1
  49. package/build/utils/GeoUtils.d.ts.map +1 -1
  50. package/build/utils/GeoUtils.js +10 -3
  51. package/build/utils/GeoUtils.js.map +1 -1
  52. package/build/utils/lazyNativeViewManager.d.ts +3 -0
  53. package/build/utils/lazyNativeViewManager.d.ts.map +1 -0
  54. package/build/utils/lazyNativeViewManager.js +11 -0
  55. package/build/utils/lazyNativeViewManager.js.map +1 -0
  56. package/build/utils/throttle.d.ts +10 -0
  57. package/build/utils/throttle.d.ts.map +1 -0
  58. package/build/utils/throttle.js +19 -0
  59. package/build/utils/throttle.js.map +1 -0
  60. package/ios/ExpoGaodeMap.podspec +5 -4
  61. package/ios/ExpoGaodeMapModule.swift +17 -43
  62. package/ios/ExpoGaodeMapView.swift +171 -113
  63. package/ios/ExpoGaodeMapViewModule.swift +10 -2
  64. package/ios/GaodeMapPrivacyManager.swift +65 -0
  65. package/ios/modules/LocationManager.swift +43 -24
  66. package/package.json +7 -11
  67. package/plugin/build/withGaodeMap.d.ts +6 -1
  68. package/plugin/build/withGaodeMap.js +58 -13
  69. package/android/src/main/java/expo/modules/gaodemap/MapPreloadManager.kt +0 -494
  70. package/ios/MapPreloadManager.swift +0 -348
package/README.md CHANGED
@@ -30,7 +30,7 @@
30
30
  - ✅ 高测试覆盖率(75.7%,207 个单元测试)
31
31
  - ✅ 支持自定义Marker覆盖物
32
32
  - ✅ 默认实现了优化地图加载,减少内存占用
33
- - ✅ 几何运算(距离计算、面积计算等,由 C++ 实现)
33
+ - ✅ 几何运算(距离/面积、点在圆/多边形、质心/边界、路径长度/抽稀、GeoHash、瓦片/像素坐标转换、最近点、热力网格等,由 C++ 实现)
34
34
  - ✅ 丰富的使用案例
35
35
  - ✅ 提供AI编程助手,帮助开发者快速集成和使用(https://TomWq.github.io/expo-gaode-map/guide/ai-skills.html)
36
36
  - ✅ 更多内容和功能请查看 [完整文档](https://TomWq.github.io/expo-gaode-map/)
@@ -44,6 +44,14 @@
44
44
 
45
45
  ## 📦 安装
46
46
 
47
+ > ⚠️ **版本兼容性说明**:
48
+ > - 如果你的项目使用 **Expo SDK 54 及以上**,请安装 默认的 版本。
49
+ > - 如果你的项目使用 **Expo SDK 53 及以下**(如 50, 51, 52, 53),请使用 **V1** 版本(Tag: `v1`)。
50
+ > ```bash
51
+ > npm install expo-gaode-map@v1
52
+ > ```
53
+ > **说明**:V1 版本除了不支持**世界地图**功能外,其余 API 与 V2 (Latest) 版本完全一致。
54
+
47
55
  ### 方案一:仅使用地图和定位(核心包)
48
56
 
49
57
  ```bash
@@ -162,9 +170,46 @@ try {
162
170
  }
163
171
  }
164
172
  ```
173
+ ### 5. 地图在模拟器上显示异常(可能会出现黑屏或闪退)
174
+
175
+ 建议:
176
+ - 确保在真机上测试,模拟器可能不支持地图功能
177
+ - 检查是否正确配置了 API Key
165
178
 
166
179
  **完整错误处理指南**:[ERROR_HANDLING_GUIDE.md](./ERROR_HANDLING_GUIDE.md)
167
180
 
181
+ ### 6. Google Play 版本支持
182
+
183
+ 如果你的应用需要上架 Google Play,需要使用高德地图 Google Play 版本的 SDK(通过了 Google Play 的合规审核)。
184
+
185
+ #### 步骤 1:下载 SDK
186
+ 前往 [高德开放平台](https://lbs.amap.com/api/android-sdk/download) 下载包含 Google Play 版本的 SDK(通常是合包,包含地图、搜索、定位功能)。
187
+
188
+ #### 步骤 2:放入项目
189
+ 将下载的 `.aar` 或 `.jar` 文件放入你的 Expo 项目根目录下的 `libs` 文件夹中(如果没有则创建),例如 `libs/AMap_3DMap_GooglePlay.aar`。
190
+
191
+ #### 步骤 3:配置插件
192
+ 在 `app.json` 中配置 `customMapSdkPath`:
193
+
194
+ ```json
195
+ {
196
+ "expo": {
197
+ "plugins": [
198
+ [
199
+ "expo-gaode-map",
200
+ {
201
+ "androidKey": "your-android-key",
202
+ "iosKey": "your-ios-key",
203
+ "customMapSdkPath": "./libs/AMap_3DMap_GooglePlay.aar"
204
+ }
205
+ ]
206
+ ]
207
+ }
208
+ }
209
+ ```
210
+
211
+ > **注意**:Google Play 版本的 SDK 通常是合包(包含地图+搜索+定位)。如果你的项目中引入了其他高德相关依赖(如 `expo-gaode-map-search`),可能会导致类冲突。插件会自动处理大部分冲突,但如果依然遇到 `Duplicate class` 错误,请检查是否引入了多余的依赖。
212
+
168
213
  支持的错误类型:
169
214
  - `SDK_NOT_INITIALIZED` - SDK 未初始化
170
215
  - `INVALID_API_KEY` - API Key 配置错误
@@ -185,6 +230,7 @@ MIT
185
230
 
186
231
  - [在线文档](https://TomWq.github.io/expo-gaode-map/)
187
232
  - [错误处理指南](./ERROR_HANDLING_GUIDE.md) 🆕
233
+ - [性能优化指南](./PERFORMANCE_GUIDE.md) 🆕
188
234
  - [GitHub 仓库](https://github.com/TomWq/expo-gaode-map)
189
235
  - [示例项目(地图)](https://github.com/TomWq/expo-gaode-map-example)
190
236
  - [示例项目(导航)](https://github.com/TomWq/expo-gaode-map-navigation-example)
@@ -31,6 +31,9 @@ if (useManagedAndroidSdkVersions) {
31
31
  }
32
32
  }
33
33
 
34
+ logger.warn("[expo-gaode-map] Thank you for using expo-gaode-map ❤️")
35
+ logger.warn("[expo-gaode-map] If you enjoy using expo-gaode-map, please consider sponsoring this project: https://github.com/TomWq")
36
+
34
37
  android {
35
38
  namespace "expo.modules.gaodemap"
36
39
  defaultConfig {
@@ -54,5 +57,29 @@ android {
54
57
 
55
58
  dependencies {
56
59
  // 高德地图 3D SDK
57
- implementation ('com.amap.api:3dmap:latest.integration')
60
+ def customSdkPath = null
61
+ println "ExpoGaodeMap: Checking for custom SDK property in rootProject: ${rootProject.name}"
62
+ if (rootProject.hasProperty("EXPO_GAODE_MAP_CUSTOM_SDK_PATH")) {
63
+ customSdkPath = rootProject.property("EXPO_GAODE_MAP_CUSTOM_SDK_PATH")
64
+ println "ExpoGaodeMap: Found custom SDK path property: ${customSdkPath}"
65
+ } else {
66
+ println "ExpoGaodeMap: No custom SDK path property found."
67
+ }
68
+
69
+ if (customSdkPath) {
70
+ def sdkFile = rootProject.file(customSdkPath)
71
+ if (!sdkFile.exists()) {
72
+ // Try to find the file relative to the project root (parent of android folder)
73
+ sdkFile = new File(rootProject.projectDir.parentFile, customSdkPath)
74
+ }
75
+
76
+ if (sdkFile.exists()) {
77
+ println "ExpoGaodeMap: Using custom Map SDK from ${sdkFile.absolutePath}"
78
+ implementation files(sdkFile)
79
+ } else {
80
+ throw new FileNotFoundException("ExpoGaodeMap: Could not find custom SDK at ${customSdkPath}. Please check your customMapSdkPath configuration.")
81
+ }
82
+ } else {
83
+ implementation ('com.amap.api:3dmap:latest.integration')
84
+ }
58
85
  }
@@ -22,3 +22,5 @@ find_library(log-lib log)
22
22
  target_link_libraries(gaodecluster
23
23
  ${log-lib}
24
24
  )
25
+
26
+ target_link_options(gaodecluster PRIVATE "-Wl,-z,max-page-size=16384")
@@ -30,42 +30,6 @@ class ExpoGaodeMapModule : Module() {
30
30
  override fun definition() = ModuleDefinition {
31
31
  Name("ExpoGaodeMap")
32
32
 
33
- // 在模块加载时尝试从本地缓存恢复隐私同意状态,避免每次启动都必须 JS 调用
34
- try {
35
- val context = appContext.reactContext!!
36
- SDKInitializer.restorePrivacyState(context)
37
-
38
- // 初始化预加载管理器(注册内存监听)
39
- MapPreloadManager.initialize(context)
40
-
41
- // 尝试从 AndroidManifest.xml 读取并设置 API Key
42
- val apiKey = context.packageManager
43
- .getApplicationInfo(context.packageName, android.content.pm.PackageManager.GET_META_DATA)
44
- .metaData?.getString("com.amap.api.v2.apikey")
45
-
46
- if (!apiKey.isNullOrEmpty()) {
47
- try {
48
- MapsInitializer.setApiKey(apiKey)
49
- com.amap.api.location.AMapLocationClient.setApiKey(apiKey)
50
-
51
-
52
- // 只有在 API Key 已设置的情况下才启动预加载
53
- android.os.Handler(android.os.Looper.getMainLooper()).postDelayed({
54
- android.util.Log.i("ExpoGaodeMap", "🚀 自动启动地图预加载")
55
- MapPreloadManager.startPreload(context, poolSize = 1)
56
- }, 2000)
57
- } catch (e: Exception) {
58
- android.util.Log.w("ExpoGaodeMap", "设置 API Key 失败: ${e.message}")
59
- }
60
- } else {
61
- android.util.Log.w("ExpoGaodeMap", "⚠️ AndroidManifest.xml 未找到 API Key,跳过自动预加载")
62
- }
63
-
64
- } catch (e: Exception) {
65
- android.util.Log.w("ExpoGaodeMap", "恢复隐私状态时出现问题: ${e.message}")
66
- }
67
-
68
-
69
33
  // ==================== SDK 初始化 ====================
70
34
 
71
35
  /**
@@ -74,23 +38,43 @@ class ExpoGaodeMapModule : Module() {
74
38
  */
75
39
  Function("initSDK") { config: Map<String, String> ->
76
40
  val androidKey = config["androidKey"]
77
- if (androidKey != null) {
78
- try {
79
- SDKInitializer.initSDK(appContext.reactContext!!, androidKey)
80
- getLocationManager() // 初始化定位管理器
81
-
82
- // 初始化成功后自动触发一次预加载
83
- MapPreloadManager.startPreload(appContext.reactContext!!, poolSize = 1)
84
- } catch (e: SecurityException) {
85
- android.util.Log.e("ExpoGaodeMap", "隐私协议未同意: ${e.message}")
86
- throw expo.modules.kotlin.exception.CodedException("PRIVACY_NOT_AGREED", e.message ?: "用户未同意隐私协议", e)
87
- } catch (e: Exception) {
88
- android.util.Log.e("ExpoGaodeMap", "SDK 初始化失败: ${e.message}")
89
- throw expo.modules.kotlin.exception.CodedException("INIT_FAILED", e.message ?: "SDK 初始化失败", e)
41
+ try {
42
+ val context = appContext.reactContext!!
43
+ if (androidKey != null) {
44
+ SDKInitializer.initSDK(context, androidKey)
45
+ } else if (!SDKInitializer.isPrivacyReady()) {
46
+ throw expo.modules.kotlin.exception.CodedException(
47
+ "PRIVACY_NOT_AGREED",
48
+ "隐私协议未完成确认,请先调用 setPrivacyShow/setPrivacyAgree",
49
+ null
50
+ )
51
+ } else {
52
+ SDKInitializer.applyPrivacyState(context)
90
53
  }
54
+
55
+ getLocationManager()
56
+ android.util.Log.i("ExpoGaodeMap", "✅ SDK 初始化完成 - Version: ${MapsInitializer.getVersion()}")
57
+ } catch (e: SecurityException) {
58
+ android.util.Log.e("ExpoGaodeMap", "隐私协议未同意: ${e.message}")
59
+ throw expo.modules.kotlin.exception.CodedException("PRIVACY_NOT_AGREED", e.message ?: "用户未同意隐私协议", e)
60
+ } catch (e: Exception) {
61
+ android.util.Log.e("ExpoGaodeMap", "SDK 初始化失败: ${e.message}")
62
+ throw expo.modules.kotlin.exception.CodedException("INIT_FAILED", e.message ?: "SDK 初始化失败", e)
91
63
  }
92
64
  }
93
65
 
66
+ Function("setPrivacyShow") { hasShow: Boolean, hasContainsPrivacy: Boolean ->
67
+ SDKInitializer.setPrivacyShow(appContext.reactContext!!, hasShow, hasContainsPrivacy)
68
+ }
69
+
70
+ Function("setPrivacyAgree") { hasAgree: Boolean ->
71
+ SDKInitializer.setPrivacyAgree(appContext.reactContext!!, hasAgree)
72
+ }
73
+
74
+ Function("getPrivacyStatus") {
75
+ SDKInitializer.getPrivacyStatus()
76
+ }
77
+
94
78
  /**
95
79
  * 设置是否加载世界向量地图
96
80
  * @param enable 是否开启
@@ -890,7 +874,6 @@ class ExpoGaodeMapModule : Module() {
890
874
  OnDestroy {
891
875
  locationManager?.destroy()
892
876
  locationManager = null
893
- MapPreloadManager.cleanup()
894
877
  }
895
878
  }
896
879
 
@@ -7,6 +7,8 @@ import com.amap.api.maps.offlinemap.OfflineMapCity
7
7
  import com.amap.api.maps.offlinemap.OfflineMapManager
8
8
  import com.amap.api.maps.offlinemap.OfflineMapProvince
9
9
  import com.amap.api.maps.offlinemap.OfflineMapStatus
10
+ import expo.modules.gaodemap.modules.SDKInitializer
11
+ import expo.modules.kotlin.exception.CodedException
10
12
  import expo.modules.kotlin.modules.Module
11
13
  import expo.modules.kotlin.modules.ModuleDefinition
12
14
 
@@ -21,6 +23,42 @@ class ExpoGaodeMapOfflineModule : Module() {
21
23
 
22
24
  // 线程安全锁
23
25
  private val lock = Any()
26
+
27
+ private fun createDownloadListener(): OfflineMapManager.OfflineMapDownloadListener {
28
+ return object : OfflineMapManager.OfflineMapDownloadListener {
29
+ override fun onDownload(status: Int, completeCode: Int, downName: String?) {
30
+ handleDownloadStatus(status, completeCode, downName)
31
+ }
32
+
33
+ override fun onCheckUpdate(hasNew: Boolean, name: String?) {
34
+ }
35
+
36
+ override fun onRemove(success: Boolean, name: String?, describe: String?) {
37
+ }
38
+ }
39
+ }
40
+
41
+ private fun getOfflineMapManager(): OfflineMapManager {
42
+ if (!SDKInitializer.isPrivacyReady()) {
43
+ throw CodedException(
44
+ "PRIVACY_NOT_AGREED",
45
+ "隐私协议未完成确认,请先调用 setPrivacyShow/setPrivacyAgree",
46
+ null
47
+ )
48
+ }
49
+
50
+ val reactContext = appContext.reactContext
51
+ ?: throw CodedException("NO_CONTEXT", "React context not available", null)
52
+
53
+ if (offlineMapManager == null) {
54
+ offlineMapManager = OfflineMapManager(
55
+ reactContext.applicationContext,
56
+ createDownloadListener()
57
+ )
58
+ }
59
+
60
+ return offlineMapManager!!
61
+ }
24
62
 
25
63
  override fun definition() = ModuleDefinition {
26
64
  Name("ExpoGaodeMapOffline")
@@ -36,25 +74,6 @@ class ExpoGaodeMapOfflineModule : Module() {
36
74
  "onDownloadCancelled"
37
75
  )
38
76
 
39
- // ==================== 模块生命周期 ====================
40
-
41
- OnCreate {
42
- // 初始化离线地图管理器
43
- offlineMapManager = OfflineMapManager(appContext.reactContext, object : OfflineMapManager.OfflineMapDownloadListener {
44
- override fun onDownload(status: Int, completeCode: Int, downName: String?) {
45
- handleDownloadStatus(status, completeCode, downName)
46
- }
47
-
48
- override fun onCheckUpdate(hasNew: Boolean, name: String?) {
49
- // 更新检查回调
50
- }
51
-
52
- override fun onRemove(success: Boolean, name: String?, describe: String?) {
53
- // 删除回调
54
- }
55
- })
56
- }
57
-
58
77
  OnDestroy {
59
78
  offlineMapManager?.destroy()
60
79
  offlineMapManager = null
@@ -64,24 +83,24 @@ class ExpoGaodeMapOfflineModule : Module() {
64
83
  // ==================== 地图列表管理 ====================
65
84
 
66
85
  AsyncFunction("getAvailableCities") {
67
- val cities = offlineMapManager?.offlineMapCityList ?: emptyList()
86
+ val cities = getOfflineMapManager().offlineMapCityList ?: emptyList()
68
87
  cities.map { city -> convertCityToMap(city) }
69
88
  }
70
89
 
71
90
  AsyncFunction("getAvailableProvinces") {
72
- val provinces = offlineMapManager?.offlineMapProvinceList ?: emptyList()
91
+ val provinces = getOfflineMapManager().offlineMapProvinceList ?: emptyList()
73
92
  provinces.map { province -> convertProvinceToMap(province) }
74
93
  }
75
94
 
76
95
  AsyncFunction("getCitiesByProvince") { provinceCode: String ->
77
- val province = offlineMapManager?.offlineMapProvinceList?.find {
96
+ val province = getOfflineMapManager().offlineMapProvinceList?.find {
78
97
  it.provinceCode == provinceCode
79
98
  }
80
99
  province?.cityList?.map { city -> convertCityToMap(city) } ?: emptyList()
81
100
  }
82
101
 
83
102
  AsyncFunction("getDownloadedMaps") {
84
- val cities = offlineMapManager?.downloadOfflineMapCityList ?: emptyList()
103
+ val cities = getOfflineMapManager().downloadOfflineMapCityList ?: emptyList()
85
104
  cities.map { city -> convertCityToMap(city) }
86
105
  }
87
106
 
@@ -95,11 +114,11 @@ class ExpoGaodeMapOfflineModule : Module() {
95
114
  downloadingCities.add(cityCode)
96
115
  pausedCities.remove(cityCode)
97
116
  }
98
- offlineMapManager?.downloadByCityCode(cityCode)
117
+ getOfflineMapManager().downloadByCityCode(cityCode)
99
118
  }
100
119
 
101
120
  AsyncFunction("pauseDownload") { cityCode: String ->
102
- val city = offlineMapManager?.getItemByCityCode(cityCode)
121
+ val city = getOfflineMapManager().getItemByCityCode(cityCode)
103
122
 
104
123
  synchronized(lock) {
105
124
  pausedCities.add(cityCode)
@@ -108,7 +127,7 @@ class ExpoGaodeMapOfflineModule : Module() {
108
127
 
109
128
  // 使用 pauseByName 暂停指定城市
110
129
  city?.city?.let { cityName ->
111
- offlineMapManager?.pauseByName(cityName)
130
+ getOfflineMapManager().pauseByName(cityName)
112
131
  }
113
132
 
114
133
  if (city != null) {
@@ -126,11 +145,11 @@ class ExpoGaodeMapOfflineModule : Module() {
126
145
  }
127
146
  // Android SDK 没有针对单个城市的恢复方法
128
147
  // 需要重新调用 downloadByCityCode 来继续下载
129
- offlineMapManager?.downloadByCityCode(cityCode)
148
+ getOfflineMapManager().downloadByCityCode(cityCode)
130
149
  }
131
150
 
132
151
  AsyncFunction("cancelDownload") { cityCode: String ->
133
- val city = offlineMapManager?.getItemByCityCode(cityCode)
152
+ val city = getOfflineMapManager().getItemByCityCode(cityCode)
134
153
 
135
154
  synchronized(lock) {
136
155
  downloadingCities.remove(cityCode)
@@ -138,7 +157,7 @@ class ExpoGaodeMapOfflineModule : Module() {
138
157
  }
139
158
 
140
159
  // 使用 stop() 停止所有下载(包括队列)
141
- offlineMapManager?.stop()
160
+ getOfflineMapManager().stop()
142
161
 
143
162
  if (city != null) {
144
163
  sendEvent("onDownloadCancelled", Bundle().apply {
@@ -149,11 +168,11 @@ class ExpoGaodeMapOfflineModule : Module() {
149
168
  }
150
169
 
151
170
  AsyncFunction("deleteMap") { cityCode: String ->
152
- val city = offlineMapManager?.getItemByCityCode(cityCode)
171
+ val city = getOfflineMapManager().getItemByCityCode(cityCode)
153
172
  ?: throw IllegalArgumentException("City not found: $cityCode")
154
173
 
155
174
  // 官方文档:remove() 需要传入城市名称,不是城市代码
156
- offlineMapManager?.remove(city.city)
175
+ getOfflineMapManager().remove(city.city)
157
176
 
158
177
  synchronized(lock) {
159
178
  downloadingCities.remove(cityCode)
@@ -165,29 +184,29 @@ class ExpoGaodeMapOfflineModule : Module() {
165
184
  synchronized(lock) {
166
185
  downloadingCities.add(cityCode)
167
186
  }
168
- offlineMapManager?.updateOfflineCityByCode(cityCode)
187
+ getOfflineMapManager().updateOfflineCityByCode(cityCode)
169
188
  }
170
189
 
171
190
  AsyncFunction("checkUpdate") { cityCode: String ->
172
- val city = offlineMapManager?.getItemByCityCode(cityCode)
191
+ val city = getOfflineMapManager().getItemByCityCode(cityCode)
173
192
  city?.state == OfflineMapStatus.NEW_VERSION
174
193
  }
175
194
 
176
195
  // ==================== 状态查询 ====================
177
196
 
178
197
  AsyncFunction("isMapDownloaded") { cityCode: String ->
179
- val city = offlineMapManager?.getItemByCityCode(cityCode)
198
+ val city = getOfflineMapManager().getItemByCityCode(cityCode)
180
199
  city?.state == OfflineMapStatus.SUCCESS ||
181
200
  city?.state == OfflineMapStatus.CHECKUPDATES
182
201
  }
183
202
 
184
203
  AsyncFunction("getMapStatus") { cityCode: String ->
185
- val city = offlineMapManager?.getItemByCityCode(cityCode)
204
+ val city = getOfflineMapManager().getItemByCityCode(cityCode)
186
205
  city?.let { convertCityToMap(it) } ?: Bundle()
187
206
  }
188
207
 
189
208
  AsyncFunction("getTotalProgress") {
190
- val downloadedCities = offlineMapManager?.downloadOfflineMapCityList ?: emptyList()
209
+ val downloadedCities = getOfflineMapManager().downloadOfflineMapCityList ?: emptyList()
191
210
  if (downloadedCities.isEmpty()) {
192
211
  0.0
193
212
  } else {
@@ -204,12 +223,12 @@ class ExpoGaodeMapOfflineModule : Module() {
204
223
 
205
224
  AsyncFunction("getStorageSize") {
206
225
  // 计算所有已下载地图的大小
207
- val cities = offlineMapManager?.downloadOfflineMapCityList ?: emptyList()
226
+ val cities = getOfflineMapManager().downloadOfflineMapCityList ?: emptyList()
208
227
  cities.sumOf { it.size }
209
228
  }
210
229
 
211
230
  AsyncFunction("getStorageInfo") {
212
- val cities = offlineMapManager?.downloadOfflineMapCityList ?: emptyList()
231
+ val cities = getOfflineMapManager().downloadOfflineMapCityList ?: emptyList()
213
232
  val offlineMapSize = cities.sumOf { it.size }
214
233
 
215
234
  // 获取存储路径的统计信息
@@ -231,9 +250,9 @@ class ExpoGaodeMapOfflineModule : Module() {
231
250
  }
232
251
 
233
252
  AsyncFunction("clearAllMaps") {
234
- offlineMapManager?.downloadOfflineMapCityList?.forEach { city ->
253
+ getOfflineMapManager().downloadOfflineMapCityList?.forEach { city ->
235
254
  // 使用城市名称删除
236
- offlineMapManager?.remove(city.city)
255
+ getOfflineMapManager().remove(city.city)
237
256
  }
238
257
  synchronized(lock) {
239
258
  downloadingCities.clear()
@@ -260,16 +279,16 @@ class ExpoGaodeMapOfflineModule : Module() {
260
279
  }
261
280
  }
262
281
  cityCodes.forEach { cityCode ->
263
- offlineMapManager?.downloadByCityCode(cityCode)
282
+ getOfflineMapManager().downloadByCityCode(cityCode)
264
283
  }
265
284
  }
266
285
 
267
286
  AsyncFunction("batchDelete") { cityCodes: List<String> ->
268
287
  cityCodes.forEach { cityCode ->
269
- val city = offlineMapManager?.getItemByCityCode(cityCode)
288
+ val city = getOfflineMapManager().getItemByCityCode(cityCode)
270
289
  // 使用城市名称删除,不是城市代码
271
290
  city?.city?.let { cityName ->
272
- offlineMapManager?.remove(cityName)
291
+ getOfflineMapManager().remove(cityName)
273
292
  }
274
293
  }
275
294
  synchronized(lock) {
@@ -287,18 +306,18 @@ class ExpoGaodeMapOfflineModule : Module() {
287
306
  }
288
307
  }
289
308
  cityCodes.forEach { cityCode ->
290
- offlineMapManager?.updateOfflineCityByCode(cityCode)
309
+ getOfflineMapManager().updateOfflineCityByCode(cityCode)
291
310
  }
292
311
  }
293
312
 
294
313
  AsyncFunction("pauseAllDownloads") {
295
314
  // pause() 只暂停正在下载的,不包括队列
296
- offlineMapManager?.pause()
315
+ getOfflineMapManager().pause()
297
316
 
298
317
  synchronized(lock) {
299
318
  pausedCities.addAll(downloadingCities)
300
319
  downloadingCities.forEach { cityCode ->
301
- val city = offlineMapManager?.getItemByCityCode(cityCode)
320
+ val city = getOfflineMapManager().getItemByCityCode(cityCode)
302
321
  if (city != null) {
303
322
  sendEvent("onDownloadPaused", Bundle().apply {
304
323
  putString("cityCode", cityCode)
@@ -321,7 +340,7 @@ class ExpoGaodeMapOfflineModule : Module() {
321
340
  downloadingCities.add(cityCode)
322
341
  pausedCities.remove(cityCode)
323
342
  }
324
- offlineMapManager?.downloadByCityCode(cityCode)
343
+ getOfflineMapManager().downloadByCityCode(cityCode)
325
344
  }
326
345
  }
327
346
  }
@@ -335,9 +354,10 @@ class ExpoGaodeMapOfflineModule : Module() {
335
354
  if (downName == null) return
336
355
 
337
356
  // downName 可能是城市代码或城市名称,尝试两种方式查找
338
- var city = offlineMapManager?.getItemByCityCode(downName)
357
+ val manager = offlineMapManager ?: return
358
+ var city = manager.getItemByCityCode(downName)
339
359
  if (city == null) {
340
- city = offlineMapManager?.offlineMapCityList?.find { it.city == downName }
360
+ city = manager.offlineMapCityList?.find { it.city == downName }
341
361
  }
342
362
 
343
363
  if (city == null) return
@@ -446,9 +466,9 @@ class ExpoGaodeMapOfflineModule : Module() {
446
466
  putString("cityName", city.city)
447
467
  putLong("size", city.size)
448
468
  putString("status", status)
449
- putInt("progress", city.getcompleteCode())
469
+ putInt("progress", getDownloadProgress(city))
450
470
  putString("version", city.version)
451
- putLong("downloadedSize", (city.size * city.getcompleteCode() / 100))
471
+ putLong("downloadedSize", (city.size * getDownloadProgress(city) / 100))
452
472
  }
453
473
  }
454
474
 
@@ -461,12 +481,54 @@ class ExpoGaodeMapOfflineModule : Module() {
461
481
  putString("cityName", province.provinceName)
462
482
  putLong("size", province.size)
463
483
  putString("status", getStatusString(province.state))
464
- putInt("progress", province.getcompleteCode())
484
+ putInt("progress", getDownloadProgress(province))
465
485
  putString("version", province.version)
466
486
  putString("provinceName", province.provinceName)
467
487
  putString("provinceCode", province.provinceCode)
468
488
  }
469
489
  }
490
+
491
+ /**
492
+ * 兼容获取 START_DOWNLOAD_FAILED 状态码
493
+ * 国内版拼写为 START_DOWNLOAD_FAILD,Google Play 版修正为 START_DOWNLOAD_FAILED
494
+ */
495
+ private val startDownloadFailedCode: Int by lazy {
496
+ try {
497
+ OfflineMapStatus::class.java.getField("START_DOWNLOAD_FAILED").getInt(null)
498
+ } catch (e: Exception) {
499
+ try {
500
+ OfflineMapStatus::class.java.getField("START_DOWNLOAD_FAILD").getInt(null)
501
+ } catch (e2: Exception) {
502
+ -1
503
+ }
504
+ }
505
+ }
506
+
507
+ /**
508
+ * 兼容获取下载进度
509
+ * Google Play 版本 SDK 可能修复了 getcompleteCode 的命名或使用了不同的 API
510
+ */
511
+ private fun getDownloadProgress(obj: Any): Int {
512
+ try {
513
+ // 尝试标准版的命名 (getcompleteCode)
514
+ val method = obj.javaClass.getMethod("getcompleteCode")
515
+ return method.invoke(obj) as Int
516
+ } catch (e: Exception) {
517
+ try {
518
+ // 尝试修正后的命名 (getCompleteCode) - Google Play 版本可能使用此命名
519
+ val method = obj.javaClass.getMethod("getCompleteCode")
520
+ return method.invoke(obj) as Int
521
+ } catch (e2: Exception) {
522
+ // 如果都失败了,尝试直接访问 completeCode 字段
523
+ try {
524
+ val field = obj.javaClass.getField("completeCode")
525
+ return field.getInt(obj)
526
+ } catch (e3: Exception) {
527
+ return 0
528
+ }
529
+ }
530
+ }
531
+ }
470
532
 
471
533
  /**
472
534
  * 获取状态字符串
@@ -484,10 +546,10 @@ class ExpoGaodeMapOfflineModule : Module() {
484
546
  OfflineMapStatus.EXCEPTION_NETWORK_LOADING -> "downloading" // 网络问题,可继续
485
547
  OfflineMapStatus.EXCEPTION_AMAP -> "failed" // 认证异常
486
548
  OfflineMapStatus.EXCEPTION_SDCARD -> "failed" // SD卡异常
487
- OfflineMapStatus.START_DOWNLOAD_FAILD -> "failed" // 开始下载失败
549
+ startDownloadFailedCode -> "failed" // 兼容两种拼写的开始下载失败
488
550
  OfflineMapStatus.CHECKUPDATES -> "not_downloaded"
489
551
  OfflineMapStatus.NEW_VERSION -> "not_downloaded"
490
552
  else -> "not_downloaded"
491
553
  }
492
554
  }
493
- }
555
+ }