flutter_google_track_and_trace/example/lib/main.dart

196 lines
5.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';
2021-10-05 21:09:19 +02:00
import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter_google_track_and_trace/google_track_trace.dart';
2021-09-28 08:18:14 +02:00
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;
2021-10-01 15:00:23 +02:00
int step = 1;
int routeLength = 0;
late final Timer timer;
2021-10-05 21:09:19 +02:00
BitmapDescriptor? startMarkerIcon;
BitmapDescriptor? destinationMarkerIcon;
2021-09-29 10:07:56 +02:00
@override
void initState() {
2021-10-05 21:09:19 +02:00
loadBitmapImages();
2021-10-01 15:00:23 +02:00
timer = Timer.periodic(const Duration(seconds: 2), (_) {
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(
2021-09-30 12:34:43 +02:00
mapStylingTheme: GoogleTrackTraceMapTheme(
themes: [
2021-10-05 21:09:19 +02:00
GoogleMapThemeFeature(
featureType: 'all',
stylers: [
{'saturation': '-50'},
//{'invert_lightness': 'true'},
],
),
GoogleMapThemeFeature(
featureType: 'landscape.natural.landcover',
stylers: [
{'color': '#00ff00'},
],
),
2021-09-30 12:34:43 +02:00
GoogleMapThemeFeature(
featureType: 'poi',
stylers: [
{'visibility': 'off'},
],
),
2021-10-05 21:09:19 +02:00
GoogleMapThemeFeature(
featureType: 'poi.park',
stylers: [
{'visibility': 'on'},
],
),
2021-09-30 12:34:43 +02:00
GoogleMapThemeFeature(
featureType: 'transit',
stylers: [
{'visibility': 'off'},
],
),
],
),
2021-10-05 21:09:19 +02:00
startPosition: Marker(
2021-09-28 08:18:14 +02:00
markerId: MarkerId('Start locatie'),
2021-10-05 21:09:19 +02:00
anchor: Offset(0.5, 0.5),
2021-09-29 12:18:53 +02:00
position: LatLng(52.356057, 4.897540),
2021-10-05 21:09:19 +02:00
icon: startMarkerIcon ?? BitmapDescriptor.defaultMarker,
2021-09-28 08:18:14 +02:00
),
2021-10-05 21:09:19 +02:00
destinationPosition: Marker(
2021-09-30 10:09:57 +02:00
markerId: MarkerId('Bestemming Locatie'),
2021-10-05 21:09:19 +02:00
anchor: Offset(0.5, 0.5),
2021-09-30 10:09:57 +02:00
position: LatLng(52.364709, 4.877157),
2021-10-05 21:09:19 +02:00
icon: destinationMarkerIcon ?? BitmapDescriptor.defaultMarker,
2021-09-30 10:09:57 +02:00
),
2021-10-05 21:09:19 +02:00
buildingsEnabled: false,
2021-09-29 10:07:56 +02:00
googleAPIKey: 'AIzaSyDaxZX8TeQeVf5tW-D6A66WLl20arbWV6c',
2021-09-30 12:34:43 +02:00
travelMode: TravelMode.walking,
mapType: MapType.normal,
2021-10-05 21:09:19 +02:00
indoorViewEnabled: false,
2021-10-01 15:00:23 +02:00
routeUpdateInterval: Duration(seconds: 30),
2021-09-29 10:07:56 +02:00
timerPrecision: TimePrecision.everySecond,
zoomGesturesEnabled: true,
2021-10-01 15:00:23 +02:00
scrollGesturesEnabled: true,
2021-09-29 12:18:53 +02:00
line: const Polyline(
2021-10-01 15:00:23 +02:00
jointType: JointType.bevel,
2021-09-29 12:18:53 +02:00
polylineId: PolylineId('test route'),
2021-09-30 12:34:43 +02:00
color: Color(0xFFFF7884),
width: 3,
2021-09-29 12:18:53 +02:00
),
2021-10-01 15:00:23 +02:00
onArrived: () {
timer.cancel();
debugPrint('stopping simulation');
},
onTap: (value) {
debugPrint(value.toString());
},
onLongPress: (value) {
debugPrint(value.toString());
},
onCameraMove: (value) {
debugPrint(value.toString());
},
2021-09-29 10:07:56 +02:00
onMapCreated: (ctr) => {
controller = ctr,
ctr.addListener(() {
2021-10-01 15:00:23 +02:00
setState(() {
if (ctr.route != null && ctr.route!.distance != routeLength) {
step = 1;
}
});
2021-09-29 10:07:56 +02:00
}),
},
2021-09-28 08:18:14 +02:00
),
);
}
2021-09-29 10:07:56 +02:00
Future<void> loadBitmapImages() async {
var loadedPicture = await rootBundle.load('assets/profile_picture.png');
var bitmap = await convertBytesToCustomBitmapDescriptor(
loadedPicture.buffer.asUint8List(),
size: 80,
addBorder: true,
borderColor: Colors.grey,
title: 'Alex',
titleBackgroundColor: Color(0xffff7884),
);
startMarkerIcon = bitmap;
var bitmapDescriptor = await BitmapDescriptor.fromAssetImage(
ImageConfiguration(size: Size(100, 100)),
'assets/ic_location_on.png',
);
setState(() {
destinationMarkerIcon = bitmapDescriptor;
controller?.end = Marker(
anchor: Offset(0.5, 0.5),
markerId: MarkerId('Bestemming Locatie'),
position: LatLng(52.364709, 4.877157),
icon: bitmapDescriptor,
);
2021-10-05 21:09:19 +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'),
2021-10-05 21:09:19 +02:00
anchor: Offset(0.5, 0.5),
2021-09-30 10:09:57 +02:00
position: LatLng(
2021-10-01 15:00:23 +02:00
controller!.route!.line[step].latitude,
controller!.route!.line[step].longitude,
2021-09-30 10:09:57 +02:00
),
2021-10-05 21:09:19 +02:00
icon: startMarkerIcon ?? BitmapDescriptor.defaultMarker,
2021-09-30 10:09:57 +02:00
);
2021-10-01 15:00:23 +02:00
step++;
routeLength = controller!.route!.distance;
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
}