Stand 2025-01-06
This commit is contained in:
parent
16f1442564
commit
b30db04fe2
1 changed files with 105 additions and 88 deletions
|
@ -7,40 +7,48 @@ import '../../consts/consts.dart';
|
|||
import '../../packages/ambito_theme/ambito_theme.dart';
|
||||
|
||||
class MapWidget extends StatefulWidget {
|
||||
const MapWidget(
|
||||
{super.key,
|
||||
required this.markers,
|
||||
required this.polygons,
|
||||
required this.polylines});
|
||||
const MapWidget({
|
||||
super.key,
|
||||
required this.initialPosition, // Make initial position required
|
||||
this.markers = const {}, // Provide default empty sets
|
||||
this.polygons = const {},
|
||||
this.polylines = const {},
|
||||
});
|
||||
|
||||
final LatLng initialPosition; // Use LatLng directly
|
||||
final Set<Marker> markers;
|
||||
final Set<Polygon> polygons;
|
||||
final Set<Polyline> polylines;
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => MapWidgetState();
|
||||
State<MapWidget> createState() => _MapWidgetState(); // Use shorter syntax
|
||||
}
|
||||
|
||||
class MapWidgetState extends State<MapWidget> {
|
||||
final Completer<GoogleMapController> _controller =
|
||||
Completer<GoogleMapController>();
|
||||
|
||||
class _MapWidgetState extends State<MapWidget> {
|
||||
final Completer<GoogleMapController> _controller = Completer();
|
||||
bool _drawPolygon = false;
|
||||
int _counter = 0;
|
||||
|
||||
static CameraPosition _initialPos = const CameraPosition(
|
||||
target: LatLng(0, 0),
|
||||
zoom: 14.4746,
|
||||
);
|
||||
late CameraPosition _initialCameraPosition; // Use late for initialization
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
setState(() {
|
||||
_initialPos =
|
||||
CameraPosition(target: widget.markers.first.position, zoom: 14);
|
||||
});
|
||||
|
||||
super.initState();
|
||||
_initialCameraPosition = CameraPosition(
|
||||
target: widget.initialPosition,
|
||||
zoom: 14,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant MapWidget oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.initialPosition != widget.initialPosition) {
|
||||
_initialCameraPosition = CameraPosition(
|
||||
target: widget.initialPosition,
|
||||
zoom: 14,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -54,44 +62,13 @@ class MapWidgetState extends State<MapWidget> {
|
|||
children: [
|
||||
Expanded(
|
||||
child: GoogleMap(
|
||||
onTap: (position) {
|
||||
if (_drawPolygon == true) {
|
||||
setState(() {
|
||||
widget.markers.add(
|
||||
Marker(
|
||||
onTap: onTapMarker('polygon_$_counter'),
|
||||
markerId: MarkerId('polygon_$_counter'),
|
||||
position: position,
|
||||
),
|
||||
);
|
||||
if (_counter > 0) {
|
||||
logger.i('add polyline');
|
||||
widget.polylines.add(
|
||||
Polyline(
|
||||
polylineId: PolylineId('polyline_$_counter'),
|
||||
color: theme.currentColorScheme.primary,
|
||||
width: 2,
|
||||
points: [
|
||||
widget.markers
|
||||
.where((el) =>
|
||||
el.markerId ==
|
||||
MarkerId('polygon_${_counter - 1}'))
|
||||
.first
|
||||
.position,
|
||||
position
|
||||
]),
|
||||
);
|
||||
}
|
||||
_counter++;
|
||||
});
|
||||
}
|
||||
},
|
||||
onTap: _handleMapTap, // Extract onTap logic
|
||||
mapToolbarEnabled: true,
|
||||
mapType: MapType.hybrid,
|
||||
markers: widget.markers,
|
||||
polygons: widget.polygons,
|
||||
polylines: widget.polylines,
|
||||
initialCameraPosition: _initialPos,
|
||||
initialCameraPosition: _initialCameraPosition,
|
||||
onMapCreated: (GoogleMapController controller) {
|
||||
_controller.complete(controller);
|
||||
},
|
||||
|
@ -101,27 +78,67 @@ class MapWidgetState extends State<MapWidget> {
|
|||
children: [
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_drawPolygon = !_drawPolygon;
|
||||
});
|
||||
},
|
||||
onPressed: () => setState(
|
||||
() => _drawPolygon = !_drawPolygon), // Shorter syntax
|
||||
icon: Icon(
|
||||
Icons.polyline_outlined,
|
||||
color: (_drawPolygon
|
||||
color: _drawPolygon
|
||||
? theme.currentColorScheme.primary
|
||||
: theme.currentColorScheme.outline),
|
||||
))
|
||||
: theme.currentColorScheme.outline,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
onTapMarker(String counter) {
|
||||
logger.d(counter);
|
||||
if (counter == 'polygon_0') {
|
||||
void _handleMapTap(LatLng position) {
|
||||
final AmbitoTheme theme = getTheme(context);
|
||||
if (_drawPolygon) {
|
||||
setState(() {
|
||||
final markerId = MarkerId('polygon_$_counter');
|
||||
widget.markers.add(
|
||||
Marker(
|
||||
onTap: () => _onTapMarker(markerId.value), // Use closure for onTap
|
||||
markerId: markerId,
|
||||
position: position,
|
||||
),
|
||||
);
|
||||
|
||||
if (_counter > 0) {
|
||||
final previousMarkerId = MarkerId('polygon_${_counter - 1}');
|
||||
final previousMarker = widget.markers.firstWhere(
|
||||
(el) => el.markerId == previousMarkerId,
|
||||
orElse: () => Marker(
|
||||
markerId: const MarkerId('invalid'),
|
||||
position: const LatLng(
|
||||
0, 0)), // Provide a default in case it is not found
|
||||
);
|
||||
|
||||
if (previousMarker.markerId.value != 'invalid') {
|
||||
widget.polylines.add(
|
||||
Polyline(
|
||||
polylineId: PolylineId('polyline_$_counter'),
|
||||
color: theme.currentColorScheme.primary,
|
||||
width: 2,
|
||||
points: [previousMarker.position, position],
|
||||
),
|
||||
);
|
||||
} else {
|
||||
logger.w('Previous marker not found');
|
||||
}
|
||||
}
|
||||
_counter++;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _onTapMarker(String markerId) {
|
||||
logger.d(markerId);
|
||||
if (markerId == 'polygon_0') {
|
||||
logger.d('first pressed');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue