geolocator 1.3.0

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

Flutter Geolocator Plugin

pub package

A Flutter geolocation plugin which provides easy access to the platform specific location services (LocationManager on Android and CLLocationManager on iOS).

BranchBuild Status
developBuild Status
masterBuild Status

Features

  • Get the current location of the device;
  • Get continuous location updates;
  • Translate an address to geocoordinates and vice verse (a.k.a. Geocoding);
  • Calculate the distance (in meters) between two geocoordinates.

Usage

To use this plugin, add geolocator as a dependency in your pubspec.yaml file. For example:

dependencies:
  geolocator: '^1.3.0'

NOTE: There's a known issue with integrating plugins that use Swift into a Flutter project created with the Objective-C template. See issue Flutter#16049 for help on integration.

API

Geolocation

To query the current location of the device simply make a call to the getPosition method:

import 'package:geolocator/geolocator.dart';
import 'package:geolocator/models/location_accuracy.dart';
import 'package:geolocator/models/position.dart';

Position position = await Geolocator().getPosition(LocationAccuracy.High);

To listen for location changes you can subscribe to the onPositionChanged stream. Supply an instance of the LocationOptions class to configure the desired accuracy and the minimum distance change (in meters) before updates are send to the application.

import 'package:geolocator/geolocator.dart';
import 'package:geolocator/models/location_accuracy.dart';
import 'package:geolocator/models/position.dart';

var geolocator = Geolocator();
var locationOptions = LocationOptions(accuracy: LocationAccuracy.High, distanceFilter: 10);

StreamSubscription<Position> positionStream = geolocator.getPositionStream(locationOptions).listen(
    (Position position) {
        print(_position == null ? 'Unknown' : _position.latitude.toString() + ', ' + _position.longitude.toString());
    });

Geocoding

To translate an address into latitude and longitude coordinates you can use the toPlacemark method:

import 'package:geolocator/geolocator.dart';
import 'package:geolocator/models/placemark.dart';

Placemark placemark = await Geolocator().toPlacemark("Gronausestraat 710, Enschede");

If you want to translate latitude and longitude coordinates into an address you can use the fromPlacemark method:

import 'package:geolocator/geolocator.dart';
import 'package:geolocator/models/placemark.dart';

Placemark placemark = await new Geolocator().toPlacemark(52.2165157, 6.9437819);

Calculate distance

To calculate the distance (in meters) between two geocoordinates you can use the distanceBetween method. The distanceBetween method takes four parameters:

ParameterTypeDescription
startLatitudedoubleLatitude of the start position
startLongitudedoubleLongitude of the start position
endLatitudedoubleLatitude of the destination position
endLongitudedoubleLongitude of the destination position
import 'package:geolocator/geolocator.dart';

double distanceInMeters = await new Geolocator().distanceBetween(52.2165157, 6.9437819, 52.3546274, 4.8285838);

See also the example project for a complete implementation.

Permissions

Android

On Android you'll need to add the ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION permissions to your Android Manifest. Todo so open the AndroidManifest.xml file and add the following two lines as direct children of the <manifest> tag:

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

iOS

On iOS you'll need to add the NSLocationWhenInUseUsageDescription to your Info.plist file in order to access the device's location. Simply open your Info.plist file and add the following:

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>

If you would like to access the device's location when your App is running in the background, you should also add the NSLocationAlwaysAndWhenInUseUsageDescription (if your App support iOS 10 or earlier you should also add the key NSLocationAlwaysUsageDescription) key to your Info.plist file:

<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>

Location accuracy

The table below outlines the accuracy options per platform:

AndroidiOS
Lowest500m3000m
Low500m1000m
Medium100 - 500m100m
High0 - 100m10m
Best0 - 100m~0m

Issues

Please file any issues, bugs or feature request as an issue on our GitHub page.

Want to contribute

If you would like to contribute to the plugin (e.g. by improving the documentation, solving a bug or adding a cool new feature), please carefully review our contribution guide and send us your pull request.

Author

This Geolocator plugin for Flutter is developed by Baseflow. You can contact us at hello@baseflow.com

