import 'dart:convert'; import 'dart:math'; import 'package:flutter/material.dart'; import 'package:flutter_tzh/home/view/YSChooseMapView.dart'; import 'package:mapbox_gl/mapbox_gl.dart'; import '../../tool/YSTools.dart'; class YSMapView extends StatefulWidget { final Map location; const YSMapView({Key? key, required this.location}) : super(key: key); @override YSMapViewState createState() => YSMapViewState(); } class YSMapViewState extends State with WidgetsBindingObserver{ MapboxMapController? _mapController; LatLng _latLng = const LatLng(0, 0); bool _isAgree = false; _onMapCreated(MapboxMapController controller) { _mapController = controller; } @override void initState() { WidgetsBinding.instance.addObserver(this); _latLng = LatLng(double.parse('${widget.location['latitude']??0}'), double.parse('${widget.location['longitude']??0}')); Future.delayed(const Duration(seconds: 1)).then((value) { _getLocation(); }); super.initState(); } _getLocation() async{ _isAgree = await permissionHandler('location'); setState(() {}); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); // if(_mapController!=null){ // _mapController!.dispose(); // } super.dispose(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { if(state==AppLifecycleState.paused){ setState(() {}); LogUtil.d('didChangeAppLifecycleState=====paused'); } super.didChangeAppLifecycleState(state); } void onStyleLoaded(MapboxMapController controller) { String geo = widget.location['coordinates']; Map geoMao = jsonDecode(geo); List coordinates = geoMao['coordinates']??[]; List geometryList = []; for (List element in coordinates) { for (List elementSub in element) { geometryList.add(LatLng(elementSub[1], elementSub[0])); } } Future.delayed(const Duration(seconds: 1), () { controller.addSymbol(SymbolOptions( geometry: _latLng, iconImage: "images/Pointer.png", textField: widget.location['name'], textColor: "#FFFFFF", textOffset: const Offset(0, -2), textHaloWidth: 2, iconSize: 2, // textHaloColor: "#4AE6F0", )); controller.addLine( LineOptions( geometry: geometryList, lineColor: '#FF9900', lineWidth: 4 ) ); controller.onSymbolTapped.add((argument) { ysShowBottomAlertView(context, YSChooseMapView(latitude: '${_latLng.latitude}', longitude: '${_latLng.longitude}')); }); }); } @override Widget build(BuildContext context) { return MapboxMap( styleString: MapboxStyles.SATELLITE, accessToken: accessToken, onMapCreated: _onMapCreated, initialCameraPosition: CameraPosition(target: _latLng, zoom: 14.0), onStyleLoadedCallback: () => onStyleLoaded(_mapController!), scrollGesturesEnabled: true, myLocationEnabled: _isAgree, attributionButtonMargins: const Point(1000, 0), logoViewMargins: const Point(1000, 0), // onUserLocationUpdated: (UserLocation location){ // LogUtil.d('MapboxMap=======${location.position}'); // _mapController!.addSymbol( SymbolOptions( // geometry: location.position, // iconImage: "images/Pointer.png", // textField: widget.location['位置'], // textColor: "#FFFFFF", // textOffset: const Offset(0, -2), // textHaloWidth: 2, // iconSize: 2, // // textHaloColor: "#4AE6F0", // )); // }, ); } }