reverseGeocodeLatLng method

Future reverseGeocodeLatLng(
  1. LatLng latLng
)

This method gets the human readable name of the location. Mostly appears to be the road name and the locality.

Implementation

Future reverseGeocodeLatLng(LatLng latLng) async {
  final endpoint =
      "https://maps.googleapis.com/maps/api/geocode/json?latlng=${latLng.latitude},${latLng.longitude}"
      "&key=${widget.apiKey}"
      "&language=${widget.language}";

  final response = await http.get(
    Uri.parse(endpoint),
  );

  if (response.statusCode == 200) {
    Map<String, dynamic> responseJson = jsonDecode(response.body);

    String road;

    String placeId = responseJson['results'][0]['place_id'];

    if (responseJson['status'] == 'REQUEST_DENIED') {
      road = 'REQUEST DENIED = please see log for more details';
      logger.e(responseJson['error_message']);
    } else {
      road =
          responseJson['results'][0]['address_components'][0]['short_name'];
    }

//      String locality =
//          responseJson['results'][0]['address_components'][1]['short_name'];

    setState(() {
      locationResult = LocationResult();
      locationResult?.address = road;
      locationResult?.latLng = latLng;
      locationResult?.placeId = placeId;
    });
  }
}