|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_map/flutter_map.dart'; |
| 3 | +import 'package:latlong2/latlong.dart'; |
| 4 | + |
| 5 | +import '../widgets/drawer.dart'; |
| 6 | + |
| 7 | +class PointToLatLngPage extends StatefulWidget { |
| 8 | + static const String route = 'point_to_latlng'; |
| 9 | + |
| 10 | + @override |
| 11 | + PointToLatlngPage createState() { |
| 12 | + return PointToLatlngPage(); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +class PointToLatlngPage extends State<PointToLatLngPage> { |
| 17 | + late final MapController mapController; |
| 18 | + final pointX = 100.0; |
| 19 | + final pointY = 100.0; |
| 20 | + final pointSize = 20.0; |
| 21 | + late final mapEventSubscription; |
| 22 | + |
| 23 | + @override |
| 24 | + void initState() { |
| 25 | + super.initState(); |
| 26 | + mapController = MapController(); |
| 27 | + |
| 28 | + mapEventSubscription = mapController.mapEventStream.listen(onMapEvent); |
| 29 | + } |
| 30 | + |
| 31 | + @override |
| 32 | + Widget build(BuildContext context) { |
| 33 | + return Scaffold( |
| 34 | + appBar: AppBar(title: Text('PointToLatlng')), |
| 35 | + drawer: buildDrawer(context, PointToLatLngPage.route), |
| 36 | + body: Stack( |
| 37 | + children: [ |
| 38 | + FlutterMap( |
| 39 | + mapController: mapController, |
| 40 | + options: MapOptions( |
| 41 | + center: LatLng(51.5, -0.09), |
| 42 | + zoom: 5.0, |
| 43 | + maxZoom: 5.0, |
| 44 | + minZoom: 3.0, |
| 45 | + ), |
| 46 | + layers: [ |
| 47 | + TileLayerOptions( |
| 48 | + urlTemplate: |
| 49 | + 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', |
| 50 | + subdomains: ['a', 'b', 'c']), |
| 51 | + ], |
| 52 | + ), |
| 53 | + Positioned( |
| 54 | + top: pointY, |
| 55 | + left: pointX, |
| 56 | + child: Container( |
| 57 | + color: Colors.red, width: pointSize, height: pointSize)) |
| 58 | + ], |
| 59 | + ), |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + void onMapEvent(MapEvent mapEvent) { |
| 64 | + if (mapEvent is MapEventMove) { |
| 65 | + final latLng = mapController.pointToLatLng(CustomPoint(pointX, pointY)); |
| 66 | + |
| 67 | + print('top left of square is hovering $latLng'); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments