guoruin123 / flutterlocation

A Flutter Plugin to get location, handling callbacks to get continuous location.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Flutter Location Plugin pub package

This plugin for Flutter handles getting location on Android and iOS. It also provides callbacks when location is changed.

Demo App

Breaking Changes

As of the 2.0 version, you have to call the returned location is an LocationData Object. You can just access the different attribute like so : location.latitude. See the API part of the README for the changes. This version also switched to AndroidX instead of the old Android Support library. In order to use it you have to do the following things:

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

Getting Started

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" />

Permission check for Android 6+ was added. Still no callback when permissions granted so aiming SDK 21 is safer.

And to use it in iOS, you have to add this permission in Info.plist :

NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription

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 = <String, double>{};

var location = new Location();

// Platform messages may fail, so we use a try/catch PlatformException.
try {
  currentLocation = await location.getLocation;
} on PlatformException {
  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);
  print(currentLocation.accuracy);
  print(currentLocation.altitude);
  print(currentLocation.speed);
  print(currentLocation.speed_accuracy); // Will always be 0 on iOS
  print(currentLocation.heading);
});

API

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

Methods Description
Future<LocationData> getLocation()
Allow to get a one time position of the user.
Future<bool> hasPermission()
Return a boolean to know the state of the location permission.
Stream<LocationData> onLocationChanged()
Get the stream of the user's location.

Objects

class LocationData {
  final double latitude;
  final double longitude;
  final double accuracy;
  final double altitude;
  final double speed;
  final double speedAccuracy;
  final double heading;
}

Feedback

Please feel free to give me any feedback helping support this plugin !

About

A Flutter Plugin to get location, handling callbacks to get continuous location.

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Java 55.3%Language:Objective-C 23.9%Language:Dart 18.4%Language:Ruby 2.4%