location 1.4.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 95

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 1.4 version, you have to call getLocation() instead of just getLocation and onLocationChanged() instead of onLocationChanged.

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((Map<String,double> 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
});

API

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

MethodsDescription
Future<Map<String, double>>getLocation() <br> Allow to get a one time position of the user.
FuturehasPermission() <br> Return a boolean to know the state of the location permission.
Stream<Map<String, double>>onLocationChanged() <br> Get the stream of the user's location.

Feedback

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

[1.4.0] 21st August 2018

  • Add lazy permission request thanks to yathit
  • Add hasPermission() thanks to vagrantrobbie
  • Bug correction thanks to jalpedersen
  • Add more examples

[1.3.4] 4th June 2018

  • Fix crash for Android API pre 27 thanks to matthewtsmith.

[1.3.3] 30th May 2018

  • Correct implementation of iOS plugin to match Android behaviour. No need to call getLocation to get permissions for location callbacks.

[1.3.2] 30th May 2018

  • Change implementation to api in build.gradle in order to solve incompatibilities between GMS versions thanks to luccascorrea

[1.3.1] 29th May 2018

  • Added speed and speed_accuracy (only Android truly discover speed accuracy, so its always 0 for now on iOS)
  • Solved a crash

[1.3.0] 27th May 2018

  • Make it compatible with Firebase thanks to quangIO
  • Resolve runtime error exception thanks to jharrison902
  • Update gitignore thanks to bcko

[1.2.0] 5th April 2018

  • Permissions denied on Android handled thanks to g123k
  • Dart 2 update thanks to efortuna

[1.1.6] - 19th Octobre 2017.

  • iOS code from Swift to Objective-C thanks to fluff

[1.1.1] - 20th July 2017.

  • Fixes for iOS result's format.

[1.1.0] - 17th July 2017.

  • Added permission check for Android 6+ (thanks netdur). Still no callback when permissions granted so aiming SDK 21 is safer.

[1.0.0] - 7th July 2017.

  • Initial Release.

example/lib/main.dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:location/location.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Map<String, double> _startLocation;
  Map<String, double> _currentLocation;

  StreamSubscription<Map<String, double>> _locationSubscription;

  Location _location = new Location();
  bool _permission = false;
  String error;

  bool currentWidget = true;

  Image image1;

  @override
  void initState() {
    super.initState();

    initPlatformState();

    _locationSubscription =
        _location.onLocationChanged().listen((Map<String,double> result) {
          setState(() {
            _currentLocation = result;
          });
        });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  initPlatformState() async {
    Map<String, double> location;
    // Platform messages may fail, so we use a try/catch PlatformException.

    try {
      _permission = await _location.hasPermission();
      location = await _location.getLocation();


      error = null;
    } on PlatformException catch (e) {
      if (e.code == 'PERMISSION_DENIED') {
        error = 'Permission denied';
      } else if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {
        error = 'Permission denied - please ask the user to enable it from the app settings';
      }

      location = null;
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    //if (!mounted) return;

    setState(() {
        _startLocation = location;
    });

  }

  @override
  Widget build(BuildContext context) {
    List<Widget> widgets;


    if (_currentLocation == null) {
      widgets = new List();
    } else {
      widgets = [
        new Image.network(
            "https://maps.googleapis.com/maps/api/staticmap?center=${_currentLocation["latitude"]},${_currentLocation["longitude"]}&zoom=18&size=640x400&key=YOUR_API_KEY")
      ];
    }

    widgets.add(new Center(
        child: new Text(_startLocation != null
            ? 'Start location: $_startLocation\n'
            : 'Error: $error\n')));

    widgets.add(new Center(
        child: new Text(_currentLocation != null
            ? 'Continuous location: $_currentLocation\n'
            : 'Error: $error\n')));

    widgets.add(new Center(
      child: new Text(_permission 
            ? 'Has permission : Yes' 
            : "Has permission : No")));

    return new MaterialApp(
        home: new Scaffold(
            appBar: new AppBar(
              title: new Text('Location plugin example app'),
            ),
            body: new Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: widgets,
            )));
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  location: ^1.4.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:location/location.dart';
  
Version Uploaded Documentation Archive
1.4.1 Aug 22, 2018 Go to the documentation of location 1.4.1 Download location 1.4.1 archive
1.4.0 Aug 21, 2018 Go to the documentation of location 1.4.0 Download location 1.4.0 archive
1.3.4 Jun 4, 2018 Go to the documentation of location 1.3.4 Download location 1.3.4 archive
1.3.3 May 30, 2018 Go to the documentation of location 1.3.3 Download location 1.3.3 archive
1.3.2 May 30, 2018 Go to the documentation of location 1.3.2 Download location 1.3.2 archive
1.3.1 May 29, 2018 Go to the documentation of location 1.3.1 Download location 1.3.1 archive
1.3.0 May 24, 2018 Go to the documentation of location 1.3.0 Download location 1.3.0 archive
1.2.0 Apr 5, 2018 Go to the documentation of location 1.2.0 Download location 1.2.0 archive
1.1.7 Nov 29, 2017 Go to the documentation of location 1.1.7 Download location 1.1.7 archive
1.1.6 Oct 19, 2017 Go to the documentation of location 1.1.6 Download location 1.1.6 archive

All 19 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
98
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
95
Learn more about scoring.

We analyzed this package on Oct 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.4
  • Flutter: 0.9.5

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/location.dart.

Run flutter format to format lib/location.dart.

Maintenance suggestions

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.58.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8