Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

flutter realtime location plugin

this plugin for flutter handles getting realtime location on android and ios. it also provides callbacks when location is changed.

realtime location

getting started with realtime location

android

in order to use this plugin in android, you have to add this permission in androidmanifest.xml :

<uses-permission android:name="android.permission.access_fine_location" />

update your gradle.properties file with this:

android.enablejetifier=true
android.useandroidx=true
org.gradle.jvmargs=-xmx1536m

please also make sure that you have those dependencies in your build.gradle:

  dependencies {
      classpath 'com.android.tools.build:gradle:3.3.0'
      classpath 'com.google.gms:google-services:4.2.0'
  }
...
  compilesdkversion 28

ios

and to use it in ios, you have to add this permission in info.plist :

nslocationwheninuseusagedescription
nslocationalwaysusagedescription

warning: there is a currently a bug in ios simulator in which you have to manually select a location several in order for the simulator to actually send data. please keep that in mind when testing in ios simulator.

example app for realtime location

the example app uses google maps flutter plugin, add your api key in the androidmanifest.xml and in appdelegate.m to use the google maps plugin.

sample code for realtime location

then you just have to import the package with

import 'package:location/location.dart';

look into the example for utilisation, but a basic implementation can be done like this for a one time location :

var currentlocation = locationdata;

var location = new location();

// platform messages may fail, so we use a try/catch platformexception.
try {
  currentlocation = await location.getlocation();
} on platformexception catch (e) {
  if (e.code == 'permission_denied') {
    error = 'permission denied';
  } 
  currentlocation = null;
}

you can also get continuous callbacks when your position is changing:

var location = new location();

location.onlocationchanged().listen((locationdata currentlocation) {
  print(currentlocation.latitude);
  print(currentlocation.longitude);
});

public method summary

in this table you can find the different functions exposed by this plugin:

return description
future<bool> requestpermission()
request the location permission. return a boolean to know if the permission has been granted.
future<bool> haspermission()
return a boolean to know the state of the location permission.
future<bool> serviceenabled()
return a boolean to know if the location service is enabled or if the user manually deactivated it.
future<bool> requestservice()
show an alert dialog to request the user to activate the location service. on ios, will only display an alert due to apple guidelines, the user having to manually go to settings. return a boolean to know if the location service has been activated (always false on ios).
future<bool> changesettings(locationaccuracy accuracy = locationaccuracy.high, int interval = 1000, double distancefilter = 0)
will change the settings of futur requests. accuracywill describe the accuracy of the request (see the locationaccuracy object). interval will set the desired interval for active location updates, in milliseconds (only affects android). distancefilter set the minimum displacement between location updates in meters.
future<locationdata> getlocation()
allow to get a one time position of the user. it will try to request permission if not granted yet and will throw a permission_denied error code if permission still not granted.
stream<locationdata> onlocationchanged()
get the stream of the user’s location. it will try to request permission if not granted yet and will throw a permission_denied error code if permission still not granted.

you should try to manage permission manually with requestpermission() to avoid error, but plugin will try handle some cases for you.

objects

class locationdata {
  final double latitude; // latitude, in degrees
  final double longitude; // longitude, in degrees
  final double accuracy; // estimated horizontal accuracy of this location, radial, in meters
  final double altitude; // in meters above the wgs 84 reference ellipsoid
  final double speed; // in meters/second
  final double speedaccuracy; // in meters/second, always 0 on ios
  final double heading; //heading is the horizontal direction of travel of this device, in degrees
  final double time; //timestamp of the locationdata
}


enum locationaccuracy { 
  powersave, // to request best accuracy possible with zero additional power consumption, 
  low, // to request "city" level accuracy
  balanced, // to request "block" level accuracy
  high, // to request the most accurate locations available
  navigation // to request location for navigation usage (affect only ios)
}

note: you can convert the timestamp into a datetime with: datetime.frommillisecondssinceepoch(locationdata.time.toint())


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.