diff --git a/lib/features/home/presentation/widgets/map/testmap_pages.dart b/lib/features/home/presentation/widgets/map/testmap_pages.dart index 3d5fa492..b91ad62c 100644 --- a/lib/features/home/presentation/widgets/map/testmap_pages.dart +++ b/lib/features/home/presentation/widgets/map/testmap_pages.dart @@ -81,6 +81,9 @@ class _MapPageEnterpriseState extends State { RobotMode _currentRobotMode = RobotMode.point; //打点模式 AreaMode _currentAreaMode = AreaMode.work; //作业区域还是障碍物区域 WorkMode? _currentWorkMode = WorkMode.bow; // 当前作业模式 + // 新增:存储转换后的path和outer坐标 + List gcjPathPoints = []; + List gcjOuterPoints = []; // ========== 核心新增状态 ========== final List _markedPoints = []; // 存储所有打点坐标 @@ -485,11 +488,9 @@ class _MapPageEnterpriseState extends State { ); } - // 注意:方法名改为 _buildWorkPanel(因为实际是作业面板,不是列表,避免混淆) Widget _buildWorkPanel() { return BlocBuilder( builder: (context, state) { - print('当前DevicesState的pathData: ${state}'); // 调试输出,查看pathData内容 // 无选中地块或无路径数据时返回空 if (_selectedPlot == null || state.pathData == null) { return const SizedBox.shrink(); @@ -505,20 +506,47 @@ class _MapPageEnterpriseState extends State { .cast>() .toList(); - // 第二步:只有列表非空时才赋值第一条数据 if (pathRecords.isNotEmpty) { _selectedPlotPath = PlotDataPath(jsonData: pathRecords.first); + // ========== 核心新增:解析并转换坐标 ========== + try { + // 1. 解析嵌套的jsonData + final String nestedJsonStr = + _selectedPlotPath!.jsonData['jsonData'] as String; + final Map nestedJson = jsonDecode(nestedJsonStr); + + // 2. 提取path和outer原始坐标 + final List rawPath = nestedJson['path'] as List; + final List rawOuter = + nestedJson['outer'] as List; + + // 3. 转换为GCJ02坐标 + gcjPathPoints = convertPathToGCJ02(rawPath); + gcjOuterPoints = convertOuterToGCJ02(rawOuter); + //setState(() {}); + + print('转换后的path坐标数量:${gcjPathPoints.length}'); + print('转换后的outer坐标数量:${gcjOuterPoints.length}'); + } catch (e) { + print('解析/转换坐标失败:$e'); + gcjPathPoints = []; + gcjOuterPoints = []; + } } else { _selectedPlotPath = null; // 空列表时清空,避免残留旧数据 + gcjPathPoints = []; + gcjOuterPoints = []; } } else { _selectedPlotPath = null; // 非List类型时清空 + gcjPathPoints = []; + gcjOuterPoints = []; } - print( - _selectedPlotPath == null - ? '无数据' - : 'path坐标: ${jsonDecode(_selectedPlotPath!.jsonData['jsonData'] as String)['planModel']}', - ); + //print( + // _selectedPlotPath == null + // ? '无数据' + // : 'path坐标: ${jsonDecode(_selectedPlotPath!.jsonData['jsonData'] as String)}', + //); String workMode = jsonDecode( _selectedPlotPath!.jsonData['jsonData'] as String, @@ -526,6 +554,14 @@ class _MapPageEnterpriseState extends State { WorkMode.bow.value ? "弓字模式" : "自定义模式"; + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted && _mapController.mapEventStream != null) { + setState(() { + // 触发地图重绘,确保坐标生效 + }); + } + }); + return Positioned( left: 0, right: 0, @@ -751,6 +787,33 @@ class _MapPageEnterpriseState extends State { '?style=8&x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1' '&key=bbb1f0f20eed6bf679eddf2625630aba', ), + if (gcjPathPoints.isNotEmpty) // 仅当有数据时绘制 + PolylineLayer( + polylines: [ + Polyline( + points: gcjPathPoints, // 转换后的GCJ02坐标 + color: Colors.blueAccent, // 折线颜色(可自定义) + strokeWidth: 3.0, // 折线宽度 + isDotted: false, // 非虚线(Line模式) + borderColor: Colors.white, // 可选:添加白色描边,提升辨识度 + borderStrokeWidth: 0.5, + ), + ], + ), + + // ========== 新增:绘制outer边框(Polygon模式) ========== + if (gcjOuterPoints.isNotEmpty) // 仅当有数据时绘制 + PolygonLayer( + polygons: [ + Polygon( + points: gcjOuterPoints, // 转换后的GCJ02坐标 + color: Colors.green.withOpacity(0.1), // 内部填充色(透明) + borderColor: Colors.green, // 边框颜色 + borderStrokeWidth: 2.0, // 边框宽度 + isFilled: true, // 开启填充(即使透明,也需要开启才能显示边框) + ), + ], + ), /// ========== 新增:中心标与历史打点的虚线连线 ========== PolylineLayer( @@ -1204,6 +1267,29 @@ class _MapPageEnterpriseState extends State { } } +// 转换单个WGS84坐标到GCJ02 +LatLng convertWGS84ToGCJ02(double lat, double lon) { + return wgs84ToGcj02(lat, lon); +} + +// 批量转换path坐标列表(path是lat+lon格式) +List convertPathToGCJ02(List pathList) { + return pathList.map((item) { + double lat = item['lat'] as double; + double lon = item['lon'] as double; + return convertWGS84ToGCJ02(lat, lon); + }).toList(); +} + +// 批量转换outer坐标列表(outer是lng+lat格式,注意顺序) +List convertOuterToGCJ02(List outerList) { + return outerList.map((item) { + double lon = item['lng'] as double; + double lat = item['lat'] as double; + return convertWGS84ToGCJ02(lat, lon); // 注意lat在前,lon=lon + }).toList(); +} + /// ======================================================= /// 坐标转换:WGS84 -> GCJ02(国内高德/腾讯通用) /// =======================================================