Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 10c71a9

Browse files
committed
Dragging marker example
1 parent bb65f55 commit 10c71a9

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright 2018 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// ignore_for_file: public_member_api_docs
6+
7+
import 'dart:math';
8+
9+
import 'package:flutter/material.dart';
10+
import 'package:google_maps_flutter/google_maps_flutter.dart';
11+
12+
import 'page.dart';
13+
14+
class DragMarkerPage extends GoogleMapExampleAppPage {
15+
DragMarkerPage() : super(const Icon(Icons.drag_handle), 'Drag marker');
16+
17+
@override
18+
Widget build(BuildContext context) {
19+
return const DragMarkerBody();
20+
}
21+
}
22+
23+
class DragMarkerBody extends StatefulWidget {
24+
const DragMarkerBody();
25+
26+
@override
27+
State<StatefulWidget> createState() => DragMarkerBodyState();
28+
}
29+
30+
typedef MarkerUpdateAction = Marker Function(Marker marker);
31+
32+
class DragMarkerBodyState extends State<DragMarkerBody> {
33+
DragMarkerBodyState();
34+
static const LatLng center = LatLng(-33.86711, 151.1947171);
35+
36+
GoogleMapController? controller;
37+
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
38+
MarkerId? selectedMarker;
39+
int _markerIdCounter = 1;
40+
LatLng? markerPosition;
41+
42+
void _onMapCreated(GoogleMapController controller) {
43+
this.controller = controller;
44+
}
45+
46+
void _onMarkerTapped(MarkerId markerId) {
47+
final Marker? tappedMarker = markers[markerId];
48+
if (tappedMarker != null) {
49+
setState(() {
50+
if (markers.containsKey(selectedMarker)) {
51+
final Marker resetOld = markers[selectedMarker]!
52+
.copyWith(iconParam: BitmapDescriptor.defaultMarker);
53+
markers[selectedMarker!] = resetOld;
54+
}
55+
selectedMarker = markerId;
56+
final Marker newMarker = tappedMarker.copyWith(
57+
iconParam: BitmapDescriptor.defaultMarkerWithHue(
58+
BitmapDescriptor.hueGreen,
59+
),
60+
);
61+
markers[markerId] = newMarker;
62+
});
63+
}
64+
}
65+
66+
void _onMarkerDrag(MarkerId markerId, LatLng newPosition) async {
67+
setState(() {
68+
this.markerPosition = newPosition;
69+
});
70+
}
71+
72+
void _add() {
73+
final int markerCount = markers.length;
74+
75+
if (markerCount == 12) {
76+
return;
77+
}
78+
79+
final String markerIdVal = 'marker_id_$_markerIdCounter';
80+
_markerIdCounter++;
81+
final MarkerId markerId = MarkerId(markerIdVal);
82+
83+
final Marker marker = Marker(
84+
draggable: true,
85+
markerId: markerId,
86+
position: LatLng(
87+
center.latitude + sin(_markerIdCounter * pi / 6.0) / 20.0,
88+
center.longitude + cos(_markerIdCounter * pi / 6.0) / 20.0,
89+
),
90+
infoWindow: InfoWindow(title: markerIdVal, snippet: '*'),
91+
onTap: () => _onMarkerTapped(markerId),
92+
onDrag: (LatLng position) => _onMarkerDrag(markerId, position),
93+
);
94+
95+
setState(() {
96+
markers[markerId] = marker;
97+
});
98+
}
99+
100+
void _remove() {
101+
setState(() {
102+
if (markers.containsKey(selectedMarker)) {
103+
markers.remove(selectedMarker);
104+
}
105+
});
106+
}
107+
108+
@override
109+
Widget build(BuildContext context) {
110+
return Column(
111+
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
112+
crossAxisAlignment: CrossAxisAlignment.stretch,
113+
children: <Widget>[
114+
Expanded(
115+
child: Center(
116+
child: GoogleMap(
117+
onMapCreated: _onMapCreated,
118+
initialCameraPosition: const CameraPosition(
119+
target: center,
120+
zoom: 11.0,
121+
),
122+
markers: markers.values.toSet(),
123+
),
124+
),
125+
),
126+
Container(
127+
height: 30,
128+
padding: EdgeInsets.only(left: 12, right: 12),
129+
child: Row(
130+
mainAxisSize: MainAxisSize.max,
131+
children: [
132+
markerPosition == null ? Container() : Expanded(child: Text("lat: ${markerPosition!.latitude}")),
133+
markerPosition == null ? Container() : Expanded(child: Text("lng: ${markerPosition!.longitude}")),
134+
],
135+
),
136+
),
137+
Row(
138+
children: <Widget>[
139+
TextButton(
140+
child: const Text('add'),
141+
onPressed: _add,
142+
),
143+
TextButton(
144+
child: const Text('remove'),
145+
onPressed: _remove,
146+
),
147+
],
148+
),
149+
],
150+
);
151+
}
152+
}

packages/google_maps_flutter/google_maps_flutter/example/lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// ignore_for_file: public_member_api_docs
66

77
import 'package:flutter/material.dart';
8+
import 'package:google_maps_flutter_example/drag_marker.dart';
89
import 'package:google_maps_flutter_example/lite_mode.dart';
910
import 'animate_camera.dart';
1011
import 'map_click.dart';
@@ -34,6 +35,7 @@ final List<GoogleMapExampleAppPage> _allPages = <GoogleMapExampleAppPage>[
3435
PlacePolylinePage(),
3536
PlacePolygonPage(),
3637
PlaceCirclePage(),
38+
DragMarkerPage(),
3739
PaddingPage(),
3840
SnapshotPage(),
3941
LiteModePage(),

packages/google_maps_flutter/google_maps_flutter/example/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ dependencies:
1818
# The example app is bundled with the plugin so we use a path dependency on
1919
# the parent directory to use the current plugin's version.
2020
path: ../
21+
google_maps_flutter_platform_interface:
22+
path: ../../google_maps_flutter_platform_interface
2123
flutter_plugin_android_lifecycle: ^2.0.1
2224

2325
dev_dependencies:

0 commit comments

Comments
 (0)