-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy path01. Flutter Google Maps Marker
106 lines (97 loc) · 3.64 KB
/
01. Flutter Google Maps Marker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:provider/provider.dart';
import 'package:sizer/sizer.dart';
class GoogleMapsMarkerNotifier extends ChangeNotifier {
final List<Marker> _markers = [];
bool isSoloMarker = false;
addNewMarker({required LatLng latLng}) {
if (isSoloMarker) {
_markers.clear();
Marker marker = Marker(
position: LatLng(latLng.latitude, latLng.longitude),
markerId: MarkerId(latLng.toString()));
_markers.add(marker);
notifyListeners();
} else {
Marker marker = Marker(
position: LatLng(latLng.latitude, latLng.longitude),
markerId: MarkerId(latLng.toString()));
_markers.add(marker);
notifyListeners();
}
}
setSoloMarker() {
isSoloMarker = !isSoloMarker;
notifyListeners();
}
clearMarkers() {
_markers.clear();
notifyListeners();
}
}
class GoogleMapsMarkerExample extends StatelessWidget {
const GoogleMapsMarkerExample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
Completer<GoogleMapController> _controller = Completer();
GoogleMapsMarkerNotifier googleMapsMarkerNotifier(
{required bool renderUI}) =>
Provider.of<GoogleMapsMarkerNotifier>(context, listen: renderUI);
return Sizer(builder: ((context, orientation, deviceType) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
floatingActionButtonLocation:
FloatingActionButtonLocation.centerDocked,
floatingActionButton: SizedBox(
child: Column(mainAxisAlignment: MainAxisAlignment.end, children: [
CustomRoundedButton(
onTap: () {
googleMapsMarkerNotifier(renderUI: false).setSoloMarker();
},
backgroundColor:
googleMapsMarkerNotifier(renderUI: true).isSoloMarker
? Colors.deepOrangeAccent
: KConstantColors.blueColor,
label: googleMapsMarkerNotifier(renderUI: true).isSoloMarker
? "Single mode"
: "Multiple mode",
height: 5,
width: 40),
vSizedBox1,
if (googleMapsMarkerNotifier(renderUI: true)._markers.isNotEmpty)
CustomRoundedButton(
onTap: () {
googleMapsMarkerNotifier(renderUI: false).clearMarkers();
},
backgroundColor: KConstantColors.bgColorFaint,
label: "Clear markers",
height: 5,
width: 40),
]),
),
appBar: AppBar(
backgroundColor: KConstantColors.bgColorFaint,
title: Text("Google maps markers",
style: KCustomTextStyle.kBold(context, 14))),
backgroundColor: KConstantColors.bgColor,
body: GoogleMap(
mapType: MapType.hybrid,
onTap: (latln) {
googleMapsMarkerNotifier(renderUI: false)
.addNewMarker(latLng: latln);
},
markers: Set<Marker>.from(
googleMapsMarkerNotifier(renderUI: true)._markers.toList()),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
initialCameraPosition: const CameraPosition(
zoom: 16, target: LatLng(28.7041, 77.1025))),
),
);
}));
}
}