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

import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter_navigation/config.dart';

import 'package:http/http.dart' as http;
import 'package:latlong2/latlong.dart';



String baseUrl = 'https://api.mapbox.com/directions/v5/mapbox';
String navType = 'driving';

Dio _dio = Dio();

Future getdrivingRouteUsingMapbox(LatLng source, LatLng destination,ref) async {
  final config = ref.read(ConfigStateprovider);

  String url =
      '$baseUrl/$navType/${source.longitude},${source.latitude};${destination.longitude},${destination.latitude}?alternatives=true&continue_straight=true&geometries=geojson&language=en&overview=full&steps=true&access_token=${config.mapboxAccessToken}';
  try {
    _dio.options.contentType = Headers.jsonContentType;
    final responseData = await _dio.get(url);
    return responseData.data;
  } catch (e) {
    print(e);
  }
}

Future<Map> getDirectionsAPIResponse(LatLng currentLatLng,LatLng destiLatLng,ref) async {

  final response = await getdrivingRouteUsingMapbox(currentLatLng, destiLatLng,ref);

  if(response != null){
    Map geometry = response['routes'][0]['geometry'];
    num duration = response['routes'][0]['duration'];
    num distance = response['routes'][0]['distance'];
    Map legs = response['routes'][0]['legs'][0];

    Map modifiedResponse = {
      "geometry": geometry,
      "duration": duration,
      "distance": distance,
      "legs" : legs,
    };
    return modifiedResponse;

  }
  else{
    Map map = {};
    return map;
  }

}

Future<Map> getAdress(LatLng pos,ref) async{
  final config = ref.read(ConfigStateprovider);

  var url = 'https://api.mapbox.com/geocoding/v5/mapbox.places/${pos.longitude},${pos.latitude}.json?&access_token=${config.mapboxAccessToken}';
  http.Response response = await http.get(Uri.parse(url));
  Map data = json.decode(response.body);
  return data;
}