-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgeocoder.service.dart
41 lines (33 loc) · 1023 Bytes
/
geocoder.service.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
import 'dart:convert';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:http/http.dart' as http;
class GeocoderService {
static const String endpoint =
"https://maps.googleapis.com/maps/api/geocode/json?latlng={LAT},{LONG}&key={KEY}";
Future<Map<String, dynamic>?> fetchAddress({
required double lat,
required double long,
required Function(Object e) onError,
}) async {
String key = dotenv.env['GOOGLE_MAPS_KEY']!;
String loaded = endpoint
.replaceAll("{KEY}", key)
.replaceAll("{LAT}", lat.toString())
.replaceAll("{LONG}", long.toString());
http.Response? response;
try {
response = await http.get(Uri.parse(loaded));
} catch (e) {
onError(e);
}
if (response == null) return null;
Map<String, dynamic> json = jsonDecode(response.body);
if (json["status"] != "OK") {
onError({
"status": json["status"],
"body": json["body"].toString(),
});
}
return json;
}
}