-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaps_vm.dart
More file actions
221 lines (180 loc) · 6.16 KB
/
maps_vm.dart
File metadata and controls
221 lines (180 loc) · 6.16 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import 'dart:async';
import 'package:devfest/core/router/navigator.dart';
import 'package:flutter/material.dart';
import 'package:devfest/views/maps_page/maps_search_page.dart';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:devfest/utils/constants.dart';
import 'package:google_place/google_place.dart' as places;
import 'package:location/location.dart';
class MapsVM extends ChangeNotifier {
TextEditingController _your_location_ctrl = TextEditingController();
TextEditingController get yourlocation_ctrl => this._your_location_ctrl;
set yourlocation_ctrl(TextEditingController value) {
this._your_location_ctrl = value;
notifyListeners();
}
TextEditingController _landmark_ctrl = TextEditingController();
TextEditingController get landmarkctrl => this._landmark_ctrl;
set landmarkctrl(TextEditingController value) {
this._landmark_ctrl = value;
notifyListeners();
}
final Completer<GoogleMapController> _controller = Completer();
late GoogleMapController _mapController;
get mapController => this._mapController;
set mapController(value) {
this._mapController = value;
notifyListeners();
}
late places.GooglePlace googlePlace;
get getGooglePlace => this.googlePlace;
void setGooglePlace(googlePlace) {
this.googlePlace = googlePlace;
//notifyListeners();
}
List<LatLng> _polylineCoordinates = [];
List<LatLng> get getPolylineCoordinates => this._polylineCoordinates;
set setPolylineCoordinates(List<LatLng> polylineCoordinates) {
this._polylineCoordinates = polylineCoordinates;
notifyListeners();
}
var _currentLocation;
get currentLocation => this._currentLocation;
set currentLocation(var value) {
this._currentLocation = value;
notifyListeners();
}
//LatLng source = const LatLng(6.5355, 3.3087);
LatLng destination =
const LatLng(6.4230432, 3.4450862); //Landmark event center.
LocationData? _curentLocation;
get getCurentLocation => this._curentLocation;
set setCurentLocation(curentLocation) {
this._curentLocation = curentLocation;
notifyListeners();
}
List<places.AutocompletePrediction> predictions = [];
Timer? _debounce;
places.DetailsResult? startPosition;
bool _directionsClicked = false;
get getDirectionsClicked => _directionsClicked;
set setDirectionsClicked(clicked) {
_directionsClicked = clicked;
notifyListeners();
}
void toggleDirections() {
setDirectionsClicked = !getDirectionsClicked;
notifyListeners();
}
late FocusNode _startFocusNode;
get getStartFocusNode => _startFocusNode;
void setStartFocusNode(focusNode) {
_startFocusNode = focusNode;
}
void getCurrentLocation() async {
Location location = Location();
location.getLocation().then((location) => setCurentLocation = location);
GoogleMapController googleMapController = await _controller.future;
location.onLocationChanged.listen((newLocation) {
setCurentLocation = newLocation;
// googleMapController.animateCamera(
// CameraUpdate.newCameraPosition(
// CameraPosition(
// zoom: 13.5,
// target: LatLng(newLocation.latitude!, newLocation.longitude!),
// ),
// ),
// );
notifyListeners();
});
}
void getStartLocation() async {
GoogleMapController googleMapController = await _controller.future;
googleMapController.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
zoom: 13.5,
target: LatLng(startPosition!.geometry!.location!.lat!,
startPosition!.geometry!.location!.lng!),
),
),
);
notifyListeners();
}
void getPolyPoint() async {
PolylinePoints polylinePoints = PolylinePoints();
PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
AppConstants.google_api_key,
PointLatLng(startPosition!.geometry!.location!.lat!,
startPosition!.geometry!.location!.lng!),
//PointLatLng(source.latitude, source.longitude),
PointLatLng(destination.latitude, destination.longitude),
);
if (result.points.isNotEmpty) {
result.points.forEach(
(PointLatLng point) => _polylineCoordinates.add(
LatLng(point.latitude, point.longitude),
),
);
notifyListeners();
}
}
// setGooglePlace(places.GooglePlace(AppConstants.google_api_key));
void initializePlaces() {
//calling setGoogle place alone threw an error, so I used mapController to call it.
setGooglePlace(places.GooglePlace(AppConstants.google_api_key));
}
void initializeFocusNode(focusnode) {
setStartFocusNode(focusnode);
}
void onMapCreated(GoogleMapController mapcontroller) {
_controller.complete(mapcontroller);
}
void autoCompleteSearch(String value) async {
var result = await googlePlace.autocomplete.get(value);
if (result != null && result.predictions != null) {
print(result.predictions!.first.description);
predictions = result.predictions!;
notifyListeners();
}
}
void onChanged(value) {
if (_debounce?.isActive ?? false) _debounce!.cancel();
_debounce = Timer(const Duration(milliseconds: 1000), () {
if (value.isNotEmpty) {
//places api
autoCompleteSearch(value);
} else {
//clear out the resut
predictions = [];
startPosition = null;
notifyListeners();
}
});
}
Future onTap(index) async {
final placeId = predictions[index].placeId!;
final details = await googlePlace.details.get(placeId);
if (details != null && details.result != null) {
if (getStartFocusNode.hasFocus) {
startPosition = details.result;
yourlocation_ctrl.text = details.result!.name!;
//clear predictions after user selection
predictions = [];
notifyListeners();
}
//GO BACK
if (startPosition != null) {
getPolyPoint();
AppNavigator.pop();
}
}
}
void clearSearchField() {
predictions = [];
yourlocation_ctrl.clear();
notifyListeners();
}
}
//void setGooglePlace(places.GooglePlace googlePlace) {}