Explorar el Código

feat: 添加百度地图坐标转高德地图坐标功能

- 在AMapLoader中添加AMap.ConvertFrom插件
- 新增convertBaiduToGaode方法,批量转换百度地图坐标为高德地图坐标
- 优化转换逻辑,每次最多转换100个坐标,避免请求过于频繁
- 在地图初始化完成后自动执行坐标转换,确保数据准确性
sequoia tungfang hace 1 mes
padre
commit
5dd360ac24
Se han modificado 1 ficheros con 62 adiciones y 2 borrados
  1. 62 2
      src/components/TongzhouTrafficMap.vue

+ 62 - 2
src/components/TongzhouTrafficMap.vue

@@ -168,6 +168,61 @@ export default {
       }
     },
 
+    // 将百度地图坐标转换为高德地图坐标
+    async convertBaiduToGaode() {
+      if (!this.AMap || !this.AMap.convertFrom) {
+        console.warn('AMap.ConvertFrom 插件未加载,跳过坐标转换');
+        return;
+      }
+
+      console.log('开始转换百度地图坐标为高德地图坐标...');
+      
+      // 批量转换,每次最多100个坐标
+      const batchSize = 100;
+      const total = this.intersectionData.length;
+      
+      for (let i = 0; i < total; i += batchSize) {
+        const batch = this.intersectionData.slice(i, i + batchSize);
+        const baiduCoords = batch.map(item => [
+          item["位置-经度"],
+          item["位置-纬度"]
+        ]);
+
+        try {
+          const result = await new Promise((resolve, reject) => {
+            this.AMap.convertFrom(baiduCoords, 'baidu', function(status, result) {
+              if (status === 'complete') {
+                resolve(result);
+              } else {
+                reject(new Error('坐标转换失败: ' + JSON.stringify(result)));
+              }
+            });
+          });
+
+          // 更新转换后的坐标
+          if (result.locations) {
+            result.locations.forEach((location, index) => {
+              if (batch[index]) {
+                batch[index]["位置-经度"] = location.lng;
+                batch[index]["位置-纬度"] = location.lat;
+              }
+            });
+          }
+
+          console.log(`已转换 ${Math.min(i + batchSize, total)} / ${total} 个坐标`);
+        } catch (error) {
+          console.error('坐标转换失败:', error);
+        }
+
+        // 避免请求过于频繁
+        if (i + batchSize < total) {
+          await new Promise(resolve => setTimeout(resolve, 100));
+        }
+      }
+
+      console.log('坐标转换完成');
+    },
+
     // 检查地图环境是否安全可用
     isMapReady() {
       return !this._isDestroyed && this.map && typeof this.map.add === 'function';
@@ -242,7 +297,7 @@ export default {
         const AMap = await AMapLoader.load({
           key: this.amapKey,
           version: "2.0",
-          plugins: ['AMap.Driving']
+          plugins: ['AMap.Driving', 'AMap.ConvertFrom']
         });
 
         // 异步回来后,首先检查组件是否还在
@@ -256,7 +311,12 @@ export default {
         });
 
         this.map.on('complete', () => {
-          if (!this._isDestroyed) this.drawStaticRoutes();
+          if (!this._isDestroyed) {
+            // 先转换坐标,再绘制路线
+            this.convertBaiduToGaode().then(() => {
+              this.drawStaticRoutes();
+            });
+          }
         });
       } catch (err) {
         console.error('地图加载失败:', err);