| | |
| | | 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 |
| | |
| | | |
| | | 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); |
| | | {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; |
| | |
| | | _mapController!.removeMarker(marker); |
| | | } |
| | | |
| | | static removeMarkers(BMFMapController? _mapController, List<BMFMarker> markers) { |
| | | static removeMarkers( |
| | | BMFMapController? _mapController, List<BMFMarker> markers) { |
| | | _mapController!.removeMarkers(markers); |
| | | } |
| | | |
| | |
| | | _mapController!.cleanAllMarkers(); |
| | | } |
| | | } |
| | | |
| | | ///路径录制管理 |
| | | /// |
| | | 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(); |
| | | } |
| | | } |