getAddress method

Future<ReverseGeocoding?> getAddress(
  1. double latitude,
  2. double longitude, {
  3. String region = "IND",
})

Implementation

Future<ReverseGeocoding?> getAddress(double latitude, double longitude,
    // it takes in the country code. LKA, IND, BTN, BGD, NPL for Sri-Lanka, India, Bhutan, Bangladesh, Nepal respectively. Default is India (IND)
    {String region = "IND"}) async {
  ReverseGeocoding? address;
  try {
    final response = await http.get(Uri.parse(
        "$baseUrl${this._apiKey}/rev_geocode?lat=$latitude&lng=$longitude&region=$region"));

    final jsonResponseData = json.decode(response.body);
    if (500 <= response.statusCode && response.statusCode <= 599) {
      throw ClientSideException(jsonResponseData["error_description"]);
      //Mapmyindia Server Error
    } else if (400 <= response.statusCode && response.statusCode <= 499) {
      //CLIENT_CREDENTIAL_EXPIRED check your API key
      throw ClientSideException(jsonResponseData["error_description"]);
    } else if (200 <= response.statusCode && response.statusCode <= 299) {
      address = ReverseGeocoding.fromJson(json.decode(response.body));
    }

    return address;
  } on SocketException catch (_) {
    // Internet Connection not Found
    throw SocketException("Please check you Internet Connection");
  } on TimeoutException catch (_) {
    // Timeout for response
    throw TimeoutException("Internet is too slow");
  } catch (e) {
    throw Exception(e.toString());
  }
}