作业行距和航线方向实时改变 生成路径完成
This commit is contained in:
@@ -168,7 +168,7 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
bounds,
|
||||
options: FitBoundsOptions(
|
||||
padding: const EdgeInsets.all(50), // 边缘留白(避免内容贴边)
|
||||
maxZoom: 18, // 最大缩放级别(防止过度放大)
|
||||
//maxZoom: 18, // 最大缩放级别(防止过度放大)
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1236,6 +1236,100 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
}).toList();
|
||||
}
|
||||
|
||||
// ========== 抽象:生成路径的核心函数 ==========
|
||||
Future<void> _generatePath({bool showTips = true}) async {
|
||||
// 1. 校验必要条件(至少需要3个打点才能形成闭合区域)
|
||||
if (_markedPoints.isEmpty) {
|
||||
if (showTips) {
|
||||
// 仅在手动点击完成时显示提示,实时调用时静默
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('请先添加作业区域打点!'), backgroundColor: Colors.red));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (_markedPoints.length < 3) {
|
||||
if (showTips) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('作业区域至少需要3个打点才能生成路径!'), backgroundColor: Colors.red));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (_currentWorkMode == null) {
|
||||
if (showTips) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('请先选择作业模式!'), backgroundColor: Colors.red));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 2. 转换页面数据为 generatePath 所需的参数格式
|
||||
// 2.1 参考点(取第一个打点,转换为 WGS84)
|
||||
final firstPoint = _markedPoints.first;
|
||||
final wgs84FirstPoint = gcj02ToWgs84(firstPoint.latitude, firstPoint.longitude);
|
||||
final referencePoint = work_area_model.ReferencePoint(lat: wgs84FirstPoint.latitude, lon: wgs84FirstPoint.longitude);
|
||||
|
||||
// 2.2 航向角(转换为整型,-1表示最优航向则取-1)
|
||||
debugPrint('当前航向角: $_angle');
|
||||
final heading = _angle == -1 ? -1 : _angle.toInt();
|
||||
|
||||
// 2.3 外边界(转换为 OuterBoundary 格式,所有点转 WGS84)
|
||||
final outerBoundary = work_area_model.OuterBoundary(
|
||||
// 将 LatLng 打点转换为 Position 列表(先转 WGS84)
|
||||
position: _markedPoints.map((latLng) {
|
||||
// 关键:每个点都转换为 WGS84 坐标系
|
||||
final wgs84Point = gcj02ToWgs84(latLng.latitude, latLng.longitude);
|
||||
return work_area_model.Position(lat: wgs84Point.latitude, lon: wgs84Point.longitude);
|
||||
}).toList(),
|
||||
sideWidth: _workDistance,
|
||||
);
|
||||
|
||||
final holes = <work_area_model.HoleBoundary>[];
|
||||
final workType = _currentWorkMode == WorkMode.bow ? 0 : 2; // 弓字模式0,自定义2
|
||||
|
||||
// 3. 调用 Cubit 的 generatePath 方法生成路径
|
||||
await context.read<DevicesCubit>().generatePath(reference: referencePoint, heading: heading, outer: outerBoundary, holes: holes, workType: workType);
|
||||
|
||||
// 4. 获取生成结果并处理
|
||||
final cubitState = context.read<DevicesCubit>().state;
|
||||
print('${cubitState.generatedPath},路径生成结果');
|
||||
|
||||
if (cubitState.errorMessage != null) {
|
||||
// 生成失败(仅手动调用时显示提示)
|
||||
if (showTips) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('路径生成失败:${cubitState.errorMessage}'), backgroundColor: Colors.red));
|
||||
}
|
||||
} else if (cubitState.generatedPath != null) {
|
||||
// 生成成功(手动调用显示提示,实时调用静默)
|
||||
if (showTips) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('路径生成成功!'), backgroundColor: Colors.green));
|
||||
}
|
||||
debugPrint('生成的路径数据:${cubitState.generatedPath}');
|
||||
|
||||
// 转换路径坐标(WGS84 → GCJ02)并更新UI
|
||||
if (cubitState.generatedPath is List) {
|
||||
setState(() {
|
||||
final pathList = cubitState.generatedPath as List;
|
||||
typedPathList = pathList.whereType<work_area_model.DeviceAddPathPointModel>().toList();
|
||||
|
||||
gcjPathPoints = typedPathList.map((point) {
|
||||
double wgs84Lat = point.latitude;
|
||||
double wgs84Lon = point.longitude;
|
||||
final gcjPoint = wgs84ToGcj02(wgs84Lat, wgs84Lon);
|
||||
return LatLng(gcjPoint.latitude, gcjPoint.longitude);
|
||||
}).toList();
|
||||
|
||||
gcjOuterPoints = List.from(_markedPoints);
|
||||
});
|
||||
moveMapToPointsCenter(gcjPathPoints);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// 异常处理(仅手动调用时显示提示)
|
||||
debugPrint('路径生成异常:$e');
|
||||
if (showTips) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('路径生成异常:${e.toString().substring(0, 50)}'), backgroundColor: Colors.red));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final screenHeight = MediaQuery.of(context).size.height;
|
||||
@@ -1515,98 +1609,11 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
'操作完成回调:当前机器人模式=$_currentRobotMode,当前区域模式=$_currentAreaMode,当前作业模式:$_currentWorkMode,作业区域点:$_markedPoints,作业行距=$_workDistance,航线方向角=$_angle',
|
||||
);
|
||||
|
||||
// 1. 校验必要条件(至少需要3个打点才能形成闭合区域)
|
||||
if (_markedPoints.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('请先添加作业区域打点!'), backgroundColor: Colors.red));
|
||||
return;
|
||||
}
|
||||
if (_markedPoints.length < 3) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('作业区域至少需要3个打点才能生成路径!'), backgroundColor: Colors.red));
|
||||
return;
|
||||
}
|
||||
if (_currentWorkMode == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('请先选择作业模式!'), backgroundColor: Colors.red));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 2. 转换页面数据为 generatePath 所需的参数格式
|
||||
// 2.1 参考点(取第一个打点,转换为 WGS84)
|
||||
final firstPoint = _markedPoints.first;
|
||||
final wgs84FirstPoint = gcj02ToWgs84(firstPoint.latitude, firstPoint.longitude);
|
||||
final referencePoint = work_area_model.ReferencePoint(lat: wgs84FirstPoint.latitude, lon: wgs84FirstPoint.longitude);
|
||||
|
||||
// 2.2 航向角(转换为整型,-1表示最优航向则取-1)
|
||||
final heading = _angle == -1 ? -1 : _angle.toInt();
|
||||
|
||||
// 2.3 外边界(转换为 OuterBoundary 格式,所有点转 WGS84)
|
||||
final outerBoundary = work_area_model.OuterBoundary(
|
||||
// 将 LatLng 打点转换为 Position 列表(先转 WGS84)
|
||||
position: _markedPoints.map((latLng) {
|
||||
// 关键:每个点都转换为 WGS84 坐标系
|
||||
final wgs84Point = gcj02ToWgs84(latLng.latitude, latLng.longitude);
|
||||
return work_area_model.Position(lat: wgs84Point.latitude, lon: wgs84Point.longitude);
|
||||
}).toList(),
|
||||
sideWidth: _workDistance,
|
||||
);
|
||||
|
||||
final holes = <work_area_model.HoleBoundary>[];
|
||||
|
||||
final workType = _currentWorkMode == WorkMode.bow ? 0 : 2; // 注意:这里弓字模式是0,自定义是2(确认和后端一致)
|
||||
|
||||
// 3. 调用 Cubit 的 generatePath 方法生成路径
|
||||
await context.read<DevicesCubit>().generatePath(
|
||||
reference: referencePoint,
|
||||
heading: heading,
|
||||
outer: outerBoundary,
|
||||
holes: holes,
|
||||
workType: workType,
|
||||
);
|
||||
|
||||
// 4. 获取生成结果并处理
|
||||
final cubitState = context.read<DevicesCubit>().state;
|
||||
print('${cubitState.generatedPath},路径生成结果');
|
||||
if (cubitState.errorMessage != null) {
|
||||
// 生成失败
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('路径生成失败:${cubitState.errorMessage}'), backgroundColor: Colors.red));
|
||||
} else if (cubitState.generatedPath != null) {
|
||||
// 生成成功
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('路径生成成功!'), backgroundColor: Colors.green));
|
||||
debugPrint('生成的路径数据:${cubitState.generatedPath}');
|
||||
|
||||
// 可选:将生成的路径转换为地图可显示的坐标(WGS84 → GCJ02)
|
||||
if (cubitState.generatedPath is List) {
|
||||
setState(() {
|
||||
// 先将 List<dynamic> 转换为 List<DeviceAddPathPointModel>
|
||||
final pathList = cubitState.generatedPath as List;
|
||||
typedPathList = pathList.whereType<work_area_model.DeviceAddPathPointModel>().toList();
|
||||
|
||||
// 然后使用 .lat 和 .lon 属性访问
|
||||
gcjPathPoints = typedPathList.map((point) {
|
||||
double wgs84Lat = point.latitude;
|
||||
double wgs84Lon = point.longitude;
|
||||
final gcjPoint = wgs84ToGcj02(wgs84Lat, wgs84Lon);
|
||||
return LatLng(gcjPoint.latitude, gcjPoint.longitude);
|
||||
}).toList();
|
||||
|
||||
gcjOuterPoints = List.from(_markedPoints); // 生成新列表,复制所有元素 // ========== 核心修改:清空临时打点列表 ==========
|
||||
//_markedPoints.clear(); // 清空后,临时划线会自动消失
|
||||
});
|
||||
moveMapToPointsCenter(gcjPathPoints);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// 异常处理
|
||||
debugPrint('路径生成异常:$e');
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text('路径生成异常:${e.toString().substring(0, 50)}'), backgroundColor: Colors.red));
|
||||
}
|
||||
|
||||
// 调用抽象后的生成路径函数(showTips=true:手动点击时显示提示)
|
||||
await _generatePath(showTips: true);
|
||||
// 5. 关闭操作面板
|
||||
setState(() {
|
||||
_isPanelOpen = false; // 关闭操作面板
|
||||
_currentWorkMode = null; // 清空作业模式,清除弓字模式的绿色半透明区域
|
||||
//_isPanelOpen = false; // 关闭操作面板
|
||||
});
|
||||
},
|
||||
onUndoTap: _undoLastPoint,
|
||||
@@ -1626,6 +1633,9 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
onDistanceTap: (distance) {
|
||||
_workDistance = distance;
|
||||
debugPrint('外部处理作业行距距离设置,当前距离:${distance.toStringAsFixed(1)}米');
|
||||
if (_markedPoints.length >= 3 && _currentWorkMode != null) {
|
||||
_generatePath(showTips: false);
|
||||
}
|
||||
},
|
||||
onSettingTap: () {
|
||||
setState(() {
|
||||
@@ -1651,11 +1661,15 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
child: RouteDirectionPanel(
|
||||
initialOptimalHeading: true,
|
||||
initialDirection: 0.0,
|
||||
onConfirm: (result) {
|
||||
onValueChanged: (result) {
|
||||
debugPrint('最优航向:${result['optimalHeading']},角度:${result['direction']}');
|
||||
_angle = result['optimalHeading'] == true ? -1 : result['direction'];
|
||||
|
||||
debugPrint('外部处理航线方向设置,当前角度:${_angle}');
|
||||
setState(() => _directionBoxOpen = false);
|
||||
if (_markedPoints.length >= 3 && _currentWorkMode != null) {
|
||||
debugPrint('外部处理航线方向设置11222:${_angle}');
|
||||
_generatePath(showTips: false);
|
||||
}
|
||||
},
|
||||
onCancel: () {
|
||||
setState(() => _directionBoxOpen = false);
|
||||
|
||||
Reference in New Issue
Block a user