YSMapView.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import 'dart:convert';
  2. import 'dart:math';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_tzh/home/view/YSChooseMapView.dart';
  5. import 'package:mapbox_gl/mapbox_gl.dart';
  6. import '../../tool/YSTools.dart';
  7. class YSMapView extends StatefulWidget {
  8. final Map location;
  9. const YSMapView({Key? key, required this.location}) : super(key: key);
  10. @override
  11. YSMapViewState createState() => YSMapViewState();
  12. }
  13. class YSMapViewState extends State<YSMapView> with WidgetsBindingObserver{
  14. MapboxMapController? _mapController;
  15. LatLng _latLng = const LatLng(0, 0);
  16. bool _isAgree = false;
  17. _onMapCreated(MapboxMapController controller) {
  18. _mapController = controller;
  19. }
  20. @override
  21. void initState() {
  22. WidgetsBinding.instance.addObserver(this);
  23. _latLng = LatLng(double.parse('${widget.location['latitude']??0}'), double.parse('${widget.location['longitude']??0}'));
  24. Future.delayed(const Duration(seconds: 1)).then((value) {
  25. _getLocation();
  26. });
  27. super.initState();
  28. }
  29. _getLocation() async{
  30. _isAgree = await permissionHandler('location');
  31. setState(() {});
  32. }
  33. @override
  34. void dispose() {
  35. WidgetsBinding.instance.removeObserver(this);
  36. // if(_mapController!=null){
  37. // _mapController!.dispose();
  38. // }
  39. super.dispose();
  40. }
  41. @override
  42. void didChangeAppLifecycleState(AppLifecycleState state) {
  43. if(state==AppLifecycleState.paused){
  44. setState(() {});
  45. LogUtil.d('didChangeAppLifecycleState=====paused');
  46. }
  47. super.didChangeAppLifecycleState(state);
  48. }
  49. void onStyleLoaded(MapboxMapController controller) {
  50. String geo = widget.location['coordinates'];
  51. Map geoMao = jsonDecode(geo);
  52. List coordinates = geoMao['coordinates']??[];
  53. List<LatLng> geometryList = [];
  54. for (List element in coordinates) {
  55. for (List elementSub in element) {
  56. geometryList.add(LatLng(elementSub[1], elementSub[0]));
  57. }
  58. }
  59. Future.delayed(const Duration(seconds: 1), () {
  60. controller.addSymbol(SymbolOptions(
  61. geometry: _latLng,
  62. iconImage: "images/Pointer.png",
  63. textField: widget.location['name'],
  64. textColor: "#FFFFFF",
  65. textOffset: const Offset(0, -2),
  66. textHaloWidth: 2,
  67. iconSize: 2,
  68. // textHaloColor: "#4AE6F0",
  69. ));
  70. controller.addLine(
  71. LineOptions(
  72. geometry: geometryList,
  73. lineColor: '#FF9900',
  74. lineWidth: 4
  75. )
  76. );
  77. controller.onSymbolTapped.add((argument) {
  78. ysShowBottomAlertView(context, YSChooseMapView(latitude: '${_latLng.latitude}', longitude: '${_latLng.longitude}'));
  79. });
  80. });
  81. }
  82. @override
  83. Widget build(BuildContext context) {
  84. return MapboxMap(
  85. styleString: MapboxStyles.SATELLITE,
  86. accessToken: accessToken,
  87. onMapCreated: _onMapCreated,
  88. initialCameraPosition: CameraPosition(target: _latLng, zoom: 14.0),
  89. onStyleLoadedCallback: () => onStyleLoaded(_mapController!),
  90. scrollGesturesEnabled: true,
  91. myLocationEnabled: _isAgree,
  92. attributionButtonMargins: const Point(1000, 0),
  93. logoViewMargins: const Point(1000, 0),
  94. // onUserLocationUpdated: (UserLocation location){
  95. // LogUtil.d('MapboxMap=======${location.position}');
  96. // _mapController!.addSymbol( SymbolOptions(
  97. // geometry: location.position,
  98. // iconImage: "images/Pointer.png",
  99. // textField: widget.location['位置'],
  100. // textColor: "#FFFFFF",
  101. // textOffset: const Offset(0, -2),
  102. // textHaloWidth: 2,
  103. // iconSize: 2,
  104. // // textHaloColor: "#4AE6F0",
  105. // ));
  106. // },
  107. );
  108. }
  109. }