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

geolocation

flutter geolocation plugin for android api 16+ and ios 9+.

features:

  • manual and automatic location permission management
  • current one-shot location
  • continuous location updates with foreground and background options

the plugin is under active development and the following features are planned soon:

  • geocode
  • geofences
  • place suggestions
  • activity recognition
  • exposition of ios/android specific apis (like significant location updates on ios)
android ios
geolocation flutter ios_screenshot

installation

follow the instructions: https://pub.dev/packages/geolocation#-installing-tab-

android

geolocation is dependent on androidx. make sure to include the following settings to ‘android/gradle.properties’:

android.useandroidx=true
android.enablejetifier=true

permission

android and ios require to declare the location permission in a configuration file.

for ios

there are two kinds of location permission available in ios: “when in use” and “always”.

if you don’t know what permission to choose for your usage, see:
https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services

you need to declare the description for the desired permission in ios/runner/info.plist:

<dict>
  <!-- for ios 11 + -->
  <key>nslocationwheninuseusagedescription</key>
  <string>reason why app needs location</string>
  <key>nslocationalwaysandwheninuseusagedescription</key>
  <string>reason why app needs location</string>

  <!-- additionally for ios 9/10, if you need always permission -->
  <key>nslocationalwaysusagedescription</key>
  <string>reason why app needs location</string>
  ...
</dict>

for android

there are two kinds of location permission in android: “coarse” and “fine”.
coarse location will allow to get approximate location based on sensors like the wifi, while fine location returns the most accurate location using gps (in addition to coarse).

you need to declare one of the two permissions in android/app/src/main/androidmanifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-permission android:name="android.permission.access_coarse_location" />
  <!-- or -->
  <uses-permission android:name="android.permission.access_fine_location" />
</manifest>

note that access_fine_location permission includes access_coarse_location.

api

for more complete documentation on all usage, check the api documentation:
https://pub.dartlang.org/documentation/geolocation/latest/geolocation/geolocation-library.html

you can also check the example project that showcase a comprehensive usage of geolocation plugin.

check if location service is operational

api documentation: https://pub.dartlang.org/documentation/geolocation/latest/geolocation/geolocation/islocationoperational.html

final geolocationresult result = await geolocation.islocationoperational();
if(result.issuccessful) {
  // location service is enabled, and location permission is granted
} else {
  // location service is not enabled, restricted, or location permission is denied
}

request location permission

on android (api 23+) and ios, apps need to request location permission at runtime.

note: you are not required to request permission manually.
geolocation plugin will request permission automatically if it’s needed, when you make a location request.

api documentation: https://pub.dartlang.org/documentation/geolocation/latest/geolocation/geolocation/requestlocationpermission.html

final geolocationresult result = await geolocation.requestlocationpermission(
  const locationpermission(
    android: locationpermissionandroid.fine,
    ios: locationpermissionios.always,
  ),
  opensettingsifdenied: true,
);

if(result.issuccessful) {
  // location permission is granted (or was already granted before making the request)
} else {
  // location permission is not granted
  // user might have denied, but it's also possible that location service is not enabled, restricted, and user never saw the permission request dialog. check the result.error.type for details.
}

get the current one-shot location

geolocation offers three methods:

// get last known location, which is a future rather than a stream (best for android)
locationresult result = await geolocation.lastknownlocation();

// force a single location update (best for ios)
streamsubscription<locationresult> subscription = geolocation.currentlocation(accuracy: locationaccuracy.best).listen((result) {
  // todo with result
});

// best option for most cases
streamsubscription<locationresult> subscription = geolocation.currentlocation(accuracy: locationaccuracy.best).listen((result) {
  if(result.issuccessful) {
    double latitude = result.location.latitude;
    // todo with result
  }
});

continuous location updates

api documentation: https://pub.dartlang.org/documentation/geolocation/latest/geolocation/geolocation/locationupdates.html

streamsubscription<locationresult> subscription = geolocation.locationupdates(
    accuracy: locationaccuracy.best,
    displacementfilter: 10.0, // in meters
    inbackground: true, // by default, location updates will pause when app is inactive (in background). set to `true` to continue updates in background.
  )
  .listen((result) {
    if(result.issuccessful) {
      // todo with result
    }
  });


// cancelling subscription will also stop the ongoing location request
subscription.cancel();

handle location result

location request return either a locationresult future or a stream of locationresult.

api documentation: https://pub.dartlang.org/documentation/geolocation/latest/geolocation/locationresult-class.html

locationresult result = await geolocation.lastknownlocation();

if (result.issuccessful) {
  // location request successful, location is guaranteed to not be null
  double lat = result.location.latitude;
  double lng = result.location.longitude;
} else {
  switch (result.error.type) {
    case geolocationresulterrortype.runtime:
      // runtime error, check result.error.message
      break;
    case geolocationresulterrortype.locationnotfound:
      // location request did not return any result
      break;
    case geolocationresulterrortype.servicedisabled:
      // location services disabled on device
      // might be that gps is turned off, or parental control (android)
      break;
    case geolocationresulterrortype.permissionnotgranted:
      // location has not been requested yet
      // app must request permission in order to access the location
      break;
    case geolocationresulterrortype.permissiondenied:
      // user denied the location permission for the app
      // rejection is final on ios, and can be on android if user checks `don't ask again`
      // user will need to manually allow the app from the settings, see requestlocationpermission(opensettingsifdenied: true)
      break;
    case geolocationresulterrortype.playservicesunavailable:
      // android only
      // result.error.additionalinfo contains more details on the play services error
      switch(result.error.additionalinfo as geolocationandroidplayservices) {
        // do something, like showing a dialog inviting the user to install/update play services
        case geolocationandroidplayservices.missing:
        case geolocationandroidplayservices.updating:
        case geolocationandroidplayservices.versionupdaterequired:
        case geolocationandroidplayservices.disabled:
        case geolocationandroidplayservices.invalid:
      }
    break;
  }
}

authors

geolocation plugin is developed by loup, a mobile development studio based in montreal and paris.
you can contact us at [email protected]

contributers

  • lukaspili
  • mit-mit
  • shehabic-work
  • abgaryan
  • shehabic
  • alfanhui

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.