getAddressFromLatLng method

Future<void> getAddressFromLatLng()

Implementation

Future<void> getAddressFromLatLng() async {
  try {
    // Get the current position (latitude and longitude)
    Position position = await Geolocator.getCurrentPosition(
      desiredAccuracy: LocationAccuracy.high,
    );

    // Reverse geocode the coordinates to get the address
    List<Placemark> placemarks = await placemarkFromCoordinates(
      position.latitude,
      position.longitude,
    );

    // Get the first address from the list
    Placemark placemark = placemarks.first;

    setState(() {
      lat = '${position.latitude}';
      long = '${position.longitude}';
      address =
          '${placemark.street}, ${placemark.locality}, ${placemark.country}';
    });
  } catch (e) {
    setState(() {
      address = 'Could not fetch address.';
    });
  }
}