108 lines
3.0 KiB
Dart
108 lines
3.0 KiB
Dart
// A map that allows the user to select a location for a new trip.
|
|
import 'dart:developer';
|
|
|
|
import 'package:anyway/constants.dart';
|
|
import 'package:anyway/modules/landmark_map_marker.dart';
|
|
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';
|
|
|
|
|
|
class NewTripMap extends StatefulWidget {
|
|
Trip trip;
|
|
NewTripMap(
|
|
this.trip,
|
|
);
|
|
|
|
@override
|
|
State<NewTripMap> createState() => _NewTripMapState();
|
|
}
|
|
|
|
class _NewTripMapState extends State<NewTripMap> {
|
|
final CameraPosition _cameraPosition = const CameraPosition(
|
|
target: LatLng(48.8566, 2.3522),
|
|
zoom: 11.0,
|
|
);
|
|
GoogleMapController? _mapController;
|
|
final Set<Marker> _markers = <Marker>{};
|
|
|
|
_onLongPress(LatLng location) {
|
|
widget.trip.landmarks.clear();
|
|
widget.trip.addLandmark(
|
|
Landmark(
|
|
uuid: 'pending',
|
|
name: 'start',
|
|
location: [location.latitude, location.longitude],
|
|
type: typeStart
|
|
)
|
|
);
|
|
}
|
|
|
|
updateTripDetails() async {
|
|
_markers.clear();
|
|
if (widget.trip.landmarks.isNotEmpty) {
|
|
Landmark landmark = widget.trip.landmarks.first;
|
|
_markers.add(
|
|
Marker(
|
|
markerId: MarkerId(landmark.uuid),
|
|
position: LatLng(landmark.location[0], landmark.location[1]),
|
|
icon: await ThemedMarker(landmark: landmark, position: 0).toBitmapDescriptor(
|
|
logicalSize: const Size(150, 150),
|
|
imageSize: const Size(150, 150)
|
|
),
|
|
)
|
|
);
|
|
// check if the controller is ready
|
|
|
|
if (_mapController != null) {
|
|
_mapController!.animateCamera(
|
|
CameraUpdate.newLatLng(
|
|
LatLng(landmark.location[0], landmark.location[1])
|
|
)
|
|
);
|
|
}
|
|
setState(() {});
|
|
}
|
|
}
|
|
|
|
void _onMapCreated(GoogleMapController controller) async {
|
|
_mapController = controller;
|
|
}
|
|
|
|
|
|
@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,
|
|
onLongPress: _onLongPress,
|
|
markers: _markers,
|
|
cloudMapId: MAP_ID,
|
|
mapToolbarEnabled: false,
|
|
zoomControlsEnabled: false,
|
|
myLocationButtonEnabled: false,
|
|
myLocationEnabled: useLocation,
|
|
);
|
|
}
|
|
} |