[1.3.0]

  • Added the option to check the distance between two geocoordinates (using the distanceBetween method).

[1.2.2]

  • Make sure that an Android App using the plugin is informed when the platform stops transmitting location updates.

[1.2.1]

  • Added feature to throttle the amount of locations updates based on a supplied distance filter.

Important:

This introduces a breaking change since the signature of the getPositionStream has changed from getPositionStream(LocationAccuracy accuracy) to getPositionStream(LocationOptions locationOptions) .

  • Made some small changes to ensure the plugin no longer is depending on JAVA 8, meaning the plugin will run using the default Android configuration.

[1.2.0]

  • Added support to translate an address into geocoordinates and vice versa (a.k.a. Geocoding). See the README.md file for more information.

[1.1.2]

  • Fixed reported formatting issues

[1.1.1]

  • Fixed a warning generated by xCode when compiling the example project (see issue #28)
  • Fixed some warnings generated by Dart static code analyser, improving code quality

[1.1.0]

  • Introduced the option to supply a desired accuracy.

Important:

This introduces a breaking change, the getPosition and onPositionChanged properties have been replaced by methods (getPosition([LocationAccuracy desiredAccuracy = LocationAccuracy.Best]) and getPositionStream([LocationAccuracy desiredAccuracy = LocationAccuracy.Best]) respectively) accepting a parameter to indicate the desired accuracy.

[1.0.0]

  • Updated documentation
  • API defined stable

[0.0.2]

  • Solved problem with missing geolocator-Swift.h header file (see also issue Flutter#16049).

[0.0.1]

  • Initial release

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:geolocator_example/widgets/calculate_distance_widget.dart';
import 'package:geolocator_example/widgets/current_location_widget.dart';
import 'package:geolocator_example/widgets/lookup_address_widget.dart';
import 'package:geolocator_example/widgets/lookup_coordinates_widget.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        home: new Scaffold(
            appBar: new AppBar(
              title: new Text('Plugin example app'),
            ),
            body: new Center(
              child: new Column(
                children: <Widget>[
                  new CurrentLocationWidget(),
                  new Divider(),
                  new LookupAddressWidget(),
                  new Divider(),
                  new LookupCoordinatesWidget(),
                  new Divider(),
                  new CalculateDistanceWidget(),
                ],
              ),
            )));
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  geolocator: ^1.3.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:geolocator/geolocator.dart';
  
Version Uploaded Documentation Archive
1.3.0 Jul 20, 2018 Go to the documentation of geolocator 1.3.0 Download geolocator 1.3.0 archive
1.2.2 Jul 13, 2018 Go to the documentation of geolocator 1.2.2 Download geolocator 1.2.2 archive
1.2.1 Jul 12, 2018 Go to the documentation of geolocator 1.2.1 Download geolocator 1.2.1 archive
1.2.0 Jul 10, 2018 Go to the documentation of geolocator 1.2.0 Download geolocator 1.2.0 archive
1.1.2 Jul 6, 2018 Go to the documentation of geolocator 1.1.2 Download geolocator 1.1.2 archive
1.1.1 Jul 6, 2018 Go to the documentation of geolocator 1.1.1 Download geolocator 1.1.1 archive
1.1.0 Jul 3, 2018 Go to the documentation of geolocator 1.1.0 Download geolocator 1.1.0 archive
1.0.0 Jun 29, 2018 Go to the documentation of geolocator 1.0.0 Download geolocator 1.0.0 archive
0.0.2 Jun 27, 2018 Go to the documentation of geolocator 0.0.2 Download geolocator 0.0.2 archive
0.0.1 Jun 22, 2018 Go to the documentation of geolocator 0.0.1 Download geolocator 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
83
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
91
Learn more about scoring.

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

  • Dart: 2.0.0-dev.69.0
  • pana: 0.11.7
  • Flutter: 0.5.5

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
meta ^1.0.5 1.1.5 1.1.6
Transitive dependencies
collection 1.14.6 1.14.11
sky_engine 0.0.99
typed_data 1.1.5 1.1.6
vector_math 2.0.6 2.0.7
Dev dependencies
flutter_test
mockito ^2.0.2