import 'dart:ui'; import 'package:flutter/material.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:flutter_baidu_mapapi_utils/flutter_baidu_mapapi_utils.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/utils/flutter_utils.dart'; import 'package:huixiang/utils/location.dart'; import 'package:huixiang/view_widget/my_appbar.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; class LocationMap extends StatefulWidget { final Map arguments; LocationMap({this.arguments}); @override State createState() { return _LocationMap(); } } class _LocationMap extends State { @override void initState() { super.initState(); startLocation(); } void startLocation() async { Location.getInstance().aMapFlutterLocation.onResultCallback().listen((event) { if (event != null && event["latitude"] != null && event["longitude"] != null) { print("location: $event"); if (event["latitude"] is String && event["longitude"] is String) { myLatLng = BMFCoordinate(double.tryParse(event["latitude"]), double.tryParse(event["longitude"])); } else { myLatLng = BMFCoordinate(event["latitude"], event["longitude"]); } AppUtils.coordConvert(myLatLng).then((value) { this.myLatLng = value; locationShow(); }); } }); Location.getInstance().prepareLoc(); Location.getInstance().startLocation(context); } locationShow() { BMFLocation location = BMFLocation( coordinate: myLatLng, altitude: 0, horizontalAccuracy: 5, verticalAccuracy: -1.0, speed: -1.0, course: -1.0, ); BMFUserLocation userLocation = BMFUserLocation( location: location, ); setState(() { _mapController.updateLocationData(userLocation); }); } @override void dispose() { super.dispose(); Location.getInstance().sotpLocation(); } @override Widget build(BuildContext context) { return Scaffold( appBar: MyAppBar( background: Color(0xFFF7F7F7), title: widget.arguments["storeName"], titleColor: Colors.black87, titleSize: 18.sp, leadingColor: Colors.black, ), body: Container( child: BMFMapWidget( mapOptions: BMFMapOptions( center: BMFCoordinate( double.tryParse(widget.arguments["lat"]), double.tryParse(widget.arguments["lng"]), ), showZoomControl: false, showMapScaleBar: false, zoomLevel: 12, ), onBMFMapCreated: onMapCreated, ), ), ); } BMFMapController _mapController; BMFCoordinate latLng; BMFCoordinate myLatLng; BMFMarker bmfMarker; onMapCreated(BMFMapController controller) { _mapController = controller; setState(() { _mapController.showUserLocation(true); _mapController.setCustomMapStyle('assets/map_style/chatian.sty', 0); BMFUserLocationDisplayParam displayParam = BMFUserLocationDisplayParam( locationViewOffsetX: 0, locationViewOffsetY: 0, accuracyCircleFillColor: Colors.red, accuracyCircleStrokeColor: Colors.blue, isAccuracyCircleShow: true, locationViewImage: 'assets/image/icon_my_location.png', locationViewHierarchy: BMFLocationViewHierarchy.LOCATION_VIEW_HIERARCHY_BOTTOM, ); _mapController.updateLocationViewWithParam(displayParam); addMarker(); }); } addMarker() async { // latLng = await AppUtils.coordConvert(BMFCoordinate(double.tryParse(widget.arguments["lat"]), // double.tryParse(widget.arguments["lng"]))); latLng = BMFCoordinate(double.tryParse(widget.arguments["lat"]), double.tryParse(widget.arguments["lng"])); if (bmfMarker == null && _mapController != null) { bmfMarker = BMFMarker( position: latLng, centerOffset: BMFPoint(0.5, 0.7), enabled: false, icon: "assets/image/icon_map_marker.png", draggable: false, ); _mapController.addMarker(bmfMarker); } _mapController.updateMapOptions( BMFMapOptions( center: latLng, zoomLevel: 15, ), ); } }