admin
2022-01-12 8327000a0cce5e47226372e0e25c1e6faec497e7
lib/ui/main/location.dart
@@ -5,11 +5,10 @@
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/model/map/location_user_model.dart';
import 'package:locations/model/map/map_model.dart';
import 'package:locations/model/user/user_info.dart';
import 'package:locations/ui/map/location_search.dart';
import 'package:locations/ui/map/map.dart';
@@ -24,19 +23,20 @@
import 'package:locations/utils/event_bus_util.dart';
import 'package:locations/utils/global.dart';
import 'package:locations/utils/location_util.dart';
import 'package:locations/utils/map_util.dart';
import 'package:locations/utils/map_js_util.dart';
import 'package:locations/utils/pageutils.dart';
import 'package:locations/utils/permission_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:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:date_format/date_format.dart';
typedef OnPositionHiidenChange = void Function(bool hidden);
BMFMapController? _mapController;
WebViewController? _webViewController;
class LocationPage extends StatefulWidget {
  LocationPage({Key? key, required this.title}) : super(key: key);
@@ -61,8 +61,8 @@
    if (uid == null) {
      return;
    }
    Map<String, dynamic>? result =
    await LocationApiUtil.getInviteLocation(uid);
    print("getLocationInvite");
    Map<String, dynamic>? result = await LocationApiUtil.getInviteLocation(uid);
    if (result!["code"] == 0) {
      LocationUserModel model = LocationUserModel.fromJson(result["data"]);
      DialogUtil.showDialog(
@@ -91,23 +91,18 @@
class _LocationPageState extends State<LocationPage>
    with AutomaticKeepAliveClientMixin {
  GlobalKey rootWidgetKey = GlobalKey();
  CaptureController _captureController = CaptureController();
  final CaptureController _captureController = CaptureController();
  //用户marker
  BMFMarker? userMarker;
  Marker? userMarker;
  String? portrait;
  SimpleLocation? location;
  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: 200));
  //定位
  var eventBusLocation;
  //定位邀请
  var eventBusLocationInvite;
  //定位权限
  var eventBusLocationPermission;
@@ -117,6 +112,9 @@
  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) {
      WebView.platform = SurfaceAndroidWebView();
    }
    eventBusLocation = eventBus.on<UserLocationInfoEventBus>().listen((event) {
      //设置用户头像
      setState(() {
@@ -126,8 +124,13 @@
            : event.user!.portrait);
      });
      location = event.location;
      drawMarker(
          BMFCoordinate(event.location!.latitude!, event.location!.longitude!));
      drawMarker(event.location!);
    });
    eventBusLocationInvite =
        eventBus.on<LocationInviteEventBus>().listen((event) {
          print("接收到 LocationInviteEventBus");
      LocationPage.getLocationInvite(context);
    });
    eventBusLocationPermission =
@@ -146,13 +149,12 @@
    LocationPage.getLocationInvite(context);
  }
  @override
  void dispose() {
    super.dispose();
    (eventBusLocation as StreamSubscription).cancel();
    (eventBusLocationPermission as StreamSubscription).cancel();
    (eventBusLocationInvite as StreamSubscription).cancel();
  }
  @override
