reverseSearch static method

Future<Place> reverseSearch({
  1. double? lat,
  2. double? lon,
  3. String? osmType,
  4. int? osmId,
  5. bool addressDetails = false,
  6. bool extraTags = false,
  7. bool nameDetails = false,
  8. String? language,
  9. int zoom = 18,
})

Searches for a place by it's coordinates or OSM object

Use either lat and lon or osmType and osmId, but don't combine them.

Using addressDetails will include a breakdown of the address into elements. Default is false.

Using extraTags will include additional information in the result if available, e.g. wikipedia link, opening hours. Default is false.

Using nameDetails will include a list of alternative names in the results. These may include language variants, references, operator and brand. Default is false.

Using language will set the preferred language order for showing search results, overrides the value specified in the Accept-Language HTTP header if you are running in a browser. Either use a standard RFC2616 accept-language string or a simple comma-separated list of language codes.

Using zoom will set the level of detail required for the address. This is a number that corresponds roughly to the zoom level used in map frameworks like Leaflet.js, Openlayers etc. In terms of address details the zoom levels are as follows:

zoom address detail
3 country
5 state
8 county
10 city
14 suburb
16 major streets
17 major and minor streets
18 building

Implementation

static Future<Place> reverseSearch({
  double? lat,
  double? lon,
  String? osmType,
  int? osmId,
  bool addressDetails = false,
  bool extraTags = false,
  bool nameDetails = false,
  String? language,
  int zoom = 18,
}) async {
  final notNullParameters =
      [lat, lon, osmType, osmId].where((e) => e != null).length;
  assert(
    notNullParameters == 2,
    'Either provide lat and lon or osmType and osmId',
  );
  assert(
    (lat != null && lon != null && osmType == null && osmId == null) ||
        (lat == null && lon == null && osmType != null && osmId != null),
    'Do not mix coordinates and OSM object',
  );
  assert(
    ['N', 'W', 'R', null].contains(osmType),
    'osmType needs to be one of N, W, R',
  );
  assert(
    zoom >= 0 && zoom <= 18,
    'Zoom needs to be between 0 and 18',
  );
  final uri = Uri.https(
    'nominatim.openstreetmap.org',
    '/reverse',
    {
      'format': 'jsonv2',
      if (lat != null) 'lat': lat.toString(),
      if (lon != null) 'lon': lon.toString(),
      if (osmType != null) 'osm_type': osmType,
      if (osmId != null) 'osm_id': osmId.toString(),
      if (addressDetails) 'addressdetails': '1',
      if (extraTags) 'extratags': '1',
      if (nameDetails) 'namedetails': '1',
      if (language != null) 'accept-language': language,
    },
  );
  final response = await http.get(uri);
  final data = json.decode(response.body) as Map<String, dynamic>;
  if (data['error'] != null) {
    throw Exception(data['error']);
  }
  return Place.fromJson(data);
}