geolocation 0.1.0

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

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 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: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

Analysis

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

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3
  • Flutter: 0.5.4

Scores

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

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

  • Homepage does not exists.

    We were unable to access https://gihub.com/loup-v/geolocation at the time of the analysis.

  • The description is too short.

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

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 3 hints.

    Run flutter format to format lib/channel/api.dart.

    Run flutter format to format lib/data/location_result.dart.

    Similar analysis of the following files failed:

    • lib/facet_android/location.dart (hint)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK <2.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.6 1.14.10
meta 1.1.5
sky_engine 0.0.99
typed_data 1.1.5
vector_math 2.0.6 2.0.7