Skip to content

Latest commit

 

History

History
57 lines (50 loc) · 2.74 KB

useGeolocation.md

File metadata and controls

57 lines (50 loc) · 2.74 KB

useGeolocation

Tracks the state of user's geographic location and permission using geolocator.

Installation

Depends on geolocator.

dependencies:
  flutter_use_geolocation: ^0.0.2

Usage

class Sample extends HookWidget {
  @override
  Widget build(BuildContext context) {
    // You need call `Geolocator.checkPermission() and Geolocator.requestPermission()` yourself.
    final geolocation = useGeolocation();
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const Text("-- Geolocation --"),
            Text("permission checked: ${geolocation.fetched}"),
            Text("location: ${geolocation.position}"),
            ElevatedButton(
              onPressed: () async {
                await Geolocator.requestPermission();
              },
              child: const Text('Grant Location permission'),
            ),
          ],
        ),
      ),
    );
  }
}

Reference

GeolocationState

  • fetched: bool - whether geographic location state is fetched;
  • position: Position - geographic detailed location state changes.
    • longitude: double - The longitude of the position in degrees normalized to the interval -180 (exclusive) to +180 (inclusive).
    • latitude: double - The latitude of this position in degrees normalized to the interval -90.0 to +90.0 (both inclusive).
    • timestamp: DateTime? - The time at which this position was determined.
    • accuracy: double - The estimated horizontal accuracy of the position in meters. The accuracy is not available on all devices. In these cases the value is 0.0.
    • altitude: double - The altitude of the device in meters. The altitude is not available on all devices. In these cases the returned value is 0.0.
    • heading: double - The heading in which the device is traveling in degrees. The heading is not available on all devices. In these cases the value is 0.0.
    • speed: double - The speed at which the devices is traveling in meters per second over ground. The speed is not available on all devices. In these cases the value is 0.0.
    • speedAccuracy: double - The estimated speed accuracy of this position, in meters per second. The speedAccuracy is not available on all devices. In these cases the value is 0.0.
    • floor: int? - The floor specifies the floor of the building on which the device is located. The floor property is only available on iOS and only when the information is available. In all other cases this value will be null.