getLatLongByAddress method

Future<NTRepository> getLatLongByAddress(
  1. String address,
  2. String keyMap
)

Implementation

Future<NTRepository> getLatLongByAddress(
    String address, String keyMap) async {
  NTRepository objReturn = NTRepository(statusId: 1, description: "Ok");
  Repository repository = Repository();

  try {
    dynamic paramBody = {"address": address, "key": keyMap};

    Response? response = await repository.callDio(
        "https://maps.googleapis.com/maps/api/geocode/json",
        null,
        null,
        paramBody,
        'get');

    if (response!.statusCode == 200) {
      Map<String, dynamic> map = response.data;
      if (map.length == 2) {
        if (map["status"] == "OK") {
          objReturn.dataValue = {
            "lat": map["results"][0]["geometry"]["location"]["lat"],
            "lng": map["results"][0]["geometry"]["location"]["lng"]
          };
        }
      } else if (map.length == 3) {
        throw Exception(map["error_message"]);
      }
    } else {
      objReturn.statusId = response.statusCode;
      objReturn.description = response.statusMessage;
    }
  } catch (ex) {
    objReturn.statusId = objReturn.statusId == 0 || objReturn.statusId == 1
        ? 99
        : objReturn.statusId;
    objReturn.description = ex.toString().replaceAll("Exception:", "");
  }

  return objReturn;
}