flutter_google_track_and_trace/example/lib/main.dart

98 lines
2.6 KiB
Dart
Raw Normal View History

2021-09-29 10:07:56 +02:00
import 'dart:async';
import 'dart:math';
2021-09-28 08:18:14 +02:00
import 'package:flutter/material.dart';
import 'package:google_track_trace/google_track_trace.dart';
class TrackTraceDemo extends StatefulWidget {
2021-09-28 10:37:48 +02:00
const TrackTraceDemo({Key? key}) : super(key: key);
2021-09-28 08:18:14 +02:00
@override
State<TrackTraceDemo> createState() => _TrackTraceDemoState();
}
class _TrackTraceDemoState extends State<TrackTraceDemo> {
2021-09-29 10:07:56 +02:00
TrackTraceController? controller;
@override
void initState() {
2021-09-29 13:42:13 +02:00
Timer.periodic(const Duration(seconds: 5), (_) {
2021-09-29 12:18:53 +02:00
moveAlongRoute();
2021-09-29 10:07:56 +02:00
});
super.initState();
}
2021-09-28 08:18:14 +02:00
@override
Widget build(BuildContext context) {
return Scaffold(
2021-09-29 10:07:56 +02:00
appBar: AppBar(
2021-09-30 10:09:57 +02:00
title: (controller == null || controller!.route == null)
? const Text('TrackTrace example')
: Text(
'${controller!.route!.duration} seconds, afstand: '
'${controller!.route!.distance / 1000} km',
),
),
2021-09-28 08:18:14 +02:00
body: GoogleTrackTraceMap(
startPosition: const Marker(
markerId: MarkerId('Start locatie'),
2021-09-29 12:18:53 +02:00
position: LatLng(52.356057, 4.897540),
2021-09-28 08:18:14 +02:00
),
destinationPosition: const Marker(
2021-09-30 10:09:57 +02:00
markerId: MarkerId('Bestemming Locatie'),
position: LatLng(52.364709, 4.877157),
),
2021-09-29 10:07:56 +02:00
googleAPIKey: 'AIzaSyDaxZX8TeQeVf5tW-D6A66WLl20arbWV6c',
2021-09-29 12:18:53 +02:00
travelMode: TravelMode.bicycling,
2021-09-30 10:09:57 +02:00
mapType: MapType.satellite,
2021-09-29 10:07:56 +02:00
routeUpdateInterval: 60,
timerPrecision: TimePrecision.everySecond,
zoomGesturesEnabled: true,
2021-09-29 12:18:53 +02:00
line: const Polyline(
polylineId: PolylineId('test route'),
color: Colors.purple,
2021-09-29 13:42:13 +02:00
width: 5,
2021-09-29 12:18:53 +02:00
),
2021-09-29 10:07:56 +02:00
onMapCreated: (ctr) => {
controller = ctr,
ctr.addListener(() {
setState(() {});
}),
},
2021-09-28 08:18:14 +02:00
),
);
}
2021-09-29 10:07:56 +02:00
void getRandomPointOnMap() {
2021-09-29 12:18:53 +02:00
// 51.989909, 6.234950 NW
// 51.939909, 6.314950 SE
2021-09-29 10:07:56 +02:00
if (controller != null) {
2021-09-29 12:18:53 +02:00
controller!.start = Marker(
2021-09-30 10:09:57 +02:00
markerId: const MarkerId('Start Locatie'),
position: LatLng(
51.93 + Random().nextDouble() * 0.06,
6.23 + Random().nextDouble() * 0.08,
),
);
2021-09-29 10:07:56 +02:00
}
}
2021-09-29 12:18:53 +02:00
void moveAlongRoute() {
2021-09-30 10:09:57 +02:00
if (controller != null &&
controller!.route != null &&
controller!.route!.line.length > 1) {
controller!.start = Marker(
markerId: const MarkerId('Start Locatie'),
position: LatLng(
controller!.route!.line[1].latitude,
controller!.route!.line[1].longitude,
),
);
2021-09-29 10:07:56 +02:00
}
}
2021-09-28 08:18:14 +02:00
}
void main() {
2021-09-29 10:07:56 +02:00
runApp(const MaterialApp(home: TrackTraceDemo()));
2021-09-28 08:18:14 +02:00
}