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();
|
}
|
}
|