forked from heshamerfan97/flutter_google_maps_example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_maps_widget.dart
201 lines (185 loc) · 5.97 KB
/
google_maps_widget.dart
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_maps_flutter_example/Repositories/map_repository.dart';
import 'package:google_maps_flutter_example/Utils/map_utils.dart';
import 'package:google_maps_flutter_example/app_bloc.dart';
import 'package:google_maps_flutter_example/controller/live_location_cubit.dart';
import 'package:location/location.dart';
class GoogleMapsWidget extends StatefulWidget {
@override
_GoogleMapsWidgetState createState() => _GoogleMapsWidgetState();
}
class _GoogleMapsWidgetState extends State<GoogleMapsWidget> {
Completer<GoogleMapController> _controller = Completer();
double lat = 30.029585;
double lng = 31.022356;
final LatLng initialLatLng = LatLng(30.029585, 31.022356);
final LatLng destinationLatLng = LatLng(30.060567, 30.962413);
Set<Marker> _markers = Set<Marker>();
late BitmapDescriptor customIcon;
bool mapDarkMode = true;
late String _darkMapStyle;
late String _lightMapStyle;
Set<Polyline> _polyline = {};
List<LatLng> polylineCoordinates = [];
@override
void initState() {
BitmapDescriptor.fromAssetImage(
ImageConfiguration(size: Size(50, 50)), 'assets/images/marker_car.png')
.then((icon) {
customIcon = icon;
});
_loadMapStyles();
super.initState();
}
Future _loadMapStyles() async {
_darkMapStyle = await rootBundle.loadString('assets/map_style/dark.json');
_lightMapStyle = await rootBundle.loadString('assets/map_style/light.json');
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
setState(() {
_setMapStyle();
});
}
@override
Widget build(BuildContext context) {
return Stack(children: [
BlocListener<LiveLocationCubit, LocationData?>(
listener: (context, liveLocation) {
if (liveLocation != null) {
_updateUserMarker(liveLocation);
}
},
child: GoogleMap(
mapType: MapType.normal,
rotateGesturesEnabled: true,
zoomGesturesEnabled: true,
trafficEnabled: false,
tiltGesturesEnabled: false,
scrollGesturesEnabled: true,
compassEnabled: true,
myLocationButtonEnabled: true,
zoomControlsEnabled: false,
mapToolbarEnabled: false,
markers: _markers,
polylines: _polyline,
initialCameraPosition: CameraPosition(
target: initialLatLng,
zoom: 14.47,
),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
_setMapPins([LatLng(30.029585, 31.022356)]);
_setMapStyle();
_addPolyLines();
},
),
),
Positioned(
top: 100,
right: 30,
child: Container(
height: 30,
width: 30,
child: IconButton(
icon: Icon(
mapDarkMode ? Icons.brightness_4 : Icons.brightness_5,
color: Theme.of(context).primaryColor,
),
onPressed: () {
setState(() {
mapDarkMode = !mapDarkMode;
_setMapStyle();
});
},
),
)),
Positioned(
bottom: 50,
left: 30,
child: Container(
height: 30,
width: 30,
child: IconButton(
icon: Icon(
Icons.send,
color: Theme.of(context).primaryColor,
),
onPressed: () {
AppBloc.liveLocationCubit.startService();
},
),
)),
]);
}
_moveCamera([double? zoom]) async {
final CameraPosition myPosition = CameraPosition(
target: LatLng(lat, lng),
zoom: zoom ?? 14.4746,
);
final GoogleMapController controller = await _controller.future;
controller.moveCamera(CameraUpdate.newCameraPosition(myPosition));
}
_setMapPins(List<LatLng> markersLocation) {
_markers.clear();
setState(() {
markersLocation.forEach((markerLocation) {
_markers.add(Marker(
markerId: MarkerId(markerLocation.toString()),
position: markerLocation,
icon: customIcon,
));
});
_markers.add(Marker(
markerId: MarkerId(destinationLatLng.toString()),
position: destinationLatLng,
));
});
}
Future _setMapStyle() async {
final controller = await _controller.future;
if (mapDarkMode)
controller.setMapStyle(_darkMapStyle);
else
controller.setMapStyle(_lightMapStyle);
}
_addPolyLines() {
setState(() {
lat = (initialLatLng.latitude + destinationLatLng.latitude) / 2;
lng = (initialLatLng.longitude + destinationLatLng.longitude) / 2;
_moveCamera(13.0);
_setPolyLine();
});
}
_setPolyLine() async {
final result = await MapRepository().getRouteCoordinates(initialLatLng, destinationLatLng);
final route = result.data["routes"][0]["overview_polyline"]["points"];
setState(() {
_polyline.add(Polyline(
polylineId: PolylineId("tripRoute"),
//pass any string here
width: 3,
geodesic: true,
points: MapUtils.convertToLatLng(MapUtils.decodePoly(route)),
color: Theme.of(context).primaryColor));
});
}
_updateUserMarker(LocationData currentLocation) {
if (currentLocation.latitude != null && currentLocation.longitude != null) {
_markers.removeWhere((marker) => marker.markerId.value == 'user');
lat = currentLocation.latitude!;
lng = currentLocation.longitude!;
_moveCamera();
setState(() {
_markers.add(Marker(
markerId: MarkerId('user'),
position: LatLng(currentLocation.latitude!, currentLocation.longitude!)));
});
}
}
}