admin
2021-11-10 ef43946e123a2e7d4c616e0dc02c89970b71d900
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
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';
 
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 drawLine(List<BMFCoordinate> points, Color lineColor,
      BMFMapController? _mapController,
      {int width = 16}) {
    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);
    _mapController?.addPolyline(colorsPolyline);
  }
 
  //获取地图的显示参数
  static Future<MapShowInfo> getMapShowParams(
      List<BMFCoordinate> points) async {
    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();
  }
}