87 lines
2.3 KiB
Dart
87 lines
2.3 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/themed_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: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,
|
|
);
|
|
late GoogleMapController _mapController;
|
|
final Set<Marker> _markers = <Marker>{};
|
|
|
|
_onLongPress(LatLng location) {
|
|
log('Long press: $location');
|
|
widget.trip.landmarks.clear();
|
|
widget.trip.addLandmark(
|
|
Landmark(
|
|
uuid: 'pending',
|
|
name: 'start',
|
|
location: [location.latitude, location.longitude],
|
|
type: start
|
|
)
|
|
);
|
|
}
|
|
|
|
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)
|
|
),
|
|
)
|
|
);
|
|
_mapController.moveCamera(
|
|
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);
|
|
return GoogleMap(
|
|
onMapCreated: _onMapCreated,
|
|
initialCameraPosition: _cameraPosition,
|
|
onLongPress: _onLongPress,
|
|
markers: _markers,
|
|
cloudMapId: MAP_ID,
|
|
mapToolbarEnabled: false,
|
|
zoomControlsEnabled: false,
|
|
// TODO: should be loaded from the sharedprefs
|
|
myLocationEnabled: true,
|
|
);
|
|
}
|
|
} |