383 lines
13 KiB
Dart
383 lines
13 KiB
Dart
import 'dart:async';
|
||
import 'dart:math';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter_map/flutter_map.dart';
|
||
import 'package:geolocator/geolocator.dart';
|
||
import 'package:latlong2/latlong.dart';
|
||
import 'package:maibu_satabot_v2/features/home/presentation/widgets/BottomDirectionLine.dart';
|
||
import 'package:maibu_satabot_v2/features/home/presentation/widgets/common/enum.dart';
|
||
import 'package:maibu_satabot_v2/features/home/presentation/widgets/startpoint_area.dart';
|
||
|
||
import '../path_list_pages.dart';
|
||
|
||
class MapPageEnterprise extends StatefulWidget {
|
||
const MapPageEnterprise({Key? key}) : super(key: key);
|
||
|
||
@override
|
||
State<MapPageEnterprise> createState() => _MapPageEnterpriseState();
|
||
}
|
||
|
||
class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||
final MapController _mapController = MapController();
|
||
|
||
LatLng? _currentLatLng;
|
||
StreamSubscription<Position>? _positionSub;
|
||
bool _hasMovedOnce = false;
|
||
bool _isPanelOpen = false; // 控制面板显示/隐藏
|
||
bool _directionBoxOpen = false; //航线面板显示/隐藏
|
||
|
||
RobotMode _currentRobotMode = RobotMode.point; //打点模式
|
||
AreaMode _currentAreaMode = AreaMode.work; //作业区域还是障碍物区域
|
||
WorkMode? _currentWorkMode = WorkMode.bow; // 当前作业模式
|
||
|
||
// ========== 核心新增状态 ==========
|
||
List<LatLng> _markedPoints = []; // 存储所有打点坐标
|
||
LatLng get _mapCenter => _mapController.center; // 实时获取地图中心坐标
|
||
// =================================
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
_initLocationEnterprise();
|
||
// 监听地图移动事件,实时更新连线
|
||
_mapController.mapEventStream.listen((event) {
|
||
if (event is MapEventMove || event is MapEventMoveEnd) {
|
||
setState(() {}); // 触发UI刷新,更新连线
|
||
}
|
||
});
|
||
}
|
||
|
||
/// ===============================
|
||
/// 企业级定位初始化(已修复坐标系)
|
||
/// ===============================
|
||
Future<void> _initLocationEnterprise() async {
|
||
final serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
||
if (!serviceEnabled) return;
|
||
|
||
LocationPermission permission = await Geolocator.checkPermission();
|
||
if (permission == LocationPermission.denied) {
|
||
permission = await Geolocator.requestPermission();
|
||
}
|
||
if (permission != LocationPermission.whileInUse &&
|
||
permission != LocationPermission.always) {
|
||
return;
|
||
}
|
||
|
||
// 1️⃣ 首次强制获取定位
|
||
final position = await Geolocator.getCurrentPosition(
|
||
desiredAccuracy: LocationAccuracy.bestForNavigation,
|
||
);
|
||
|
||
final gcj = wgs84ToGcj02(position.latitude, position.longitude);
|
||
_updateLocation(gcj, moveMap: true);
|
||
|
||
// 2️⃣ 实时监听(不再强制移动地图)
|
||
_positionSub =
|
||
Geolocator.getPositionStream(
|
||
locationSettings: const LocationSettings(
|
||
accuracy: LocationAccuracy.bestForNavigation,
|
||
distanceFilter: 2,
|
||
),
|
||
).listen((pos) {
|
||
final gcj = wgs84ToGcj02(pos.latitude, pos.longitude);
|
||
_updateLocation(gcj, moveMap: false);
|
||
});
|
||
}
|
||
|
||
/// ===============================
|
||
/// 更新位置
|
||
/// ===============================
|
||
void _updateLocation(LatLng latLng, {bool moveMap = false}) {
|
||
setState(() {
|
||
_currentLatLng = latLng;
|
||
});
|
||
|
||
if (moveMap && !_hasMovedOnce) {
|
||
_hasMovedOnce = true;
|
||
_mapController.move(latLng, 17);
|
||
}
|
||
}
|
||
|
||
/// 手动回到当前位置
|
||
void _moveToCurrentLocation() {
|
||
if (_currentLatLng == null) return;
|
||
_mapController.move(_currentLatLng!, 17);
|
||
}
|
||
|
||
// ========== 核心方法:打点逻辑 ==========
|
||
void _addMarkedPoint() {
|
||
setState(() {
|
||
_markedPoints.add(_mapCenter); // 在地图中心打点
|
||
});
|
||
debugPrint(
|
||
'新增打点:第${_markedPoints.length}个点,经纬度:${_mapCenter.latitude.toStringAsFixed(6)}, ${_mapCenter.longitude.toStringAsFixed(6)}',
|
||
);
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
_positionSub?.cancel();
|
||
super.dispose();
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final screenHeight = MediaQuery.of(context).size.height;
|
||
final menuHeight = 16 * 6; // 假设 VerticalFloatMenu 有 6 个选项,每个高度为 56
|
||
final maxTop = screenHeight - menuHeight;
|
||
return Scaffold(
|
||
body: SafeArea(
|
||
child: Stack(
|
||
children: [
|
||
// 地图核心组件
|
||
FlutterMap(
|
||
mapController: _mapController,
|
||
options: MapOptions(
|
||
initialCenter:
|
||
_currentLatLng ?? const LatLng(39.9042, 116.4074),
|
||
initialZoom: 15,
|
||
maxZoom: 18,
|
||
// 禁止地图点击事件(避免和中心标冲突)
|
||
onTap: (_, __) {}, // 空实现,禁用地图点击响应
|
||
),
|
||
children: [
|
||
/// 高德瓦片(GCJ-02)
|
||
TileLayer(
|
||
urlTemplate:
|
||
'https://webrd02.is.autonavi.com/appmaptile'
|
||
'?style=8&x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1'
|
||
'&key=bbb1f0f20eed6bf679eddf2625630aba',
|
||
),
|
||
|
||
/// ========== 新增:中心标与历史打点的虚线连线 ==========
|
||
if (_markedPoints.isNotEmpty)
|
||
PolylineLayer(
|
||
polylines: [
|
||
for (var point in _markedPoints)
|
||
Polyline(
|
||
points: [_mapCenter, point], // 中心标 ↔ 历史打点
|
||
color: Colors.orange.withOpacity(
|
||
0.5,
|
||
), // 降低透明度,模拟虚线视觉效果
|
||
strokeWidth: 1.5, // 调细一点,更接近虚线
|
||
// 去掉 dashArray,直接用实线
|
||
),
|
||
],
|
||
),
|
||
|
||
/// 当前定位 Marker
|
||
if (_currentLatLng != null)
|
||
MarkerLayer(
|
||
markers: [
|
||
Marker(
|
||
point: _currentLatLng!,
|
||
width: 20,
|
||
height: 20,
|
||
child: const Icon(
|
||
Icons.my_location,
|
||
color: Colors.blue,
|
||
size: 30,
|
||
),
|
||
),
|
||
],
|
||
),
|
||
|
||
/// ========== 新增:历史打点的绿色标记 ==========
|
||
MarkerLayer(
|
||
markers: _markedPoints.asMap().entries.map((entry) {
|
||
int index = entry.key + 1; // 打点序号(从1开始)
|
||
LatLng point = entry.value;
|
||
|
||
return Marker(
|
||
point: point,
|
||
width: 80,
|
||
height: 40,
|
||
child: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: [
|
||
// 绿色打点标记
|
||
Container(
|
||
width: 16,
|
||
height: 16,
|
||
decoration: const BoxDecoration(
|
||
color: Color(0xFF00C853), // 绿色主题色
|
||
shape: BoxShape.circle,
|
||
boxShadow: [
|
||
BoxShadow(color: Colors.black12, blurRadius: 2),
|
||
],
|
||
),
|
||
child: Center(
|
||
child: Text(
|
||
'$index',
|
||
style: const TextStyle(
|
||
color: Colors.white,
|
||
fontSize: 10,
|
||
fontWeight: FontWeight.bold,
|
||
),
|
||
),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}).toList(),
|
||
),
|
||
],
|
||
),
|
||
|
||
/// ========== 核心:地图正中心固定定位标 ==========
|
||
const Positioned(
|
||
left: 0,
|
||
right: 0,
|
||
top: 0,
|
||
bottom: 0,
|
||
child: Center(
|
||
child: Icon(
|
||
Icons.location_pin, // 定位标图标
|
||
color: Colors.red, // 红色醒目
|
||
size: 30,
|
||
shadows: [BoxShadow(color: Colors.white, blurRadius: 2)],
|
||
),
|
||
),
|
||
),
|
||
|
||
// 右侧悬浮菜单
|
||
Positioned(
|
||
height: 336,
|
||
right: 16,
|
||
top: maxTop > 10 ? 11 : maxTop,
|
||
child: FloatingActionButton(
|
||
onPressed: _moveToCurrentLocation,
|
||
child: new VerticalFloatMenu(
|
||
onEditTap: (bool isOpen) {
|
||
setState(() {
|
||
_isPanelOpen = isOpen;
|
||
});
|
||
},
|
||
onWorkModeSelected: (mode) {
|
||
_currentWorkMode = mode;
|
||
debugPrint('外部收到作业模式:$mode');
|
||
},
|
||
),
|
||
),
|
||
),
|
||
|
||
// 底部操作面板
|
||
if (_isPanelOpen)
|
||
Positioned(
|
||
left: 0,
|
||
right: 0,
|
||
bottom: 0,
|
||
child: BottomOperationPanel(
|
||
initialRobotMode: RobotMode.point,
|
||
initialAreaMode: AreaMode.work,
|
||
onComplete: () {
|
||
setState(() => _isPanelOpen = false);
|
||
},
|
||
onRobotModeChanged: (mode) {
|
||
setState(() => _currentRobotMode = mode);
|
||
debugPrint('外部收到机器人模式:$mode');
|
||
},
|
||
onAreaModeChanged: (mode) {
|
||
setState(() => _currentAreaMode = mode);
|
||
debugPrint('外部收到区域模式:$mode');
|
||
},
|
||
// ========== 绑定加号打点事件 ==========
|
||
onAddTap: () {
|
||
_addMarkedPoint(); // 点击加号打点
|
||
debugPrint('外部处理加号按钮点击,已添加打点');
|
||
},
|
||
onSettingTap: () {
|
||
setState(() {
|
||
_directionBoxOpen = true;
|
||
});
|
||
debugPrint('外部处理设置按钮点击');
|
||
},
|
||
onLandTap: () {
|
||
debugPrint('外部处理地块标签点击');
|
||
},
|
||
onRouteTap: () {
|
||
debugPrint('外部处理航线标签点击');
|
||
},
|
||
),
|
||
),
|
||
|
||
// 航线方向面板
|
||
if (_directionBoxOpen)
|
||
Positioned(
|
||
left: 0,
|
||
right: 0,
|
||
bottom: 0,
|
||
child: RouteDirectionPanel(
|
||
initialOptimalHeading: true,
|
||
initialDirection: 0.0,
|
||
onConfirm: (result) {
|
||
debugPrint(
|
||
'最优航向:${result['optimalHeading']},角度:${result['direction']}',
|
||
);
|
||
setState(() => _directionBoxOpen = false);
|
||
},
|
||
onCancel: () {
|
||
setState(() => _directionBoxOpen = false);
|
||
},
|
||
),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
/// =======================================================
|
||
/// 坐标转换:WGS84 -> GCJ02(国内高德/腾讯通用)
|
||
/// =======================================================
|
||
const double _pi = 3.14159265358979324;
|
||
const double _a = 6378245.0;
|
||
const double _ee = 0.00669342162296594323;
|
||
|
||
LatLng wgs84ToGcj02(double lat, double lon) {
|
||
if (_outOfChina(lat, lon)) {
|
||
return LatLng(lat, lon);
|
||
}
|
||
double dLat = _transformLat(lon - 105.0, lat - 35.0);
|
||
double dLon = _transformLon(lon - 105.0, lat - 35.0);
|
||
double radLat = lat / 180.0 * _pi;
|
||
double magic = sin(radLat);
|
||
magic = 1 - _ee * magic * magic;
|
||
double sqrtMagic = sqrt(magic);
|
||
dLat = (dLat * 180.0) / ((_a * (1 - _ee)) / (magic * sqrtMagic) * _pi);
|
||
dLon = (dLon * 180.0) / (_a / sqrtMagic * cos(radLat) * _pi);
|
||
double mgLat = lat + dLat;
|
||
double mgLon = lon + dLon;
|
||
return LatLng(mgLat, mgLon);
|
||
}
|
||
|
||
bool _outOfChina(double lat, double lon) {
|
||
return lon < 72.004 || lon > 137.8347 || lat < 0.8293 || lat > 55.8271;
|
||
}
|
||
|
||
double _transformLat(double x, double y) {
|
||
double ret =
|
||
-100.0 +
|
||
2.0 * x +
|
||
3.0 * y +
|
||
0.2 * y * y +
|
||
0.1 * x * y +
|
||
0.2 * sqrt(x.abs());
|
||
ret += (20.0 * sin(6.0 * x * _pi) + 20.0 * sin(2.0 * x * _pi)) * 2.0 / 3.0;
|
||
ret += (20.0 * sin(y * _pi) + 40.0 * sin(y / 3.0 * _pi)) * 2.0 / 3.0;
|
||
ret += (160.0 * sin(y / 12.0 * _pi) + 320 * sin(y * _pi / 30.0)) * 2.0 / 3.0;
|
||
return ret;
|
||
}
|
||
|
||
double _transformLon(double x, double y) {
|
||
double ret =
|
||
300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(x.abs());
|
||
ret += (20.0 * sin(6.0 * x * _pi) + 20.0 * sin(2.0 * x * _pi)) * 2.0 / 3.0;
|
||
ret += (20.0 * sin(x * _pi) + 40.0 * sin(x / 3.0 * _pi)) * 2.0 / 3.0;
|
||
ret +=
|
||
(150.0 * sin(x / 12.0 * _pi) + 300.0 * sin(x / 30.0 * _pi)) * 2.0 / 3.0;
|
||
return ret;
|
||
}
|