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