|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_map/flutter_map.dart'; |
| 5 | +import 'package:latlong2/latlong.dart'; |
| 6 | + |
| 7 | +import '../widgets/drawer.dart'; |
| 8 | + |
| 9 | +class PointToLatLngPage extends StatefulWidget { |
| 10 | + static const String route = 'point_to_latlng'; |
| 11 | + |
| 12 | + const PointToLatLngPage({Key? key}) : super(key: key); |
| 13 | + |
| 14 | + @override |
| 15 | + PointToLatlngPage createState() { |
| 16 | + return PointToLatlngPage(); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +class PointToLatlngPage extends State<PointToLatLngPage> { |
| 21 | + late final MapController mapController; |
| 22 | + late final StreamSubscription mapEventSubscription; |
| 23 | + final pointSize = 40.0; |
| 24 | + final pointY = 200.0; |
| 25 | + |
| 26 | + LatLng? latLng; |
| 27 | + |
| 28 | + @override |
| 29 | + void initState() { |
| 30 | + super.initState(); |
| 31 | + mapController = MapController(); |
| 32 | + |
| 33 | + mapEventSubscription = mapController.mapEventStream |
| 34 | + .listen((mapEvent) => onMapEvent(mapEvent, context)); |
| 35 | + |
| 36 | + Future.delayed(Duration.zero, () { |
| 37 | + mapController.onReady.then((_) => _updatePointLatLng(context)); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + @override |
| 42 | + Widget build(BuildContext context) { |
| 43 | + return Scaffold( |
| 44 | + appBar: AppBar(title: const Text('PointToLatlng')), |
| 45 | + floatingActionButton: Column( |
| 46 | + mainAxisAlignment: MainAxisAlignment.end, |
| 47 | + children: [ |
| 48 | + FloatingActionButton( |
| 49 | + child: const Icon(Icons.rotate_right), |
| 50 | + onPressed: () => mapController.rotate(60.0), |
| 51 | + ), |
| 52 | + const SizedBox(height: 15), |
| 53 | + FloatingActionButton( |
| 54 | + child: const Icon(Icons.cancel), |
| 55 | + onPressed: () => mapController.rotate(0.0), |
| 56 | + ), |
| 57 | + ], |
| 58 | + ), |
| 59 | + drawer: buildDrawer(context, PointToLatLngPage.route), |
| 60 | + body: Stack( |
| 61 | + children: [ |
| 62 | + FlutterMap( |
| 63 | + mapController: mapController, |
| 64 | + options: MapOptions( |
| 65 | + center: LatLng(51.5, -0.09), |
| 66 | + zoom: 5.0, |
| 67 | + minZoom: 3.0, |
| 68 | + ), |
| 69 | + children: [ |
| 70 | + TileLayerWidget( |
| 71 | + options: TileLayerOptions( |
| 72 | + urlTemplate: |
| 73 | + 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', |
| 74 | + subdomains: ['a', 'b', 'c'])), |
| 75 | + if (latLng != null) |
| 76 | + MarkerLayerWidget( |
| 77 | + options: MarkerLayerOptions( |
| 78 | + markers: [ |
| 79 | + Marker( |
| 80 | + width: pointSize, |
| 81 | + height: pointSize, |
| 82 | + point: latLng!, |
| 83 | + builder: (ctx) => const FlutterLogo(), |
| 84 | + ) |
| 85 | + ], |
| 86 | + )) |
| 87 | + ], |
| 88 | + ), |
| 89 | + Container( |
| 90 | + color: Colors.white, |
| 91 | + height: 60, |
| 92 | + child: Center( |
| 93 | + child: Column( |
| 94 | + mainAxisAlignment: MainAxisAlignment.center, |
| 95 | + children: [ |
| 96 | + Text( |
| 97 | + 'flutter logo (${latLng?.latitude.toStringAsPrecision(4)},${latLng?.longitude.toStringAsPrecision(4)})', |
| 98 | + textAlign: TextAlign.center, |
| 99 | + ), |
| 100 | + ], |
| 101 | + ))), |
| 102 | + Positioned( |
| 103 | + top: pointY - pointSize / 2, |
| 104 | + left: _getPointX(context) - pointSize / 2, |
| 105 | + child: Icon(Icons.crop_free, size: pointSize)) |
| 106 | + ], |
| 107 | + ), |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + void onMapEvent(MapEvent mapEvent, BuildContext context) { |
| 112 | + _updatePointLatLng(context); |
| 113 | + } |
| 114 | + |
| 115 | + void _updatePointLatLng(context) { |
| 116 | + final pointX = _getPointX(context); |
| 117 | + |
| 118 | + final latLng = mapController.pointToLatLng(CustomPoint(pointX, pointY)); |
| 119 | + |
| 120 | + setState(() { |
| 121 | + this.latLng = latLng; |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + double _getPointX(BuildContext context) { |
| 126 | + return MediaQuery.of(context).size.width / 2; |
| 127 | + } |
| 128 | + |
| 129 | + @override |
| 130 | + void dispose() { |
| 131 | + super.dispose(); |
| 132 | + mapEventSubscription.cancel(); |
| 133 | + } |
| 134 | +} |
0 commit comments