flutter_google_track_and_trace/example/lib/main.dart

85 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_maps_flutter/google_maps_flutter.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() {
Timer.periodic(const Duration(seconds: 10), (_) {
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-29 12:18:53 +02:00
title: (controller == null || controller!.route == null)
2021-09-29 10:07:56 +02:00
? const Text('TrackTrace example')
2021-09-29 12:18:53 +02:00
: Text(controller!.route!.duration.toString() +
' seconds, afstand: ' +
(controller!.route!.distance / 1000).toString() +
' 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-29 12:18:53 +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-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,
width: 7,
),
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(
markerId: const MarkerId('Start Locatie'),
2021-09-29 10:07:56 +02:00
position: LatLng(51.93 + Random().nextDouble() * 0.06,
6.23 + Random().nextDouble() * 0.08));
}
}
2021-09-29 12:18:53 +02:00
void moveAlongRoute() {
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
}