admin
2022-01-12 8327000a0cce5e47226372e0e25c1e6faec497e7
lib/ui/map/map.dart
@@ -1,29 +1,29 @@
import 'dart:async';
import 'dart:io';
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart';
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';
import 'package:locations/api/http.dart';
import 'package:locations/model/map/location_model.dart';
import 'package:locations/ui/map/location_search.dart';
import 'package:locations/model/map/map_model.dart';
import 'package:locations/ui/widget/capture.dart';
import 'package:locations/ui/widget/map_marker.dart';
import 'package:locations/utils/global.dart';
import 'package:locations/utils/event_bus_util.dart';
import 'package:locations/utils/location_util.dart';
import 'package:locations/utils/map_util.dart';
import 'package:locations/utils/pageutils.dart';
import 'package:locations/utils/map_js_util.dart';
import 'package:locations/utils/string_util.dart';
import 'package:locations/utils/ui_constant.dart';
import 'package:locations/utils/ui_utils.dart';
import 'package:locations/utils/user_util.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:event_bus/event_bus.dart';
//控件阴影
List<BoxShadow> getViewShadow() {
  return [
    BoxShadow(
    const BoxShadow(
      blurRadius: 6.5,
      spreadRadius: 1,
      color: Color(0x4D0E96FF),
@@ -56,14 +56,7 @@
  _MapPageState(this.title, {this.share = false, this.uid, this.location});
  BMFMapController? _mapController;
  BMFMapOptions mapOptions = BMFMapOptions(
      showMapScaleBar: false,
      mapType: BMFMapType.Standard,
      center: Global.currentPosition ?? BMFCoordinate(39.917215, 116.380341),
      mapScaleBarPosition: BMFPoint(0, 200),
      zoomLevel: 12,
      mapPadding: BMFEdgeInsets(left: 30, top: 0, right: 30, bottom: 0));
  WebViewController? _webViewController;
  //选择地图类型索引(0-2D 1-3D 2-卫星)
  int selectMapTypeIndex = 0;
@@ -82,13 +75,13 @@
  SimpleLocation? _currentTargetPosition;
  BMFMarker? myMarker;
  Marker? myMarker;
  BMFMarker? targetUserMarker;
  Marker? targetUserMarker;
  BMFMarker? myAddressMarker;
  Marker? myAddressMarker;
  BMFMarker? targetAddressUserMarker;
  Marker? targetAddressUserMarker;
  //是否显示地址信息
  bool _showAddress = true;
@@ -101,6 +94,8 @@
  Timer? _timer;
  //
  final CaptureController _captureMyController = CaptureController();
  final CaptureController _captureTargetController = CaptureController();
@@ -109,7 +104,21 @@
  double? pixelRatio;
  var eventBusMapClick;
  void _init() {
    if (location != null) {
      MapUtil.setCenter(_webViewController, location!);
    }
    eventBusMapClick = eventBus.on<MapClickEventBus>().listen((event) {
      //测距
      if (_mesure) {
        _measureManager!.addPoint(event.location!).then((value) {
          _showDistance(value);
        });
      }
    });
    //定位
    LocationUtil.startLocation(0, (state, map) {
      if (state == LocationState.success) {
@@ -126,12 +135,10 @@
        _showTargetLocationMarker();
        //设置地图中心点与缩放等级
        if (_currentTargetPosition != null && _currentMyPosition != null) {
          MapUtil.getMapShowParams([
            _currentTargetPosition!.toBMFCoordinate(),
            _currentMyPosition!.toBMFCoordinate()
          ]).then((value) {
            _mapController!.updateMapOptions(BMFMapOptions(
                zoomLevel: value.zoomLevel, center: value.center));
          MapUtil.getMapShowParams(
              [_currentTargetPosition!, _currentMyPosition!]).then((value) {
            MapUtil.setCenter(_webViewController, value.center);
            MapUtil.setZoom(_webViewController, value.zoomLevel);
          });
        }
      });
@@ -149,8 +156,7 @@
          });
          if (travelRECIng) {
            _travelRECManager!.addLocation(BMFCoordinate(
                value.location!.latitude!, value.location!.longitude!));
            _travelRECManager!.addLocation(value.location!);
          }
          //设置现在的位置
@@ -165,31 +171,40 @@
    if (_currentTargetPosition != null) {
      //画头像
      if (targetUserMarker != null) {
        targetUserMarker!
            .updatePosition(_currentTargetPosition!.toBMFCoordinate());
        MapMarkerUtil.update(_webViewController, targetUserMarker!.Id,
            position: _currentTargetPosition);
      } else {
        String value = await _captureTargetController.capturePng();
        if (StringUtil.isNullOrEmpty(value)) {
          return;
        }
        targetUserMarker = await MapUtil.addMarker(
            _mapController, _currentTargetPosition!.toBMFCoordinate(), value);
            _webViewController, _currentTargetPosition!, value,
            width: 40, height: 40);
      }
      //画地址
      if (targetAddressUserMarker != null) {
        targetAddressUserMarker!
            .updatePosition(_currentTargetPosition!.toBMFCoordinate());
        MapMarkerUtil.update(_webViewController, targetAddressUserMarker!.Id,
            position: _currentTargetPosition);
        String value = await _captureTargetAddressController.capturePng();
        await targetAddressUserMarker!.updateIcon(value);
        await MapMarkerUtil.update(
            _webViewController, targetAddressUserMarker!.Id,
            icon: value);
      } else {
        String value = await _captureTargetAddressController.capturePng();
        if (StringUtil.isNullOrEmpty(value)) {
          return;
        }
        targetAddressUserMarker = await MapUtil.addMarker(
            _mapController, _currentTargetPosition!.toBMFCoordinate(), value,
            offset: BMFPoint(0, 2 * 64));
            _webViewController, _currentTargetPosition!, value,
            offset: Offset(0, 36 + 2), width: 132, height: 36);
      }
      //只有一个人
      if (myMarker == null) {
        MapUtil.setCenter(_webViewController, _currentTargetPosition!);
      }
    }
  }
@@ -204,23 +219,27 @@
    if (_currentMyPosition != null) {
      //画头像
      if (myMarker != null) {
        myMarker!.updatePosition(_currentMyPosition!.toBMFCoordinate());
        MapMarkerUtil.update(_webViewController, myMarker!.Id,
            position: _currentMyPosition);
      } else {
        String value = await _captureMyController.capturePng();
        myMarker = await MapUtil.addMarker(
            _mapController, _currentMyPosition!.toBMFCoordinate(), value);
            _webViewController, _currentMyPosition!, value,
            width: 40, height: 40);
      }
      //画地址
      if (myAddressMarker != null) {
        myAddressMarker!.updatePosition(_currentMyPosition!.toBMFCoordinate());
        MapMarkerUtil.update(_webViewController, myAddressMarker!.Id,
            position: _currentMyPosition);
        String value = await _captureMyAddressController.capturePng();
        myAddressMarker!.updateIcon(value);
        MapMarkerUtil.update(_webViewController, myAddressMarker!.Id,
            icon: value);
      } else {
        String value = await _captureMyAddressController.capturePng();
        myAddressMarker = await MapUtil.addMarker(
            _mapController, _currentMyPosition!.toBMFCoordinate(), value,
            offset: BMFPoint(0, 64 * 2));
            _webViewController, _currentMyPosition!, value,
            offset: Offset(0, 36 + 2), width: 132, height: 36);
      }
    }
  }
