@@ -2,15 +2,22 @@ import 'dart:async';
22import 'package:bloc/bloc.dart' ;
33import 'package:geolocator/geolocator.dart' ;
44import 'package:google_maps_flutter/google_maps_flutter.dart' ;
5+ import 'package:nesteo_app/model/nestingbox.dart' ;
56import './mapcontrol.dart' ;
67
8+ typedef Marker MarkerUpdateAction (Marker marker);
9+
710class MapControlBloc extends Bloc <MapControlEvent , MapControlState > {
811 GoogleMapController controller;
912 GoogleMap googleMap;
13+ Set <Marker > markers;
1014 LatLng location = LatLng (52.3537269 , 9.724127 );
1115 MapType mapType = MapType .normal;
1216 double zoom = 16 ;
1317 double tilt = 20 ;
18+ List <NestingBox > nestingBoxList;
19+ LatLngBounds bounds;
20+ bool created = false ;
1421
1522 @override
1623 MapControlState get initialState => InitialMapControlState ();
@@ -19,23 +26,25 @@ class MapControlBloc extends Bloc<MapControlEvent, MapControlState> {
1926 Stream <MapControlState > mapEventToState (
2027 MapControlEvent event,
2128 ) async * {
22- print (event.toString ());
2329 if (event is RebuildMapEvent ) {
2430 this .add (
2531 BuildMapEvent (
2632 mapType: this .mapType,
2733 tilt: this .tilt,
2834 zoom: this .zoom,
35+ markers: this .markers,
36+ nestingBoxList: event.nestingBoxList,
2937 ),
3038 );
3139 }
40+
3241 if (event is BuildMapEvent ) {
3342 if (this .state is ! InitialMapControlState ) {
3443 yield MapChangingState ();
3544 }
36- print ('BuildMapEvent' );
3745 location = this .location;
3846 mapType = event.mapType;
47+ nestingBoxList = event.nestingBoxList;
3948
4049 this .googleMap = GoogleMap (
4150 initialCameraPosition: CameraPosition (
@@ -44,25 +53,117 @@ class MapControlBloc extends Bloc<MapControlEvent, MapControlState> {
4453 tilt: event.tilt,
4554 ),
4655 mapType: this .mapType,
47- onMapCreated: (GoogleMapController newController) {
56+ onMapCreated: (GoogleMapController newController) async {
4857 this .controller = newController;
58+ this .bounds = await controller.getVisibleRegion ();
59+
60+ if (await Geolocator ().isLocationServiceEnabled ()) {
61+ this .add (CenterMapEvent ());
62+ }
63+ created = true ;
64+ },
65+ markers: this .markers,
66+ mapToolbarEnabled: false ,
67+ onCameraIdle: () async {
68+ if (created) {
69+ LatLngBounds newBounds = await controller.getVisibleRegion ();
70+ bool changed =
71+ newBounds.northeast.latitude != bounds.northeast.latitude;
72+ print ("${DateTime .now ()} $changed " );
73+ if (changed) {
74+ bounds = newBounds;
75+ print ("${DateTime .now ()} Sending Event" );
76+ this .add (UpdateBoxMarkerEvent ());
77+ }
78+ }
4979 },
5080 );
51- yield MapReadyState ();
81+ if (nestingBoxList.length == 0 ) {
82+ yield MapBuiltState ();
83+ } else {
84+ yield MapReadyState ();
85+ }
5286 }
87+
5388 if (event is CenterMapEvent ) {
54- print ('Centering' );
5589 Position position = await Geolocator ()
5690 .getCurrentPosition (desiredAccuracy: LocationAccuracy .high);
5791
58- print ('${position .latitude }, ${position .longitude }' );
5992 this .location = LatLng (position.latitude, position.longitude);
60- controller.animateCamera (CameraUpdate .newCameraPosition (CameraPosition (
61- target: this .location,
62- zoom: this .zoom,
63- tilt: this .tilt,
64- )));
65- print ('Animation finished' );
93+
94+ this .add (
95+ AddMarkerEvent (
96+ location: this .location,
97+ infoWindowText: "You are here!" ,
98+ markerId: "user" ),
99+ );
100+
101+ controller.animateCamera (
102+ CameraUpdate .newCameraPosition (
103+ CameraPosition (
104+ target: this .location,
105+ zoom: this .zoom,
106+ tilt: this .tilt,
107+ ),
108+ ),
109+ );
110+ }
111+
112+ if (event is AddMarkerEvent ) {
113+ final Marker newMarker = Marker (
114+ markerId: MarkerId (event.markerId),
115+ position: event.location,
116+ infoWindow: InfoWindow (title: event.infoWindowText),
117+ onTap: () {},
118+ );
119+
120+ if (this .markers == null ) {
121+ this .markers = Set <Marker >();
122+ }
123+
124+ this .markers.removeWhere ((marker) {
125+ return marker.markerId.value == event.markerId;
126+ });
127+ this .markers.add (newMarker);
128+ this .add (RebuildMapEvent (nestingBoxList: nestingBoxList));
129+ }
130+
131+ if (event is UpdateBoxMarkerEvent && bounds != null ) {
132+ print ("${DateTime .now ()} Receiving event (${nestingBoxList .length })" );
133+ Set <Marker > boxMarkers = Set <Marker >();
134+ this .location = LatLng (
135+ bounds.northeast.latitude -
136+ (bounds.northeast.latitude - bounds.southwest.latitude) / 2 ,
137+ bounds.northeast.longitude -
138+ (bounds.northeast.longitude - bounds.southwest.longitude) / 2 );
139+
140+ for (NestingBox box in this .nestingBoxList) {
141+ LatLng boxLoc = LatLng (box.coordinateLatitude, box.coordinateLongitude);
142+ if (bounds.contains (boxLoc)) {
143+ boxMarkers.add (
144+ Marker (
145+ markerId: MarkerId (box.id),
146+ position: LatLng (box.coordinateLatitude, box.coordinateLongitude),
147+ infoWindow: InfoWindow (title: box.id),
148+ onTap: () {},
149+ icon: BitmapDescriptor .defaultMarkerWithHue (
150+ BitmapDescriptor .hueGreen),
151+ ),
152+ );
153+ }
154+ }
155+
156+ if (this .markers == null ) {
157+ this .markers = Set <Marker >();
158+ } else {
159+ this .markers.retainWhere ((marker) {
160+ return marker.markerId.value == "user" ;
161+ });
162+ }
163+
164+ this .markers.addAll (boxMarkers);
165+
166+ this .add (RebuildMapEvent (nestingBoxList: nestingBoxList));
66167 }
67168 }
68169}
0 commit comments