Home > Back-end >  Flutter get altitude of device
Flutter get altitude of device

Time:04-05

Currently I am working on a project in flutter where I want to retrieve the geolocation height of a device to determine in which floor of a building the device is.

Lets say the building has 3 floors on a height above 0 of 350m, 355m and 359m. If the device lays down on the second floor I should get the height of 355m.

Is this even possible in flutter?
I found a package called geolocator but there you can only get the latitude and longitude of a device.

Thank you for your answers!

Solution:
Using Flutters built-in location package provides the needed functionality.

CodePudding user response:

Try having a look at Flutter's built-in location package.

There is a getLocation() function within this package which returns you LocationData and that has an 'altitude' property

Location location = new Location();
LocationData locationData;

// request the permissions for location data

locationData = await location.getLocation();

// do something with locationData.altitude

I'm not sure as to the accuracy of the data you get from this, but make sure you request the correct permissions before calling getLocation or else you may get incorrect data returned as per the documentation. Also bear in mind that it may require the device to have a barometer to get this altitude correctly, a sensor that not all devices have so you may get 0 or null returned.

Alternatively, you also have the option of accessing the device's barometer (if the device supports it) using Flutter's native code functionality and calling the native API for it i.e. for Android you can use the SensorManager API to call getAltitude.

  • Related