@@ -228,16 +247,18 @@
  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) {
      WebView.platform = SurfaceAndroidWebView();
    }
    _currentTargetPosition = location;
  }
  @override
  void dispose() {
    super.dispose();
    if (null != subscription) {
      subscription!.cancel();
    if (eventBusMapClick != null) {
      eventBusMapClick.cancel();
    }
    LocationUtil.stopLocation();
    if (_timer != null) {
      _timer!.cancel();
    }
@@ -259,47 +280,45 @@
    }
  }
  WebView? _webView;
  //获取地图视图
  Widget getMapView() {
    return Container(
      child: BMFMapWidget(
        onBMFMapCreated: (controller) {
          _mapController = controller;
          _travelRECManager = TravelRECManager(_mapController);
          _measureManager = MeasureManager(_mapController);
    _webView ??= getMapWebView(context, (controller) {
      _webViewController = controller;
          _init();
      //TODO 测量模式
          _mapController?.setMapOnClickedMapPoiCallback(
              callback: (BMFMapPoi poi) {
            //是否为测量模式
            if (!_mesure) {
              return;
            }
            _measureManager!.addPoint(poi.pt!).then((value) {
              _showDistance(value);
            });
          });
          _mapController?.setMapOnClickedMapBlankCallback(
              callback: (BMFCoordinate coordinate) {
            //是否为测量模式
            if (!_mesure) {
              return;
            }
            print("地图点击:${coordinate.toString()}");
            _measureManager!.addPoint(coordinate).then((value) {
              _showDistance(value);
            });
          });
        },
        mapOptions: mapOptions,
      ),
    );
      // _webViewController?.setMapOnClickedMapPoiCallback(
      //     callback: (BMFMapPoi poi) {
      //       //是否为测量模式
      //       if (!_mesure) {
      //         return;
      //       }
      //       _measureManager!.addPoint(poi.pt!).then((value) {
      //         _showDistance(value);
      //       });
      //     });
      // _webViewController?.setMapOnClickedMapBlankCallback(
      //     callback: (BMFCoordinate coordinate) {
      //       //是否为测量模式
      //       if (!_mesure) {
      //         return;
      //       }
      //
      //       print("地图点击:${coordinate.toString()}");
      //
      //       _measureManager!.addPoint(coordinate).then((value) {
      //         _showDistance(value);
      //       });
      //     });
    }, pageFinish: (url) {
      _travelRECManager = TravelRECManager(_webViewController);
      _measureManager = MeasureManager(_webViewController);
      _init();
    });
    return _webView!;
  }
  StreamSubscription<Map<String, Object>?>? subscription;
  @override
  Widget build(BuildContext context) {
@@ -560,58 +579,6 @@
        ]));
  }
  Widget getMapTypeView(String name, String imgPath, int index) {
    return InkWell(
        onTap: () {
          setState(() {
            selectMapTypeIndex = index;
          });
          if (index == 2) {
            _mapController?.updateMapOptions(
                BMFMapOptions(mapType: BMFMapType.Satellite));
          } else if (index == 1) {
            _mapController?.updateMapOptions(BMFMapOptions(
                mapType: BMFMapType.Standard,
                buildingsEnabled: true,
                baseIndoorMapEnabled: true,
                overlookEnabled: true));
            _mapController?.showBaseIndoorMap(true);
          } else if (index == 0) {
            _mapController?.updateMapOptions(BMFMapOptions(
                mapType: BMFMapType.Standard,
                buildingsEnabled: false,
                baseIndoorMapEnabled: false));
          }
        },
        child: Column(
          children: [
            ClipRRect(
                borderRadius: BorderRadius.circular(23),
                child: Container(
                  alignment: Alignment.center,
                  color: selectMapTypeIndex == index
                      ? ColorConstant.theme
                      : Color(0xFF9DAAB3),
                  width: 71,
                  height: 71,
                  child: Image.asset(
                    imgPath,
                    width: 65,
                    height: 65,
                  ),
                )),
            Text(
              name,
              style: TextStyle(
                  fontSize: 14,
                  color: index == selectMapTypeIndex
                      ? ColorConstant.theme
                      : Color(0xFF9DAAB3)),
            )
          ],
        ));
  }
  Widget getContentView() {
    //------------工具栏-----------
    return share
@@ -620,7 +587,7 @@
                right: 10,
                top: 10,
                child: Container(
                  padding: EdgeInsets.only(top: 13, bottom: 13),
                  padding: const EdgeInsets.only(top: 13, bottom: 13),
                  width: 43,
                  decoration: BoxDecoration(
                      color: Colors.white,
@@ -635,8 +602,8 @@
                              return;
                            }
                            _travelRECManager!.startREC(
                                _currentTargetPosition!.toBMFCoordinate());
                            _travelRECManager!
                                .startREC(_currentTargetPosition!);
                          } else {
                            //结束录制
                            _travelRECManager!.stopREC();
@@ -672,12 +639,15 @@
                      InkWell(
                        onTap: () {
                          if (myAddressMarker != null) {
                            myAddressMarker!.updateVisible(!_showAddress);
                            MapMarkerUtil.update(
                                _webViewController, myAddressMarker!.Id,
                                visibility: !_showAddress);
                          }
                          if (targetAddressUserMarker != null) {
                            targetAddressUserMarker!
                                .updateVisible(!_showAddress);
                            MapMarkerUtil.update(
                                _webViewController, targetAddressUserMarker!.Id,
                                visibility: !_showAddress);
                          }
                          setState(() {