admin
2021-11-27 3465ac6e980f1473e4f42ba3eaafc7815423efec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import 'dart:ui';
 
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart';
import 'package:flutter_baidu_mapapi_utils/flutter_baidu_mapapi_utils.dart';
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';
import 'package:locations/model/map/map_model.dart';
import 'package:locations/ui/widget/map_marker.dart';
import 'package:locations/utils/ui_constant.dart';
 
///地图工具类
class MapUtil {
  ///画线
  ///https://lbs.baidu.com/index.php?title=flutter/loc/render-map/ployline
  ///https://mapopen-pub-androidsdk.bj.bcebos.com/map/flutter/docs/utils_api_1.2/flutter_baidu_mapapi_utils/flutter_baidu_mapapi_utils-library.html
  static Future drawLine(List<BMFCoordinate> points, Color lineColor,
      BMFMapController? _mapController,
      {int width = 16}) async {
    List<Color> colors = [lineColor];
    List<int> indexs = List.filled(points.length - 1, 0);
 
    /// 创建polyline
    BMFPolyline colorsPolyline = BMFPolyline(
        coordinates: points,
        indexs: indexs,
        colors: colors,
        width: width,
        dottedLine: false,
        lineDashType: BMFLineDashType.LineDashTypeNone,
        lineCapType: BMFLineCapType.LineCapRound,
        lineJoinType: BMFLineJoinType.LineJoinRound);
    await _mapController?.addPolyline(colorsPolyline);
  }
 
  //获取地图的显示参数
  static Future<MapShowInfo> getMapShowParams(
      List<BMFCoordinate> points) async {
    if (points.length == 1) {
      return MapShowInfo(points[0], 18);
    } else {
      List<double?> outSides = _getOutSidePoint(points);
      BMFCoordinate center = BMFCoordinate(
          (outSides[0]! + outSides[1]!) / 2, (outSides[2]! + outSides[3]!) / 2);
      //留1/20的边界
      int zoom =
          await _getZoom(outSides[0], outSides[1], outSides[2], outSides[3]);
      return MapShowInfo(center, zoom);
    }
  }
 
  //
 
  ///获取地图的中心点
  static List<double?> _getOutSidePoint(List<BMFCoordinate> points) {
    double? maxLat = points[0].latitude;
    double? minLat = points[0].latitude;
    double? maxLng = points[0].longitude;
    double? minLng = points[0].longitude;
 
    //获取东南西北四个点
    points.forEach((element) {
      if (maxLat! < element.latitude) {
        maxLat = element.latitude;
      }
      if (minLat! > element.latitude) {
        minLat = element.latitude;
      }
 
      if (maxLng! < element.longitude) {
        maxLng = element.longitude;
      }
 
      if (minLng! > element.longitude) {
        minLng = element.longitude;
      }
    });
 
    return [maxLat, minLat, maxLng, minLng];
  }
 
  static Future<int> _getZoom(
      double? maxLat, double? minLat, double? maxLng, double? minLng) async {
    List<double> zoom = [
      50,
      100,
      200,
      500,
      1000,
      2000,
      5000,
      10000,
      20000,
      25000,
      50000,
      100000,
      200000,
      500000,
      1000000,
      2000000
    ]; //级别18到3。
    var pointA = BMFCoordinate(maxLat!, maxLng!); // 创建点坐标A
    var pointB = BMFCoordinate(minLat!, minLng!); // 创建点坐标B
    //留1/20的边界
    double? distance =
        await BMFCalculateUtils.getLocationDistance(pointA, pointB);
    distance = distance! / 10 + distance;
    for (var i = 0, zoomLen = zoom.length; i < zoomLen; i++) {
      if (zoom[i] - distance > 0) {
        return 18 - i + 3; //之所以会多3,是因为地图范围常常是比例尺距离的10倍以上。所以级别会增加3。
      }
    }
    return 18;
  }
 
  static Future<BMFMarker> addMarker(
      BMFMapController? _mapController, BMFCoordinate position, String icon,
      {String identifier = "flutter_marker",
      int zIndex = 0,
      BMFPoint? offset}) async {
    BMFMarker marker = BMFMarker(
        position: position,
        identifier: identifier,
        icon: icon,
        zIndex: zIndex,
        centerOffset: offset);
 
    /// 添加Marker
    await _mapController?.addMarker(marker);
    return marker;
  }
 
  static removeMarker(BMFMapController? _mapController, BMFMarker marker) {
    _mapController!.removeMarker(marker);
  }
 
  static removeMarkers(
      BMFMapController? _mapController, List<BMFMarker> markers) {
    _mapController!.removeMarkers(markers);
  }
 
  static cleanAllMarkers(BMFMapController? _mapController) {
    _mapController!.cleanAllMarkers();
  }
 
  static Future<double?> getDistance(
      BMFCoordinate pointA, BMFCoordinate pointB) async {
    return await BMFCalculateUtils.getLocationDistance(pointA, pointB);
  }
}
 
///路径录制管理
///
class TravelRECManager {
  BMFMapController? mapController;
  List<BMFCoordinate> positionList = [];
  List<BMFMarker> startMarkers = [];
 
  TravelRECManager(this.mapController);
 
  //画起始点
  _addStartMarker(BMFCoordinate position) {}
 
  //画路径
  _drawLine(BMFCoordinate start, BMFCoordinate end) {
    MapUtil.drawLine([start, end], ColorConstant.theme, mapController);
  }
 
  //开始录制
  startREC(BMFCoordinate currentPosition) {
    positionList.clear();
    positionList.add(currentPosition);
 
    //画起始点
    MapMarkerUtil.addTravelStartMarker(mapController, currentPosition!)
        .then((value) {
      startMarkers = value;
    });
  }
 
  //添加位置
  addLocation(BMFCoordinate lication) async {
    if (positionList.length < 1) {
      return;
    }
    //计算距离,如果距离在10米内就不添加到数组
    double? distance = await BMFCalculateUtils.getLocationDistance(
        positionList[positionList.length - 1], lication);
    if (distance != null && distance >= 10) {
      //上传位置
      positionList.add(lication);
      _drawLine(positionList[positionList.length - 2],
          positionList[positionList.length - 1]);
    }
  }
 
  //停止录制
  stopREC() {
    //清除数据
    positionList.clear();
    //删除线
    mapController!.cleanAllMarkers();
  }
}