admin
2021-12-03 f092e392700f68cdbfc545c9801f530d19fd39fa
lib/utils/map_util.dart
@@ -1,5 +1,6 @@
import 'dart:ui';
import 'package:flutter/material.dart';
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';
@@ -12,7 +13,7 @@
  ///画线
  ///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,
  static Future<String> drawLine(List<BMFCoordinate> points, Color lineColor,
      BMFMapController? _mapController,
      {int width = 16}) async {
    List<Color> colors = [lineColor];
@@ -29,6 +30,11 @@
        lineCapType: BMFLineCapType.LineCapRound,
        lineJoinType: BMFLineJoinType.LineJoinRound);
    await _mapController?.addPolyline(colorsPolyline);
    return colorsPolyline.toMap()["id"].toString();
  }
  static Future removeLine(BMFMapController? _mapController, String id) async {
    await _mapController?.removeOverlay(id);
  }
  //获取地图的显示参数
@@ -132,9 +138,9 @@
    _mapController!.removeMarker(marker);
  }
  static removeMarkers(
      BMFMapController? _mapController, List<BMFMarker> markers) {
    _mapController!.removeMarkers(markers);
  static Future<bool> removeMarkers(
      BMFMapController? _mapController, List<BMFMarker> markers) async {
    return await _mapController!.removeMarkers(markers);
  }
  static cleanAllMarkers(BMFMapController? _mapController) {
@@ -145,6 +151,14 @@
      BMFCoordinate pointA, BMFCoordinate pointB) async {
    return await BMFCalculateUtils.getLocationDistance(pointA, pointB);
  }
  static String getDistanceDesc(int distance) {
    if (distance >= 1000) {
      return "${(distance / 1000).toStringAsFixed(1)}米";
    } else {
      return "$distance米";
    }
  }
}
///路径录制管理
@@ -154,14 +168,18 @@
  List<BMFCoordinate> positionList = [];
  List<BMFMarker> startMarkers = [];
  List<String> lineIds = [];
  TravelRECManager(this.mapController);
  //画起始点
  _addStartMarker(BMFCoordinate position) {}
  //画路径
  _drawLine(BMFCoordinate start, BMFCoordinate end) {
    MapUtil.drawLine([start, end], ColorConstant.theme, mapController);
  _drawLine(BMFCoordinate start, BMFCoordinate end) async {
    String lineId = await MapUtil.drawLine(
        [start, end], ColorConstant.theme, mapController);
    lineIds.add(lineId);
  }
  //开始录制
@@ -170,7 +188,7 @@
    positionList.add(currentPosition);
    //画起始点
    MapMarkerUtil.addTravelStartMarker(mapController, currentPosition!)
    MapMarkerUtil.addTravelStartMarker(mapController, currentPosition)
        .then((value) {
      startMarkers = value;
    });
@@ -178,7 +196,7 @@
  //添加位置
  addLocation(BMFCoordinate lication) async {
    if (positionList.length < 1) {
    if (positionList.isEmpty) {
      return;
    }
    //计算距离,如果距离在10米内就不添加到数组
@@ -193,10 +211,109 @@
  }
  //停止录制
  stopREC() {
  stopREC() async {
    //删除marker
    await MapUtil.removeMarkers(mapController, startMarkers);
    //删除线
    lineIds.forEach((element) {
      MapUtil.removeLine(mapController, element);
    });
    //清除数据
    positionList.clear();
    //删除线
    mapController!.cleanAllMarkers();
  }
}
//测距
class MeasureManager {
  BMFMapController? mapController;
  List<BMFCoordinate> positionList = [];
  List<BMFMarker> markers = [];
  String? lineId;
  final List<String> POINTS = [
    "assets/images/map/icon_map_meaure_node_1.png",
    "assets/images/map/icon_map_meaure_node_2.png",
    "assets/images/map/icon_map_meaure_node_3.png"
  ];
  MeasureManager(this.mapController);
  //清除所有覆盖物
  clear() async {
    positionList.clear();
    if (markers.isNotEmpty) {
      await MapUtil.removeMarkers(mapController, markers);
    }
    if (lineId != null) {
      await MapUtil.removeLine(mapController, lineId!);
      lineId = null;
    }
    markers.clear();
  }
  Future<double> _recycle() async {
    print("_recycle");
    //删除覆盖物
    if (markers.isNotEmpty) {
      await MapUtil.removeMarkers(mapController, markers);
    }
    if (lineId != null) {
      try {
        MapUtil.removeLine(mapController, lineId!);
      } catch (e) {}
      lineId = null;
    }
    //添加覆盖物
    if (positionList.length > 1) {
      //绘制线
      lineId = await MapUtil.drawLine(
          positionList, ColorConstant.theme, mapController,
          width: 8);
      print("lineID:$lineId");
    }
    //绘制marker
    await mapController?.addMarkers(markers);
    //计算距离
    double distance = 0;
    if (positionList.length > 1) {
      for (var i = 1; i < positionList.length; i++) {
        distance +=
            (await MapUtil.getDistance(positionList[i - 1], positionList[i]))!;
      }
    }
    return distance;
  }
  //添加位置
  Future<double> addPoint(BMFCoordinate location) async {
    print("addPoint");
    int markerIndex = positionList.length % 3;
    //添加点
    positionList.add(location);
    BMFMarker marker = BMFMarker(
        position: location,
        icon: POINTS[markerIndex],
        zIndex: 2,
        centerOffset: BMFPoint(15, 15));
    markers.add(marker);
    return await _recycle();
  }
  //回退位置
  Future<double> backPoint() async {
    if (positionList.isEmpty || markers.isEmpty) {
      return 0;
    }
    positionList.removeAt(positionList.length - 1);
    if (markers.isNotEmpty) {
      await MapUtil.removeMarker(mapController, markers[markers.length - 1]);
      markers.removeAt(markers.length - 1);
    }
    return await _recycle();
  }
}