-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
executable file
·400 lines (377 loc) · 14.9 KB
/
main.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import 'dart:developer';
import 'dart:math' hide log;
import 'package:flutter_svg/flutter_svg.dart';
import 'package:geolocator/geolocator.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:vietmap_flutter_navigation/vietmap_flutter_navigation.dart';
import 'components/bottom_sheet_address_info.dart';
import 'components/floating_search_bar.dart';
import 'data/models/vietmap_place_model.dart';
import 'data/models/vietmap_reverse_model.dart';
import 'domain/repository/vietmap_api_repositories.dart';
import 'domain/usecase/get_location_from_latlng_usecase.dart';
import 'domain/usecase/get_place_detail_usecase.dart';
void main() {
runApp(MaterialApp(
builder: EasyLoading.init(),
title: 'VietMap Navigation example app',
home: const VietMapNavigationScreen(),
debugShowCheckedModeBanner: false,
));
}
class VietMapNavigationScreen extends StatefulWidget {
const VietMapNavigationScreen({super.key});
@override
State<VietMapNavigationScreen> createState() =>
_VietMapNavigationScreenState();
}
class _VietMapNavigationScreenState extends State<VietMapNavigationScreen> {
MapNavigationViewController? _controller;
late MapOptions _navigationOption;
final _vietmapNavigationPlugin = VietMapNavigationPlugin();
List<LatLng> waypoints = [
const LatLng(10.759091, 106.675817),
const LatLng(10.762528, 106.653099)
];
Widget instructionImage = const SizedBox.shrink();
String guideDirection = "";
Widget recenterButton = const SizedBox.shrink();
RouteProgressEvent? routeProgressEvent;
bool _isRouteBuilt = false;
bool _isRunning = false;
FocusNode focusNode = FocusNode();
@override
void initState() {
super.initState();
initialize();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
Geolocator.requestPermission();
});
}
Future<void> initialize() async {
if (!mounted) return;
_navigationOption = _vietmapNavigationPlugin.getDefaultOptions();
_navigationOption.simulateRoute = false;
_navigationOption.apiKey = 'YOUR_API_KEY_HERE';
_navigationOption.mapStyle =
"https://maps.vietmap.vn/api/maps/light/styles.json?apikey=YOUR_API_KEY_HERE";
_vietmapNavigationPlugin.setDefaultOptions(_navigationOption);
}
MapOptions? options;
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
onPressed: () {
_controller?.moveCamera(
latLng: const LatLng(22.762528, 106.653099),
zoom: 8,
tilt: 0,
bearing: 0);
},
child: const Icon(Icons.animation),
),
// AbsorbPointer()
FloatingActionButton(
onPressed: () async {
await _controller?.addImageMarkers([
NavigationMarker(
width: 120,
height: 120,
imagePath: 'assets/50.png',
latLng: const LatLng(10.762528, 106.653099)),
NavigationMarker(
imagePath: 'assets/40.png',
latLng: const LatLng(10.762528, 106.753099),
width: 80,
height: 80),
]);
},
child: const Icon(Icons.animation),
),
],
),
body: SafeArea(
top: false,
child: Stack(
children: [
NavigationView(
onRouteBuildFailed: (p0) {
EasyLoading.dismiss();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Không thể tìm tuyến đường')));
},
onMarkerClicked: (p0) {
debugPrint(p0.toString());
log("marker clicked");
_controller?.removeMarkers([p0 ?? 0]);
},
mapOptions: _navigationOption,
onNewRouteSelected: (p0) {
log(p0.toString());
},
onMapCreated: (p0) {
_controller = p0;
},
onMapMove: () => _showRecenterButton(),
onRouteBuilt: (p0) {
setState(() {
EasyLoading.dismiss();
_isRouteBuilt = true;
});
debugPrint(p0.geometry);
},
onMapRendered: () async {},
onMapLongClick: (LatLng? latLng, Point? point) async {
if (_isRunning) return;
EasyLoading.show();
var data =
await GetLocationFromLatLngUseCase(VietmapApiRepositories())
.call(LocationPoint(
lat: latLng?.latitude ?? 0,
long: latLng?.longitude ?? 0));
EasyLoading.dismiss();
data.fold((l) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Có lỗi xảy ra')));
}, (r) => _showBottomSheetInfo(r));
},
onMapClick: (LatLng? latLng, Point? point) async {
if (_isRunning) return;
if (focusNode.hasFocus) {
FocusScope.of(context).requestFocus(FocusNode());
return;
}
var dataQuery = await _controller?.queryRenderedFeatures(
point: Point(
point?.x.toDouble() ?? 0, point?.y.toDouble() ?? 0));
log(dataQuery.toString());
if (dataQuery?.isNotEmpty == true) {}
EasyLoading.show();
var data =
await GetLocationFromLatLngUseCase(VietmapApiRepositories())
.call(LocationPoint(
lat: latLng?.latitude ?? 0,
long: latLng?.longitude ?? 0));
EasyLoading.dismiss();
data.fold((l) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content:
Text('Không tìm thấy địa điểm gần vị trí bạn chọn')));
}, (r) => _showBottomSheetInfo(r));
},
onRouteProgressChange: (RouteProgressEvent routeProgressEvent) {
// print('-----------ProgressChange----------');
// print(routeProgressEvent.currentLocation?.bearing);
// print(routeProgressEvent.currentLocation?.altitude);
// print(routeProgressEvent.currentLocation?.accuracy);
// print(routeProgressEvent.currentLocation?.bearing);
// print(routeProgressEvent.currentLocation?.latitude);
// print(routeProgressEvent.currentLocation?.longitude);
setState(() {
this.routeProgressEvent = routeProgressEvent;
});
_setInstructionImage(routeProgressEvent.currentModifier,
routeProgressEvent.currentModifierType);
},
onArrival: () {
_isRunning = false;
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Container(
height: 100,
color: Colors.red,
child: const Text('Bạn đã tới đích'))));
},
),
Positioned(
top: MediaQuery.of(context).viewPadding.top,
left: 0,
child: BannerInstructionView(
routeProgressEvent: routeProgressEvent,
instructionIcon: instructionImage,
)),
Positioned(
bottom: 0,
child: BottomActionView(
recenterButton: recenterButton,
controller: _controller,
onOverviewCallback: _showRecenterButton,
onStopNavigationCallback: _onStopNavigation,
routeProgressEvent: routeProgressEvent,
)),
_isRunning
? const SizedBox.shrink()
: Positioned(
top: MediaQuery.of(context).viewPadding.top + 20,
child: FloatingSearchBar(
focusNode: focusNode,
onSearchItemClick: (p0) async {
EasyLoading.show();
VietmapPlaceModel? data;
var res = await GetPlaceDetailUseCase(
VietmapApiRepositories())
.call(p0.refId ?? '');
res.fold((l) {
EasyLoading.dismiss();
return;
}, (r) {
data = r;
});
waypoints.clear();
var location = await Geolocator.getCurrentPosition();
waypoints
.add(LatLng(location.latitude, location.longitude));
if (data?.lat != null) {
waypoints.add(LatLng(data?.lat ?? 0, data?.lng ?? 0));
}
_controller?.buildRoute(waypoints: waypoints);
},
)),
_isRouteBuilt && !_isRunning
? Positioned(
bottom: 20,
left: 0,
child: SizedBox(
width: MediaQuery.of(context).size.width,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
style: ButtonStyle(
shape: WidgetStateProperty.all<
RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(18.0),
side: const BorderSide(
color: Colors.blue)))),
onPressed: () {
_isRunning = true;
_controller?.startNavigation();
},
child: const Text('Bắt đầu')),
ElevatedButton(
style: ButtonStyle(
shape: WidgetStateProperty.all<
RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(18.0),
side: const BorderSide(
color: Colors.blue)))),
onPressed: () {
_controller?.clearRoute();
setState(() {
_isRouteBuilt = false;
});
},
child: const Text('Xoá tuyến đường')),
],
),
),
)
: const SizedBox.shrink(),
],
),
),
);
}
_showRecenterButton() {
recenterButton = TextButton(
onPressed: () {
_controller?.recenter();
setState(() {
recenterButton = const SizedBox.shrink();
});
},
child: Container(
height: 50,
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
color: Colors.white,
border: Border.all(color: Colors.black45, width: 1)),
child: const Row(
children: [
Icon(
Icons.keyboard_double_arrow_up_sharp,
color: Colors.lightBlue,
size: 35,
),
Text(
'Về giữa',
style: TextStyle(fontSize: 18, color: Colors.lightBlue),
)
],
)));
setState(() {});
}
_setInstructionImage(String? modifier, String? type) {
if (modifier != null && type != null) {
List<String> data = [
type.replaceAll(' ', '_'),
modifier.replaceAll(' ', '_')
];
String path = 'assets/navigation_symbol/${data.join('_')}.svg';
setState(() {
instructionImage = SvgPicture.asset(path, color: Colors.white);
});
}
}
_onStopNavigation() {
setState(() {
routeProgressEvent = null;
_isRunning = false;
});
}
_showBottomSheetInfo(VietmapReverseModel data) {
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (_) => AddressInfo(
data: data,
buildRoute: () async {
EasyLoading.show();
waypoints.clear();
var location = await Geolocator.getCurrentPosition();
waypoints.add(LatLng(location.latitude, location.longitude));
if (data.lat != null) {
waypoints.add(LatLng(
data.lat?.toDouble() ?? 0, data.lng?.toDouble() ?? 0));
}
_controller?.buildRoute(waypoints: waypoints);
if (!mounted) return;
Navigator.pop(context);
},
buildAndStartRoute: () async {
EasyLoading.show();
waypoints.clear();
var location = await Geolocator.getCurrentPosition();
waypoints.add(LatLng(location.latitude, location.longitude));
if (data.lat != null) {
waypoints.add(LatLng(
data.lat?.toDouble() ?? 0, data.lng?.toDouble() ?? 0));
}
_controller?.buildAndStartNavigation(
waypoints: waypoints,
profile: DrivingProfile.drivingTraffic);
setState(() {
_isRunning = true;
});
if (!mounted) return;
Navigator.pop(context);
},
));
}
@override
void dispose() {
_controller?.onDispose();
super.dispose();
}
}