geolocation 0.1.0

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

Geolocation

Flutter geolocation plugin that works on Android API 16+ and iOS 9+.

Features:

  • Automatic permission management
  • Retrieve last known location
  • Request single location update

Plugin is under active development and will feature soon:

  • Continuous location updates with foreground and background strategies
  • Geocode
  • Geofences
  • Place suggestions
  • Activity recognition

Documentation will improve as well.

Demo app

AndroidiOS

Getting Started

Add geolocation to your pubspec.yaml:

dependencies:
  geolocation: ^0.1.0

Note:

The iOS code of geolocation plugin is written in swift. There is a known issue for integrating swift written plugin into Flutter project created with Objective-C template. See #16049 for help on integration.

Permission

In order to work with location, app needs to specify location permission in configuration file and request it at runtime. Geolocation plugin will check at runtime if permission configuration is missing, and throw an exception otherwise.

iOS configuration

Depending on the iOS version you target, you need to add to Infos.plist:

  • iOS 9/10: NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription
  • iOS 11+: NSLocationAlwaysAndWhenInUseUsageDescription or NSLocationWhenInUseUsageDescription

Depending if you specify permission description for when in use or always in Infos.plist, geolocation plugin will automatically request the proper associated permission at runtime.

Android configuration

You need to add one of the following permission to AndroidManifest.xml:

  • <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  • <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Note that ACCESS_FINE_LOCATION includes ACCESS_COARSE_LOCATION.

Runtime request

On iOS and on Android API 23+, app also needs to request permission at runtime. Geolocation plugin handles it automatically. If the user denied location permission, location result will contain the following error:

LocationResult result = ...
result.isSuccessful // => false
result.error.type // => GeolocationResultErrorType.permissionDenied

Location result

All geolocation API related to getting a location returns a Future<LocationResult>.

final LocationResult result = await Geolocation.XXX;

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.permissionDenied:
      // user denied location permission request
      // rejection is final on iOS, and can be on Android
      // user will need to manually allow the app from the settings
      break;
    case GeolocationResultErrorType.playServicesUnavailable:
      // android only
      // result.error.additionalInfo contains more details on the play services error
      switch(result.error.additionalInfo as GeolocationAndroidPlayServices) {
        case GeolocationAndroidPlayServices.missing:
        case GeolocationAndroidPlayServices.updating:
        case GeolocationAndroidPlayServices.versionUpdateRequired:
        case GeolocationAndroidPlayServices.disabled:
        case GeolocationAndroidPlayServices.invalid:
      }
    break;
  }
}

Single location

Last known location

See api documentation: [link]

LocationResult result = await Geolocation.lastKnownLocation;

Current location

See api documentation: [link]

LocationResult result = await Geolocation.currentLocation(LocationAccuracy.best);

Single location update

See api documentation: [link]

LocationResult result = await Geolocation.singleLocationUpdate(LocationAccuracy.best);

[0.1.0] - Initial release

  • New feature: Last known location
  • New feature: Current location
  • New feature: Location updates

example/lib/main.dart

//  Copyright (c) 2018 Loup Inc.
//  Licensed under Apache License v2.0

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'tab_location.dart';

void main() => runApp(new MyApp());

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

class _MyAppState extends State<MyApp> {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new CupertinoTabScaffold(
        tabBar: new CupertinoTabBar(
          items: <BottomNavigationBarItem>[
            new BottomNavigationBarItem(
              title: new Text('Current'),
              icon: new Icon(Icons.location_on),
            ),
            new BottomNavigationBarItem(
              title: new Text('Track'),
              icon: new Icon(Icons.location_searching),
            ),
            new BottomNavigationBarItem(
              title: new Text('Geocode'),
              icon: new Icon(Icons.location_city),
            ),
          ],
        ),
        tabBuilder: (BuildContext context, int index) {
          return new CupertinoTabView(
            builder: (BuildContext context) {
              switch (index) {
                case 0:
                  return new TabLocation();
                default:
                  return new Container(
                    child: new Center(
                      child: new Text('TBD'),
                    ),
                  );
              }
            },
          );
        },
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  geolocation: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or 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:geolocation/geolocation.dart';
  
Version Uploaded Documentation Archive
0.2.1 Apr 12, 2018 Go to the documentation of geolocation 0.2.1 Download geolocation 0.2.1 archive
0.2.0 Apr 12, 2018 Go to the documentation of geolocation 0.2.0 Download geolocation 0.2.0 archive
0.1.1 Apr 3, 2018 Go to the documentation of geolocation 0.1.1 Download geolocation 0.1.1 archive
0.1.0 Apr 3, 2018 Go to the documentation of geolocation 0.1.0 Download geolocation 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
95
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
48
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK <2.0.0