hritika agarwal
hritika agarwal

Reputation: 87

Live location in flutter

How to get the coordinates of live location in flutter ? I want to get the live location coordinates. What could be done to get this ? I am imported the location package in flutter but I just want to get the coordinates of live location .

Upvotes: 0

Views: 292

Answers (2)

yusuf
yusuf

Reputation: 41

First of all you need to get a location plugin. https://pub.dev/packages/location. Also I recommend that use Provider plugin and create model files. It will be more easier to manage. In addition , you have to check permission status of your device . you will reach all of information what you want to learn through link

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

 Location _location = Location();
 LocationData currentLocation;


@override
  void initState() {
    location = new Location();
    location.onLocationChanged.listen((LocationData cLoc) {
      currentLocation = cLoc;

      });

    super.initState();
  }


  @override


Widget build(BuildContext context) {
        return Center(
  child: Text(
      'Location: Lat${currentLocation.latitude}, Long: ${currentLocation.longitude}'),);}}
          

    

Upvotes: 1

Location location = new Location();

bool _serviceEnabled;
PermissionStatus _permissionGranted;
LocationData _locationData;

_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
  _serviceEnabled = await location.requestService();
  if (!_serviceEnabled) {
    return;
  }
}

_permissionGranted = await location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
  _permissionGranted = await location.requestPermission();
  if (_permissionGranted != PermissionStatus.granted) {
    return;
  }
}

location.onLocationChanged.listen((LocationData currentLocation) {
  // Use current location
});

Upvotes: 0

Related Questions