点击某一项需要吧轨迹会知道地图上,初步

This commit is contained in:
mmc
2026-02-28 14:02:04 +08:00
parent d547ed2480
commit 560a4d6aaf

View File

@@ -81,6 +81,9 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
RobotMode _currentRobotMode = RobotMode.point; //打点模式
AreaMode _currentAreaMode = AreaMode.work; //作业区域还是障碍物区域
WorkMode? _currentWorkMode = WorkMode.bow; // 当前作业模式
// 新增:存储转换后的path和outer坐标
List<LatLng> gcjPathPoints = [];
List<LatLng> gcjOuterPoints = [];
// ========== 核心新增状态 ==========
final List<LatLng> _markedPoints = []; // 存储所有打点坐标
@@ -485,11 +488,9 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
);
}
// 注意:方法名改为 _buildWorkPanel(因为实际是作业面板,不是列表,避免混淆)
Widget _buildWorkPanel() {
return BlocBuilder<DevicesCubit, DevicesState>(
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<MapPageEnterprise> {
.cast<Map<String, dynamic>>()
.toList();
// 第二步:只有列表非空时才赋值第一条数据
if (pathRecords.isNotEmpty) {
_selectedPlotPath = PlotDataPath(jsonData: pathRecords.first);
// ========== 核心新增:解析并转换坐标 ==========
try {
// 1. 解析嵌套的jsonData
final String nestedJsonStr =
_selectedPlotPath!.jsonData['jsonData'] as String;
final Map<String, dynamic> nestedJson = jsonDecode(nestedJsonStr);
// 2. 提取path和outer原始坐标
final List<dynamic> rawPath = nestedJson['path'] as List<dynamic>;
final List<dynamic> rawOuter =
nestedJson['outer'] as List<dynamic>;
// 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<MapPageEnterprise> {
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<MapPageEnterprise> {
'?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<MapPageEnterprise> {
}
}
// 转换单个WGS84坐标到GCJ02
LatLng convertWGS84ToGCJ02(double lat, double lon) {
return wgs84ToGcj02(lat, lon);
}
// 批量转换path坐标列表(path是lat+lon格式)
List<LatLng> convertPathToGCJ02(List<dynamic> 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<LatLng> convertOuterToGCJ02(List<dynamic> 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(国内高德/腾讯通用)
/// =======================================================