@@ -163,6 +165,30 @@
        body: Stack(
          children: [
            getMapView(),
            Positioned(
                left: 10,
                bottom: 80,
                child: InkWell(
                    onTap: () {
                      if (location == null) {
                        print("还没定到位");
                        LocationUtil.startLocation(0, (state, map) {
                          if (state == LocationState.success) {
                            location = map;
                            eventBus
                                .fire(UserLocationInfoEventBus(location, null));
                          }
                        });
                      } else {
                        //移动到中心点
                        MapUtil.setCenter(_webViewController, location!);
                      }
                    },
                    child: Image.asset(
                      "assets/images/map/icon_current_position.png",
                      width: 21,
                    ))),
            PositionInfoPage(),
            Positioned(
                top: -100,
@@ -174,7 +200,6 @@
                      width: 33,
                    ))),
            locationPermissionStatus != null &&
                    locationPermissionStatus != PermissionStatus.granted
                ? Align(
@@ -182,7 +207,7 @@
                    child: InkWell(
                        onTap: () {
                          PermissionUtil.openPermission(
                              PermissionUtil.getLocationPermission(),
                                  PermissionUtil.getLocationPermission(),
                                  force: true)
                              .then((value) {
                            setState(() {
@@ -249,38 +274,29 @@
    ];
  }
  Widget? _mapView;
  //获取地图视图
  Widget getMapView() {
    return Container(
      child: BMFMapWidget(
        onBMFMapCreated: (controller) {
          _mapController = controller;
          if (location != null) {
            drawMarker(
                BMFCoordinate(location!.latitude!, location!.longitude!));
          }
        },
        mapOptions: mapOptions,
      ),
    );
    _mapView ??= getMapWebView(context, (controller) {
      _webViewController = controller;
    });
    return _mapView!;
  }
  //画marker
  void drawMarker(BMFCoordinate location) async {
    if (_mapController == null) {
      return;
    }
  void drawMarker(SimpleLocation location) async {
    String base64Img = await _captureController.capturePng();
    if (userMarker == null) {
      userMarker = await MapUtil.addMarker(_mapController, location, base64Img);
      userMarker = await MapUtil.addMarker(
          _webViewController, location, base64Img,
          height: 40, width: 40, offset: Offset(0, 20));
    } else {
      userMarker!.updateIcon(base64Img);
      userMarker!.updatePosition(location);
      MapMarkerUtil.update(_webViewController, userMarker!.Id,
          icon: base64Img, position: location);
    }
    _mapController!.setCenterCoordinate(location, true);
    MapUtil.setCenter(_webViewController, location);
  }
  @override
  bool get wantKeepAlive => true;
@@ -318,8 +334,27 @@
  GlobalKey rootWidgetKey = GlobalKey();
  var eventBusLogin;
  var eventBusLocation;
  List<LocationUserModel>? userList;
  _startLocation() {
    //开始定位
    LocationUtil.startLocation(0, (state, map) {
      if (state == LocationState.success) {
        setState(() {
          _userLocationInfo = UserLocationInfo(
              uid: user != null ? user!.id : null,
              location: map,
              updateTime: formatDate(
                  DateTime.now(), [yyyy, '-', mm, '-', dd, ' ', HH, ':', nn]),
              locationCount: 1);
        });
        eventBus
            .fire(UserLocationInfoEventBus(_userLocationInfo!.location, user));
      }
    });
  }
  @override
  void initState() {
@@ -328,20 +363,8 @@
      setState(() {
        user = value;
      });
      //开始定位
      LocationUtil.startLocation(0, (state, map) {
        if (state == LocationState.success) {
          BaiduLocation location = BaiduLocation.fromJson(map);
          setState(() {
            _userLocationInfo = UserLocationInfo(
                uid: user != null ? user!.id : null,
                location: SimpleLocation.fromBaiDuLocation(location),
                updateTime: location.locTime,
                locationCount: 1);
          });
          eventBus.fire(
              UserLocationInfoEventBus(_userLocationInfo!.location, user));
        }
      Future.delayed(const Duration(seconds: 1), () {
        _startLocation();
      });
    });
    //注册eventbus
@@ -358,6 +381,19 @@
        });
      }
    });
    //注册eventbus
    eventBusLocation = eventBus.on<RequestLocationEventBus>().listen((event) {
      if (event.location) {
        _startLocation();
      }
      if (event.center &&
          _userLocationInfo != null &&
          _userLocationInfo!.location != null) {
        MapUtil.setCenter(_webViewController, _userLocationInfo!.location!);
      }
    });
  }
  @override
@@ -365,6 +401,7 @@
    super.dispose();
    //取消订阅
    eventBusLogin.cancel();
    eventBusLocation.cancel();
  }
  void _selectLocationUser() async {
@@ -730,14 +767,12 @@
                                      ]))))
                ],
              )),
          Expanded(child:
          Align(
              alignment: Alignment.bottomCenter,
              child: getAddLocationObjectView())),
          Expanded(
              child: Align(
                  alignment: Alignment.bottomCenter,
                  child: getAddLocationObjectView())),
        ]));
  }
  //添加想定位的人
  Widget getAddLocationObjectView() {
@@ -751,10 +786,9 @@
            }
            NavigatorUtil.navigateToNextPage(
                context, AddLocationPersonPage(title: ""), (data) {
                  if(data!=null) {
                    _setSelectUserData(data);
                  }
              if (data != null) {
                _setSelectUserData(data);
              }
            });
          });
        },
@@ -1041,3 +1075,10 @@
    }
  }
}
class RequestLocationEventBus {
  bool center;
  bool location;
  RequestLocationEventBus(this.center, this.location);
}