getAddressByLatLong method
Implementation
Future<Result<dynamic, String>> getAddressByLatLong(
String lat, String lng, String keyMap) async {
Repository repository = Repository();
try {
dynamic paramBody = {
"address": "$lat,$lng",
"key": keyMap,
"sensor": false
};
Response? response = await repository.callDio(
"https://maps.googleapis.com/maps/api/geocode/json",
null,
null,
paramBody,
'get');
if (response != null && response.statusCode == 200) {
Map<String, dynamic> map = response.data;
if (map["status"] == "OK" &&
map.containsKey("results") &&
map["results"].isNotEmpty) {
if (map.length == 3) {
return Failure(map["error_message"] ?? "Falha desconhecida.");
} else {
var location = map["results"][0]["geometry"]["location"];
return Success({"lat": location["lat"], "lng": location["lng"]});
}
} else if (map["status"] != "OK" && map.containsKey("error_message")) {
return Failure(map["error_message"] ?? "Falha desconhecida.");
} else {
return const Failure("Erro inesperado na resposta da API.");
}
} else {
return Failure(
response?.statusMessage ?? "Falha na comunicação com a API.");
}
} catch (ex) {
return Failure(
"Erro ao acessar a API: ${ex.toString().replaceAll("Exception:", "")}");
}
}