getAddress method

Future<FMData?> getAddress({
  1. required double lat,
  2. required double lng,
  3. int maxRetries = 3,
  4. dynamic onError(
    1. Object,
    2. StackTrace
    )?,
})

Get address from coordinates (Reverse Geocoding)

Implementation

Future<FMData?> getAddress({
  required double lat,
  required double lng,
  int maxRetries = 3,
  Function(Object, StackTrace)? onError,
}) async {
  assert(maxRetries <= 5, 'Keep maxRetries less than or equal to 5');
  try {
    final query = {'lat': lat, 'lon': lng, 'zoom': 18, 'format': 'jsonv2'};
    String url = _baseURL;
    url += _reverseEndpoint;
    url += '?${query.keys.map((k) => '$k=${query[k]}').join('&')}';

    final res = await http.get(Uri.parse(url));
    return FMRawData.fromJSON(jsonDecode(res.body)).data;
  } catch (e, st) {
    if (maxRetries > 0) {
      return getAddress(lat: lat, lng: lng, maxRetries: --maxRetries);
    }
    if (onError != null) onError(e, st);
    return null;
  }
}