42 lines
1.1 KiB
Dart
42 lines
1.1 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:latlong2/latlong.dart';
|
|
|
|
Future<String> searchPosition(LatLng position) async {
|
|
const apiKey = "AIzaSyAVUr4Ku4O1HlSkK8n9KGnUyqvsXBL-yfs";
|
|
final latitudeString = position.latitude.toString();
|
|
final longitudeString = position.longitude.toString();
|
|
const url = 'https://maps.googleapis.com/maps/api/geocode/json';
|
|
|
|
try {
|
|
final response = await Dio().get(
|
|
url,
|
|
queryParameters: {
|
|
'latlng': '$latitudeString,$longitudeString',
|
|
'key': apiKey,
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = response.data;
|
|
// Mengambil compound_code dari hasil JSON
|
|
final plusCode = data['plus_code'];
|
|
if (plusCode != null && plusCode['compound_code'] != null) {
|
|
return plusCode['compound_code'];
|
|
} else {
|
|
print('Alamat Tidak Ditemukan');
|
|
return "";
|
|
}
|
|
} else {
|
|
print('Failed to load data');
|
|
return "";
|
|
}
|
|
} catch (e) {
|
|
print('Error: $e');
|
|
return "";
|
|
}
|
|
}
|
|
|
|
Future<String> positionToAddressGoogleApis(LatLng position) async {
|
|
return await searchPosition(position);
|
|
}
|