打点添加距离

This commit is contained in:
mmc
2026-03-11 16:41:03 +08:00
parent 21bef7d86a
commit 89de0bc384

View File

@@ -1256,9 +1256,33 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
],
),
if (_markedPoints.isNotEmpty && !_isWorkAreaCompleted && _currentRobotMode == RobotMode.point)
PolylineLayer(
polylines: [
Polyline(points: [_mapCenter, _markedPoints.last], color: Colors.blue.withOpacity(0.5), strokeWidth: 1.5),
Stack(
// 核心:替换Column为Stack
children: [
// 1. 蓝色连线
PolylineLayer(
polylines: [
Polyline(points: [_mapCenter, _markedPoints.last], color: Colors.blue.withOpacity(0.5), strokeWidth: 1.5),
],
),
// 2. 距离文本Marker
MarkerLayer(
markers: [
Marker(
point: _getMiddleLatLng(_mapCenter, _markedPoints.last),
width: 100,
height: 30,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
child: Text(
_calculateLatLngDistance(_mapCenter, _markedPoints.last),
style: const TextStyle(color: Colors.red, fontSize: 12, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
),
),
],
),
],
),
@@ -2528,3 +2552,35 @@ double _transformLon(double x, double y) {
ret += (150.0 * sin(x / 12.0 * _pi) + 300.0 * sin(x / 30.0 * _pi)) * 2.0 / 3.0;
return ret;
}
/// 计算两个经纬度点之间的地面距离(单位:米,GCJ02/WGS84通用)
String _calculateLatLngDistance(LatLng point1, LatLng point2) {
const double earthRadius = 6371000; // 地球半径(米)
// 经纬度转弧度
double lat1Rad = point1.latitude * pi / 180;
double lat2Rad = point2.latitude * pi / 180;
double lng1Rad = point1.longitude * pi / 180;
double lng2Rad = point2.longitude * pi / 180;
// 哈维正弦公式(Haversine)计算地球表面两点实际距离
double dLat = lat2Rad - lat1Rad;
double dLng = lng2Rad - lng1Rad;
double a = sin(dLat / 2) * sin(dLat / 2) + cos(lat1Rad) * cos(lat2Rad) * sin(dLng / 2) * sin(dLng / 2);
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
double distance = earthRadius * c;
// 格式化显示:短距离显示米,长距离显示千米
if (distance < 1000) {
return '${distance.toStringAsFixed(1)} 米';
} else {
return '${(distance / 1000).toStringAsFixed(2)} 千米';
}
}
/// 获取两个坐标点的中点坐标(用于显示距离文本)
LatLng _getMiddleLatLng(LatLng point1, LatLng point2) {
double middleLat = (point1.latitude + point2.latitude) / 2;
double middleLng = (point1.longitude + point2.longitude) / 2;
return LatLng(middleLat, middleLng);
}