Better location handling on map
This commit is contained in:
		@@ -6,22 +6,23 @@ import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:anyway/structs/landmark.dart';
 | 
			
		||||
import 'package:anyway/structs/trip.dart';
 | 
			
		||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
 | 
			
		||||
import 'package:shared_preferences/shared_preferences.dart';
 | 
			
		||||
import 'package:widget_to_marker/widget_to_marker.dart';
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class MapWidget extends StatefulWidget {
 | 
			
		||||
class CurrentTripMap extends StatefulWidget {
 | 
			
		||||
 | 
			
		||||
  final Trip? trip;
 | 
			
		||||
 | 
			
		||||
  MapWidget({
 | 
			
		||||
  CurrentTripMap({
 | 
			
		||||
    this.trip
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  State<MapWidget> createState() => _MapWidgetState();
 | 
			
		||||
  State<CurrentTripMap> createState() => _CurrentTripMapState();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class _MapWidgetState extends State<MapWidget> {
 | 
			
		||||
class _CurrentTripMapState extends State<CurrentTripMap> {
 | 
			
		||||
  late GoogleMapController mapController;
 | 
			
		||||
 | 
			
		||||
  CameraPosition _cameraPosition = CameraPosition(
 | 
			
		||||
@@ -67,9 +68,27 @@ class _MapWidgetState extends State<MapWidget> {
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  Widget build(BuildContext context) {
 | 
			
		||||
    widget.trip?.addListener(setMapMarkers);
 | 
			
		||||
    Future<SharedPreferences> preferences = SharedPreferences.getInstance();
 | 
			
		||||
 | 
			
		||||
    return FutureBuilder(
 | 
			
		||||
      future: preferences,
 | 
			
		||||
      builder: (context, snapshot) {
 | 
			
		||||
        if (snapshot.hasData) {
 | 
			
		||||
          SharedPreferences prefs = snapshot.data as SharedPreferences;
 | 
			
		||||
          bool useLocation = prefs.getBool('useLocation') ?? true;
 | 
			
		||||
          return _buildMap(useLocation);
 | 
			
		||||
        } else {
 | 
			
		||||
          return const CircularProgressIndicator();
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Widget _buildMap(bool useLocation) {
 | 
			
		||||
    return GoogleMap(
 | 
			
		||||
      onMapCreated: _onMapCreated,
 | 
			
		||||
      initialCameraPosition: _cameraPosition,
 | 
			
		||||
@@ -79,7 +98,9 @@ class _MapWidgetState extends State<MapWidget> {
 | 
			
		||||
      cloudMapId: MAP_ID,
 | 
			
		||||
      mapToolbarEnabled: false,
 | 
			
		||||
      zoomControlsEnabled: false,
 | 
			
		||||
      myLocationEnabled: true,
 | 
			
		||||
      myLocationEnabled: useLocation,
 | 
			
		||||
      myLocationButtonEnabled: false,
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@ import 'package:anyway/structs/landmark.dart';
 | 
			
		||||
import 'package:anyway/structs/trip.dart';
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
 | 
			
		||||
import 'package:shared_preferences/shared_preferences.dart';
 | 
			
		||||
import 'package:widget_to_marker/widget_to_marker.dart';
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -72,6 +73,23 @@ class _NewTripMapState extends State<NewTripMap> {
 | 
			
		||||
  @override
 | 
			
		||||
  Widget build(BuildContext context) {
 | 
			
		||||
    widget.trip.addListener(updateTripDetails);
 | 
			
		||||
    Future<SharedPreferences> preferences = SharedPreferences.getInstance();
 | 
			
		||||
 | 
			
		||||
    return FutureBuilder(
 | 
			
		||||
      future: preferences,
 | 
			
		||||
      builder: (context, snapshot) {
 | 
			
		||||
        if (snapshot.hasData) {
 | 
			
		||||
          SharedPreferences prefs = snapshot.data as SharedPreferences;
 | 
			
		||||
          bool useLocation = prefs.getBool('useLocation') ?? true;
 | 
			
		||||
          return _buildMap(useLocation);
 | 
			
		||||
        } else {
 | 
			
		||||
          return const CircularProgressIndicator();
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Widget _buildMap(bool useLocation) {
 | 
			
		||||
    return GoogleMap(
 | 
			
		||||
      onMapCreated: _onMapCreated,
 | 
			
		||||
      initialCameraPosition: _cameraPosition,
 | 
			
		||||
@@ -80,8 +98,8 @@ class _NewTripMapState extends State<NewTripMap> {
 | 
			
		||||
      cloudMapId: MAP_ID,
 | 
			
		||||
      mapToolbarEnabled: false,
 | 
			
		||||
      zoomControlsEnabled: false,
 | 
			
		||||
      // TODO: should be loaded from the sharedprefs
 | 
			
		||||
      myLocationEnabled: true,
 | 
			
		||||
      myLocationButtonEnabled: false,
 | 
			
		||||
      myLocationEnabled: useLocation,
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user