-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocation_services.dart
68 lines (57 loc) · 2.38 KB
/
location_services.dart
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
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;
class LocationService {
final String key = 'API-KEY';
// currently not being used, this function is just for reference
Future<Map<String, dynamic>> getPlaceID(String input) async {
const String url =
'https://maps.googleapis.com/maps/api/place/details/output?parameters'; // example url
var response = await http.get(Uri.parse(url));
var json = convert.jsonDecode(response.body);
var results = json['result'] as Map<String, dynamic>;
return results;
}
Future<Map> getDirections(String origin, String destination) async {
final String url =
'https://maps.googleapis.com/maps/api/directions/json?origin=${origin}&destination=${destination}&key=${key}';
var response = await http.get(Uri.parse(url));
var json = convert.jsonDecode(response.body);
var results = {
'bounds_ne': json['routes'][0]['bounds']['northeast'],
'bounds_sw': json['routes'][0]['bounds']['southwest'],
'start_location': json['routes'][0]['legs'][0]['start_location'],
'end_location': json['routes'][0]['legs'][0]['end_location'],
'polyline': json['routes'][0]['overview_polyline']['points'],
'distanceInMiles': json['routes'][0]['legs'][0]['distance']['text'],
'durationToReachDestination': json['routes'][0]['legs'][0]['duration']
['text'],
'polyline_decoded': PolylinePoints()
.decodePolyline(json['routes'][0]['overview_polyline']['points']),
};
return results;
}
Future<Map> getSearchResults(String input) async {
final String url =
'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=${input}&key=${key}';
var response = await http.get(Uri.parse(url));
var json = convert.jsonDecode(response.body);
var results = {
'length': json['predictions'].length,
'results': json['predictions']
};
return results;
}
Future<String>? getDistanceInMiles(var directions) async {
if (directions['distanceInMiles'] == "") {
return "0 mi";
}
return directions['distanceInMiles'];
}
Future<String>? getDurationToReachDestination(var directions) async {
if (directions['durationToReachDestination'] == "") {
return "0 mi";
}
return directions['durationToReachDestination'];
}
}