summaryrefslogtreecommitdiffstats
path: root/lib/map/networkPolyline.dart
blob: 0241bec68356849d0ed611754c30bbc4576702cd (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
// SPDX-License-Identifier: Apache-2.0

import 'dart:math';
import 'package:flutter_cluster_dashboard/cluster_config.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:latlong2/latlong.dart';

class NetworkHelper {
  NetworkHelper(
      {required this.startLng,
      required this.startLat,
      required this.endLng,
      required this.endLat});

  final String url = 'https://api.openrouteservice.org/v2/directions/';

  final double startLng;
  final double startLat;
  final double endLng;
  final double endLat;

  Future getData(WidgetRef ref) async {
    final config = ref.read(clusterConfigStateprovider);
    String uriStr =
        '$url${config.orsPathParam}?api_key=${config.orsApiKey}&start=$startLng,$startLat&end=$endLng,$endLat';
    http.Response response = await http.get(Uri.parse(uriStr));

    if (response.statusCode == 200) {
      String data = response.body;
      return jsonDecode(data);
    } else {
      print("Warning: API Response Code: ${response.statusCode}");
    }
  }
}

Future getJsonData(
  WidgetRef ref,
  double startLat,
  double startLng,
  double endLat,
  double endLng,
) async {
  if (startLat == endLat && startLng == endLng) {
    return [];
  } else {
    NetworkHelper network = NetworkHelper(
      startLat: startLat,
      startLng: startLng,
      endLat: endLat,
      endLng: endLng,
    );
    try {
      final apikey = ref.read(clusterConfigStateprovider).orsApiKey;
      if (apikey.isNotEmpty) {
        var data = await network.getData(ref);
        return data['features'][0]['geometry']['coordinates'];
      }
      else {
        return [];
      }
    } catch (error) {
      print('Warning: Something Wrong with openstreet API Key !');
      return [];
    }
  }
}

double calcAngle(LatLng a, LatLng b) {
  List<double> newA = convertCoord(a);
  List<double> newB = convertCoord(b);
  double slope = (newB[1] - newA[1]) / (newB[0] - newA[0]);
  // -1 * deg + 180
  return ((atan(slope) * 180) / pi);
}

List<double> convertCoord(LatLng coord) {
  double oldLat = coord.latitude;
  double oldLong = coord.longitude;
  double newLong = (oldLong * 20037508.34 / 180);
  double newlat =
      (log(tan((90 + oldLat) * pi / 360)) / (pi / 180)) * (20037508.34 / 180);
  return [newlat, newLong];
}