adding google maps baby!

This commit is contained in:
2024-05-17 20:12:59 +02:00
parent de221493ce
commit 7c03b8142a
8 changed files with 212 additions and 69 deletions

View File

@@ -0,0 +1,81 @@
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class MapWidget extends StatefulWidget {
@override
State<MapWidget> createState() => _MapWidgetState();
}
class _MapWidgetState extends State<MapWidget> {
late GoogleMapController mapController;
final LatLng _center = const LatLng(45.521563, -122.677433);
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
void _onCameraIdle() {
// print(mapController.getLatLng());
}
@override
Widget build(BuildContext context) {
return GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
onCameraIdle: _onCameraIdle,
);
}
}
// GeoCode geoCode = GeoCode();
// String _currentCityName = "...";
// final Debounce _debounce = Debounce(Duration(seconds: 3));
// final LatLng _center = const LatLng(45.521563, -122.677433);
// late GoogleMapController mapController;
// void _onMapCreated(GoogleMapController controller) {
// mapController = controller;
// }
// // void _setCurrentCityName() async {
// if (mapController.camera.zoom < 9) {
// return; // Don't bother if the view is too wide
// }
// var currentCoordinates = mapController.camera.center;
// String? city;
// try{
// List<Placemark> placemarks = await placemarkFromCoordinates(currentCoordinates.latitude, currentCoordinates.longitude);
// city = placemarks[0].locality.toString();
// } catch (e) {
// debugPrint("Error: $e");
// try {
// Address address = await geoCode.reverseGeocoding(latitude: currentCoordinates.latitude, longitude: currentCoordinates.longitude);
// if (address.city == null || address.city.toString().contains("Throttled!")){
// throw Exception("Probably rate limited");
// }
// city = address.city.toString();
// } catch (e) {
// debugPrint("Error: $e");
// }
// }
// if (city != null) {
// setState(() {
// _currentCityName = city!;
// });
// } else {
// _debounce(() async {_setCurrentCityName();});
// }
// }

View File

@@ -1,17 +1,21 @@
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:sliding_up_panel/sliding_up_panel.dart';
import 'package:latlong2/latlong.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geocode/geocode.dart';
import 'dart:async';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:fast_network_navigation/modules/navigation.dart';
import 'package:fast_network_navigation/modules/map.dart';
class MapPage extends StatefulWidget {
class MainPage extends StatefulWidget {
@override
_MapPageState createState() => _MapPageState();
State<MainPage> createState() => _MainPageState();
}
class Debounce {
Duration delay;
Timer? _timer;
@@ -31,44 +35,7 @@ class Debounce {
}
class _MapPageState extends State<MapPage> {
GeoCode geoCode = GeoCode();
final mapController = MapController();
String _currentCityName = "...";
final Debounce _debounce = Debounce(Duration(seconds: 3));
void _setCurrentCityName() async {
if (mapController.camera.zoom < 9) {
return; // Don't bother if the view is too wide
}
var currentCoordinates = mapController.camera.center;
String? city;
try{
List<Placemark> placemarks = await placemarkFromCoordinates(currentCoordinates.latitude, currentCoordinates.longitude);
city = placemarks[0].locality.toString();
} catch (e) {
debugPrint("Error: $e");
try {
Address address = await geoCode.reverseGeocoding(latitude: currentCoordinates.latitude, longitude: currentCoordinates.longitude);
if (address.city == null || address.city.toString().contains("Throttled!")){
throw Exception("Probably rate limited");
}
city = address.city.toString();
} catch (e) {
debugPrint("Error: $e");
}
}
if (city != null) {
setState(() {
_currentCityName = city!;
});
} else {
_debounce(() async {_setCurrentCityName();});
}
}
class _MainPageState extends State<MainPage> {
@override
Widget build(BuildContext context) {
@@ -77,21 +44,7 @@ class _MapPageState extends State<MapPage> {
renderPanelSheet: false,
panel: _floatingPanel(theme),
collapsed: _floatingCollapsed(theme),
body: FlutterMap(
mapController: mapController,
options: MapOptions(
initialZoom: 11,
initialCenter: LatLng(51.509364, -0.128928),
onMapReady: () {
mapController.mapEventStream.listen((evt) {_debounce(() async {_setCurrentCityName();});});
// And any other `MapController` dependent non-movement methods
},
),
children: [
openStreetMapTileLayer,
],
),
body: MapWidget()
);
}
@@ -138,16 +91,9 @@ class _MapPageState extends State<MapPage> {
Widget Greeting (ThemeData theme) {
return Center(
child: Text(
"Explore ${_currentCityName}",
"Explore #todo",
style: TextStyle(color: theme.primaryColor, fontSize: 24.0, fontWeight: FontWeight.bold),
),
);
}
}
TileLayer get openStreetMapTileLayer => TileLayer(
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName: 'flutter_map',
);
// Add a pin to the map