Skip to content

Commit 5980183

Browse files
Merge pull request #1879 from EnsembleUI/fixed-map-pin
feat(maps): add fixedMarker and draggableMarker functionality to Map Widget
2 parents bbad9d2 + 1173409 commit 5980183

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

modules/location/lib/widget/maps/maps.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ class EnsembleMapWidget extends StatefulWidget
8585
_controller.toolbarRight = Utils.optionalInt(value, min: 0),
8686

8787
'mapType': (value) => _controller.mapType = value,
88+
'fixedMarker': (value) => _controller.fixedMarker =
89+
Utils.getBool(value, fallback: _controller.fixedMarker),
90+
'draggableMarker': (value) => _controller.draggableMarker =
91+
Utils.getBool(value, fallback: _controller.draggableMarker),
8892
'markers': (markerData) => setMarkers(markerData),
8993
'scrollableMarkerOverlay': (value) => _controller
9094
.scrollableMarkerOverlay =
@@ -185,6 +189,11 @@ class MyController extends WidgetController with LocationCapability {
185189
bool tiltEnabled = true;
186190
bool zoomEnabled = true;
187191

192+
// Determines whether the marker remains fixed at the center of the map when the user moves the map.
193+
bool fixedMarker = false;
194+
// Determines whether the marker can be dragged by the user.
195+
bool draggableMarker = false;
196+
188197
// toolbar has multiple button options
189198
bool showToolbar = true;
190199
bool showMapTypesButton = true;

modules/location/lib/widget/maps/maps_state.dart

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import 'package:ensemble/layout/templated.dart';
1212
import 'package:ensemble/screen_controller.dart';
1313
import 'package:ensemble/util/debouncer.dart';
1414
import 'package:ensemble/util/utils.dart';
15-
import 'package:ensemble/widget/shape.dart';
1615
import 'package:ensemble_location/location_manager.dart';
1716
import 'package:ensemble_location/widget/maps/custom_marker_pin.dart';
1817
import 'package:ensemble_location/widget/maps/map_actions.dart';
@@ -41,6 +40,8 @@ class EnsembleMapState extends MapsActionableState
4140
static const MAX_WIDTH = 500;
4241
static const MAX_HEIGHT = 500;
4342

43+
late FixedMarker _fixedMarker;
44+
4445
final Completer<GoogleMapController> _controller =
4546
Completer<GoogleMapController>();
4647

@@ -85,6 +86,7 @@ class EnsembleMapState extends MapsActionableState
8586
@override
8687
void initState() {
8788
super.initState();
89+
_fixedMarker = FixedMarker(position: widget.controller.defaultCameraLatLng);
8890
_initCurrentLocation();
8991
}
9092

@@ -230,6 +232,12 @@ class EnsembleMapState extends MapsActionableState
230232
_selectedMarkerId = markerId;
231233
}
232234

235+
// build marker image for fixed marker
236+
if (markerTemplate != null && widget.controller.fixedMarker) {
237+
_fixedMarker.icon =
238+
await _buildMarkerFromTemplate(payloads.first, markerTemplate);
239+
}
240+
233241
BitmapDescriptor? markerAsset;
234242
double zIndex = 0;
235243
if (markerId == _selectedMarkerId) {
@@ -255,6 +263,10 @@ class EnsembleMapState extends MapsActionableState
255263
position: markerPayload.latLng,
256264
icon: markerAsset ?? BitmapDescriptor.defaultMarker,
257265
consumeTapEvents: true,
266+
draggable: widget.controller.draggableMarker,
267+
onDrag: (latLng) {
268+
_moveCamera(latLng);
269+
},
258270
onTap: () {
259271
_selectMarker(markerId);
260272

@@ -571,6 +583,13 @@ class EnsembleMapState extends MapsActionableState
571583
//log("Camera moved");
572584
});
573585
}
586+
587+
if (widget.controller.fixedMarker &&
588+
_fixedMarker.position != position.target) {
589+
setState(() {
590+
_fixedMarker.position = position.target;
591+
});
592+
}
574593
}
575594

576595
void _onCameraIdle() {}
@@ -597,7 +616,17 @@ class EnsembleMapState extends MapsActionableState
597616
Set<Marker> _getMarkers() {
598617
Set<Marker> markers = {};
599618
for (MarkerPayload markerPayload in _markerPayloads) {
600-
if (markerPayload.marker != null) {
619+
if (widget.controller.fixedMarker) {
620+
markers.add(Marker(
621+
markerId: const MarkerId("fixed_marker"),
622+
position: _fixedMarker.position,
623+
icon: _fixedMarker.icon ?? BitmapDescriptor.defaultMarker,
624+
draggable: widget.controller.draggableMarker,
625+
onDrag: (position) {
626+
_moveCamera(position);
627+
},
628+
));
629+
} else if (markerPayload.marker != null) {
601630
markers.add(markerPayload.marker!);
602631
}
603632
}
@@ -625,3 +654,10 @@ class MarkerPayload {
625654
final LatLng latLng;
626655
Marker? marker;
627656
}
657+
658+
class FixedMarker {
659+
LatLng position;
660+
BitmapDescriptor? icon;
661+
662+
FixedMarker({required this.position, this.icon});
663+
}

0 commit comments

Comments
 (0)