summaryrefslogtreecommitdiffstats
path: root/lib/map/navigationHome.dart
blob: 5218de748089afbc1fe69503e23345dc890a1a47 (plain)
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
// SPDX-License-Identifier: Apache-2.0

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_cluster_dashboard/cluster_config.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_cluster_dashboard/map/networkPolyline.dart';
import 'package:flutter_cluster_dashboard/provider.dart';
import 'package:flutter_cluster_dashboard/vehicle_signal/vehicle_signal_provider.dart';
import 'package:latlong2/latlong.dart';

class NavigationHome extends ConsumerStatefulWidget {
  const NavigationHome({Key? key}) : super(key: key);

  @override
  ConsumerState<NavigationHome> createState() => _NavigationHomeState();
}

class _NavigationHomeState extends ConsumerState<NavigationHome> {
  late Timer timerCurrLocation;
  Timer timerPolyline = Timer.periodic(const Duration(hours: 10), ((timer) {}));
  double pathStroke = 5;
  late MapController mapController;
  // randomly initialization of the variable
  LatLng src = LatLng(31.71, 76.95);
  LatLng markerLocation = LatLng(31.71, 76.95);

  @override
  void initState() {
    super.initState();
    mapController = MapController();

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      var vehicle = ref.read(vehicleSignalProvider);
      var polylineDB = ref.read(polyLineStateProvider);
      final polylineDBNotifier = ref.read(polyLineStateProvider.notifier);
      final clusterConfig = ref.read(clusterConfigStateprovider);
      // timer for updating map center and zoom
      timerCurrLocation = Timer.periodic(const Duration(seconds: 2), (timer) {
        polylineDB = ref.read(polyLineStateProvider);

        vehicle = ref.read(vehicleSignalProvider);
        markerLocation = LatLng(vehicle.currLat, vehicle.currLng);
        // move and center
        mapController.move(LatLng(vehicle.currLat, vehicle.currLng), 15);

        // rotate
        double rotationDegree = 0;
        int n = polylineDB.currPolyLineList.length;
        if (polylineDB.currPolyLineList.isNotEmpty && n > 1) {
          rotationDegree = calcAngle(
              polylineDB.currPolyLineList[0], polylineDB.currPolyLineList[1]);

          rotationDegree = (rotationDegree.isNaN) ? 0 : rotationDegree;
        }
        // print("Rotation:$rotationDegree");
        mapController.rotate(-1 * rotationDegree);
      });

      // update polyline in polyline db
      if (polylineDB.currPolyLineList.isEmpty && clusterConfig.orsApiKey.isNotEmpty) {
        timerPolyline.cancel();
        timerPolyline =
            Timer.periodic(const Duration(seconds: 10), (timer) async {
          List data = await getJsonData(ref, vehicle.currLat, vehicle.currLng,
              vehicle.desLat, vehicle.desLng);
          List<LatLng> currList =
              data.map((element) => LatLng(element[1], element[0])).toList();
          polylineDBNotifier.update(currPolyLineList: currList);
        });
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    timerCurrLocation.cancel();
    timerPolyline.cancel();
  }

  double tempangle = 0;
  @override
  Widget build(BuildContext context) {
    final currListProvider = ref.watch(polyLineStateProvider);
    List<LatLng> currPolyLineList = currListProvider.currPolyLineList;

    return FlutterMap(
      mapController: mapController,
      options: MapOptions(
        rotation: 0,
        center: src,
        minZoom: 12,
        zoom: 12,
        maxZoom: 22.0,
        keepAlive: true,
      ),
      layers: [
        TileLayerOptions(
          maxZoom: 22,
          maxNativeZoom: 18,
          subdomains: ["a", "b", "c"],
          urlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
          userAgentPackageName: 'dev.fleaflet.flutter_map.example',
        ),
        if (currPolyLineList.isNotEmpty)
          PolylineLayerOptions(
            polylineCulling: false,
            polylines: [
              if (currPolyLineList.isNotEmpty)
                Polyline(
                  strokeWidth: pathStroke,
                  points: currPolyLineList,
                  color: Colors.blue,
                ),
            ],
          ),
        if (currPolyLineList.isNotEmpty)
          MarkerLayerOptions(
            rotate: true,
            markers: [
              Marker(
                point: markerLocation,
                width: 70,
                height: 70,
                builder: (context) => Image.asset(
                  "images/car.png",
                ),
              ),
            ],
          ),
      ],
    );
  }
}