空洞逻辑
This commit is contained in:
@@ -100,6 +100,11 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
List<LatLng>? _currentTracePoints; //收到机器的点坐标
|
||||
List typedPathList = <work_area_model.DeviceAddPathPointModel>[]; // 存储生成路径的坐标列表(已转换为LatLng)
|
||||
|
||||
// ========== 障碍物模式核心状态 ==========
|
||||
List<List<LatLng>> _obstacleHoles = []; // 存储多组障碍物打点(嵌套数组:每组是一个空洞)
|
||||
List<LatLng> _currentObstaclePoints = []; // 当前正在绘制的障碍物打点
|
||||
bool _isObstacleEditing = false; // 是否处于障碍物编辑状态
|
||||
|
||||
//打点
|
||||
double _workDistance = 0.0; // 作业行距(单位:米)
|
||||
double _angle = 0.0; // 航线方向角(单位:度)
|
||||
@@ -531,6 +536,9 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
_directionBoxOpen = false;
|
||||
_isListBoxOpen = false;
|
||||
_isWorkPanelOpen = false;
|
||||
_obstacleHoles.clear();
|
||||
_currentObstaclePoints.clear();
|
||||
_isObstacleEditing = false;
|
||||
|
||||
// 重置选中状态
|
||||
_selectedPlot = null;
|
||||
@@ -606,10 +614,59 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
_mapController.move(_currentLatLng!, 17);
|
||||
}
|
||||
|
||||
// ========== 障碍物模式:完成当前障碍物绘制 ==========
|
||||
void _completeObstacle() {
|
||||
if (_currentObstaclePoints.length < 3) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('障碍物区域至少需要3个打点!'), backgroundColor: Colors.red));
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_obstacleHoles.add(List.from(_currentObstaclePoints)); // 保存当前障碍物组
|
||||
_currentObstaclePoints.clear(); // 清空当前绘制的障碍物点
|
||||
_isObstacleEditing = false; // 退出编辑状态
|
||||
});
|
||||
|
||||
debugPrint('完成障碍物绘制,当前障碍物组数:${_obstacleHoles.length}');
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('已添加第${_obstacleHoles.length}个障碍物区域'), backgroundColor: Colors.green));
|
||||
}
|
||||
|
||||
void _undoObstaclePoint() {
|
||||
if (_currentObstaclePoints.isNotEmpty) {
|
||||
setState(() => _currentObstaclePoints.removeLast());
|
||||
debugPrint('撤回障碍物打点,剩余:${_currentObstaclePoints.length}个');
|
||||
} else if (_obstacleHoles.isNotEmpty) {
|
||||
// 无当前绘制点时,删除最后一组障碍物
|
||||
setState(() => _obstacleHoles.removeLast());
|
||||
debugPrint('删除最后一组障碍物,剩余组数:${_obstacleHoles.length}');
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('暂无障碍物打点可撤回')));
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 障碍物模式:清空所有障碍物 ==========
|
||||
void _clearAllObstacles() {
|
||||
setState(() {
|
||||
_obstacleHoles.clear();
|
||||
_currentObstaclePoints.clear();
|
||||
_isObstacleEditing = false;
|
||||
});
|
||||
debugPrint('已清空所有障碍物区域');
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('已清空所有障碍物区域'), backgroundColor: Colors.amber));
|
||||
}
|
||||
|
||||
// ========== 核心方法:打点逻辑 ==========
|
||||
void _addMarkedPoint() {
|
||||
setState(() {
|
||||
_markedPoints.add(_mapCenter); // 在地图中心打点
|
||||
if (_currentAreaMode == AreaMode.work) {
|
||||
_markedPoints.add(_mapCenter);
|
||||
debugPrint('新增作业区域打点:第${_markedPoints.length}个点,经纬度:${_mapCenter.latitude}, ${_mapCenter.longitude}');
|
||||
} else {
|
||||
// 障碍物模式:添加到当前障碍物打点
|
||||
_currentObstaclePoints.add(_mapCenter);
|
||||
_isObstacleEditing = true; // 标记进入障碍物编辑状态
|
||||
debugPrint('新增障碍物打点:第${_currentObstaclePoints.length}个点,经纬度:${_mapCenter.latitude}, ${_mapCenter.longitude}');
|
||||
}
|
||||
});
|
||||
debugPrint('$_markedPoints,新增打点:第${_markedPoints.length}个点,经纬度:${_mapCenter.latitude.toStringAsFixed(20)}, ${_mapCenter.longitude.toStringAsFixed(20)}');
|
||||
}
|
||||
@@ -1240,15 +1297,14 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
|
||||
// ========== 抽象:生成路径的核心函数 ==========
|
||||
Future<void> _generatePath({bool showTips = true}) async {
|
||||
// 1. 校验必要条件(至少需要3个打点才能形成闭合区域)
|
||||
if (_markedPoints.isEmpty) {
|
||||
// 1. 校验必要条件
|
||||
if (_currentAreaMode == AreaMode.work && _markedPoints.isEmpty) {
|
||||
if (showTips) {
|
||||
// 仅在手动点击完成时显示提示,实时调用时静默
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('请先添加作业区域打点!'), backgroundColor: Colors.red));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (_markedPoints.length < 3) {
|
||||
if (_currentAreaMode == AreaMode.work && _markedPoints.length < 3) {
|
||||
if (showTips) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('作业区域至少需要3个打点才能生成路径!'), backgroundColor: Colors.red));
|
||||
}
|
||||
@@ -1263,53 +1319,82 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
|
||||
try {
|
||||
// 2. 转换页面数据为 generatePath 所需的参数格式
|
||||
// 2.1 参考点(取第一个打点,转换为 WGS84)
|
||||
// 2.1 参考点(取作业区域第一个打点)
|
||||
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');
|
||||
// 2.2 航向角
|
||||
final heading = _angle == -1 ? -1 : _angle.toInt();
|
||||
|
||||
// 2.3 外边界(转换为 OuterBoundary 格式,所有点转 WGS84)
|
||||
// 2.3 外边界(作业区域)
|
||||
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,
|
||||
);
|
||||
|
||||
// ========== 核心新增:转换障碍物为holes参数 ==========
|
||||
final holes = <work_area_model.HoleBoundary>[];
|
||||
final workType = _currentWorkMode == WorkMode.bow ? 0 : 2; // 弓字模式0,自定义2
|
||||
if (_obstacleHoles.isNotEmpty) {
|
||||
for (var holePoints in _obstacleHoles) {
|
||||
// 转换每组障碍物点为WGS84坐标系
|
||||
final holePosition = holePoints.map((latLng) {
|
||||
final wgs84Point = gcj02ToWgs84(latLng.latitude, latLng.longitude);
|
||||
return work_area_model.Position(lat: wgs84Point.latitude, lon: wgs84Point.longitude);
|
||||
}).toList();
|
||||
|
||||
// 3. 调用 Cubit 的 generatePath 方法生成路径
|
||||
await context.read<DevicesCubit>().generatePath(reference: referencePoint, heading: heading, outer: outerBoundary, holes: holes, workType: workType);
|
||||
// 添加到holes数组(sideWidth固定为0)
|
||||
holes.add(work_area_model.HoleBoundary(position: holePosition, sideWidth: 0.0));
|
||||
}
|
||||
}
|
||||
try {
|
||||
// 将holes转换为普通Map,便于JSON序列化
|
||||
final holesJson = holes.map((hole) {
|
||||
return {
|
||||
'sideWidth': hole.sideWidth,
|
||||
'position': hole.position.map((p) => {'lat': p.lat, 'lon': p.lon}).toList(),
|
||||
};
|
||||
}).toList();
|
||||
|
||||
// 4. 获取生成结果并处理
|
||||
// 格式化打印JSON(带缩进,更易读)
|
||||
debugPrint('holes参数(JSON格式):\n${const JsonEncoder.withIndent(' ').convert(holesJson)}');
|
||||
} catch (e) {
|
||||
debugPrint('holes转JSON失败:$e');
|
||||
}
|
||||
final workType = _currentWorkMode == WorkMode.bow ? 0 : 2;
|
||||
|
||||
// 3. 调用 Cubit 的 generatePath 方法(传入holes参数)
|
||||
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();
|
||||
debugPrint('✅ 过滤并转换后的路径点列表:typedPathList 长度=${typedPathList.length}');
|
||||
//打印
|
||||
|
||||
gcjPathPoints = typedPathList.map((point) {
|
||||
double wgs84Lat = point.latitude;
|
||||
@@ -1319,13 +1404,12 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
}).toList();
|
||||
|
||||
gcjOuterPoints = List.from(_markedPoints);
|
||||
_markedPoints.clear();
|
||||
//_markedPoints.clear();
|
||||
});
|
||||
moveMapToPointsCenter(gcjPathPoints);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// 异常处理(仅手动调用时显示提示)
|
||||
debugPrint('路径生成异常:$e');
|
||||
if (showTips) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('路径生成异常:${e.toString().substring(0, 50)}'), backgroundColor: Colors.red));
|
||||
@@ -1432,6 +1516,99 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
),
|
||||
],
|
||||
),
|
||||
if (_obstacleHoles.isNotEmpty)
|
||||
PolygonLayer(
|
||||
polygons: _obstacleHoles.map((holePoints) {
|
||||
return Polygon(
|
||||
points: holePoints,
|
||||
color: Colors.red.withOpacity(0.2), // 红色半透明填充
|
||||
borderColor: Colors.red, // 红色边框
|
||||
borderStrokeWidth: 1.5,
|
||||
isFilled: true,
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
|
||||
// 2. 正在绘制的障碍物打点连线(红色虚线)
|
||||
if (_isObstacleEditing && _currentObstaclePoints.isNotEmpty)
|
||||
PolylineLayer(
|
||||
polylines: [
|
||||
// 已绘制的障碍物点连线
|
||||
for (int i = 0; i < _currentObstaclePoints.length - 1; i++)
|
||||
Polyline(
|
||||
points: [_currentObstaclePoints[i], _currentObstaclePoints[i + 1]],
|
||||
color: Colors.red.withOpacity(0.8),
|
||||
strokeWidth: 1.5,
|
||||
isDotted: true, // 虚线区分作业区域
|
||||
),
|
||||
// 最后一个点到地图中心的连线
|
||||
Polyline(points: [_mapCenter, _currentObstaclePoints.last], color: Colors.red.withOpacity(0.5), strokeWidth: 1.5, isDotted: true),
|
||||
],
|
||||
),
|
||||
|
||||
// 3. 障碍物打点标记(红色)
|
||||
if (_currentAreaMode == AreaMode.obstacle)
|
||||
MarkerLayer(
|
||||
markers: [
|
||||
// 已完成的障碍物打点
|
||||
for (var holeIndex = 0; holeIndex < _obstacleHoles.length; holeIndex++)
|
||||
for (var pointIndex = 0; pointIndex < _obstacleHoles[holeIndex].length; pointIndex++)
|
||||
Marker(
|
||||
point: _obstacleHoles[holeIndex][pointIndex],
|
||||
width: 80,
|
||||
height: 40,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const SizedBox(height: 8),
|
||||
Container(
|
||||
width: 16,
|
||||
height: 16,
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.red, // 红色标记区分作业区域
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 2)],
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'${holeIndex + 1}-${pointIndex + 1}', // 格式:组号-点号
|
||||
style: const TextStyle(color: Colors.white, fontSize: 8, fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// 正在绘制的障碍物打点
|
||||
for (var pointIndex = 0; pointIndex < _currentObstaclePoints.length; pointIndex++)
|
||||
Marker(
|
||||
point: _currentObstaclePoints[pointIndex],
|
||||
width: 80,
|
||||
height: 40,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const SizedBox(height: 8),
|
||||
Container(
|
||||
width: 16,
|
||||
height: 16,
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.orange, // 橙色标记区分正在绘制
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 2)],
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'${pointIndex + 1}',
|
||||
style: const TextStyle(color: Colors.white, fontSize: 10, fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
/// 历史打点的绿色标记
|
||||
MarkerLayer(
|
||||
@@ -1611,16 +1788,35 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
debugPrint(
|
||||
'操作完成回调:当前机器人模式=$_currentRobotMode,当前区域模式=$_currentAreaMode,当前作业模式:$_currentWorkMode,作业区域点:$_markedPoints,作业行距=$_workDistance,航线方向角=$_angle',
|
||||
);
|
||||
|
||||
// 调用抽象后的生成路径函数(showTips=true:手动点击时显示提示)
|
||||
await _generatePath(showTips: true);
|
||||
if (_currentAreaMode == AreaMode.obstacle) {
|
||||
// 障碍物模式:完成当前障碍物绘制
|
||||
_completeObstacle();
|
||||
await _generatePath(showTips: true);
|
||||
} else {
|
||||
// 作业区域模式:生成路径
|
||||
await _generatePath(showTips: true);
|
||||
}
|
||||
// 5. 关闭操作面板
|
||||
setState(() {
|
||||
//_isPanelOpen = false; // 关闭操作面板
|
||||
});
|
||||
},
|
||||
onUndoTap: _undoLastPoint,
|
||||
onDeleteTap: _deleteAllPoints,
|
||||
onUndoTap: () {
|
||||
// 区分模式:作业区域撤回/障碍物撤回
|
||||
if (_currentAreaMode == AreaMode.work) {
|
||||
_undoLastPoint();
|
||||
} else {
|
||||
_undoObstaclePoint();
|
||||
}
|
||||
},
|
||||
onDeleteTap: () {
|
||||
// 区分模式:作业区域清空/障碍物清空
|
||||
if (_currentAreaMode == AreaMode.work) {
|
||||
_deleteAllPoints();
|
||||
} else {
|
||||
_clearAllObstacles();
|
||||
}
|
||||
},
|
||||
onRobotModeChanged: (mode) {
|
||||
setState(() => _currentRobotMode = mode);
|
||||
debugPrint('外部收到机器人模式:$mode');
|
||||
|
||||
Reference in New Issue
Block a user