user_location 0.1.2

user_location #

pub package travis

A plugin for FlutterMap package to handle and plot the current user location

Usage #

Add flutter_map and user_location to your pubspec.yaml :

dependencies:
  flutter_map: any
  user_location: any # or the latest version on Pub

Update your gradle.properties file with this:

android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536M

Also make sure that you have added 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 #

Android #

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.

iOS #

  • Note: I have not tested the plugin in 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>

Installation guide #

  • Declare and initialize MapController mapController = MapController(); List<Marker> markers = [];
  • Add UserLocationPlugin() to plugins
  • Add MarkerLayerOptions and UserLocationOptions in layers

Sample code #

import 'package:flutter/material.dart';
import 'package:user_location/user_location.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong/latlong.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'User Location Plugin Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  // ADD THIS
  MapController mapController = MapController();
  UserLocationOptions userLocationOptions;
  // ADD THIS
  List<Marker> markers = [];
  @override
  Widget build(BuildContext context) {
    // You can use the userLocationOptions object to change the properties
    // of UserLocationOptions in runtime
    userLocationOptions = UserLocationOptions(
                context: context,
                mapController: mapController,
                markers: markers,
                );
    return Scaffold(
        appBar: AppBar(title: Text("User Location Plugin")),
        body: FlutterMap(
          options: MapOptions(
            center: LatLng(0,0),
            zoom: 15.0,
            plugins: [
             // ADD THIS
              UserLocationPlugin(),
            ],
          ),
          layers: [
            TileLayerOptions(
              urlTemplate: "https://api.tiles.mapbox.com/v4/"
                  "{id}/{z}/{x}/{y}@2x.png?access_token={accessToken}",
              additionalOptions: {
                'accessToken': '<access_token>',
                'id': 'mapbox.streets',
              },
            ),
            // ADD THIS
            MarkerLayerOptions(markers: markers),
            // ADD THIS
            userLocationOptions,
          ],
          // ADD THIS
          mapController: mapController,
        ));
  }
}

Optional parameters #

  • markerWidget overrides the default marker widget
  • onLocationChange is a callback function to get the current location of user. It's uses is defined in the example program.
  • updateMapLocationOnPositionChange moves the map to the current location of the user if set to true
  • showMoveToCurrentLocationFloatingActionButton displays a floating action button at the bottom right of the screen which will redirect the user to their current location. You can also pass your own widget as FAB and control the position using options fabBottom and fabRight options.
  • showHeading is used to control whether or not to show heading in the marker widget
  • zoomToCurrentLocationOnLoad if true, zooms to the current location of the user with a zoom factor 17
  • moveToCurrentLocationFloatingActionButton is a widget when passed overrides the default floating action button. Default floating action button code:
Container(
    decoration: BoxDecoration(
    color: Colors.blueAccent,
    borderRadius: BorderRadius.circular(20.0),
    boxShadow: [
        BoxShadow(color: Colors.grey, blurRadius: 10.0)
        ]),
    child: Icon(
        Icons.my_location,
        color: Colors.white,
    ),
)

Run the example #

See the example/ folder for a working example app.

0.1.2 #

  • Disposes compass stream #34

0.1.1 #

  • Fixes #10
  • Adds user direction head(Fixes #18)

0.1.0 #

  • Upgraded flutter map to v8.0.2
  • Fix map zoom issue #26
  • Upgraded README.md

0.0.9 #

  • adds zoom options to MapController
  • FAB when clicked zooms to current location

0.0.7 #

  • Upgraded the flutter map plugin to 0.80

0.0.5 #

  • Made mapController optional
  • Added FloatingActionButton to redirect map to current location
  • Minor Fixes

0.0.4 #

  • Added Listener for location status of the phone
  • Provides current location of the marker
  • Minor bug fixes

0.0.3 #

  • Added marker to show accuracy
  • Custom user marker widget
  • Removed unnecessary files

0.0.2 #

  • Changed author email id

0.0.1 #

  • TODO: Describe initial release.

example/README.md

user_location_example #

Demonstrates how to use the user_location plugin.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

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


dependencies:
  user_location: ^0.1.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:user_location/user_location.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
88
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]
94
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.5
  • Flutter: 1.12.13+hotfix.7

Health suggestions

Fix lib/src/user_location_options.dart. (-0.50 points)

Analysis of lib/src/user_location_options.dart reported 1 hint:

line 4 col 8: Unused import: 'package:location/location.dart'.

Format lib/src/user_location_layer.dart.

Run flutter format to format lib/src/user_location_layer.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
flutter_compass ^0.3.4 0.3.6
flutter_map ^0.8.2 0.8.2
latlong ^0.6.1 0.6.1
location ^2.3.5 2.4.0
Transitive dependencies
ansicolor 1.0.2
async 2.4.0
cached_network_image 2.0.0
charcode 1.1.3
collection 1.14.11 1.14.12
console_log_handler 1.1.6
convert 2.1.1
crypto 2.1.4
flutter_cache_manager 1.1.3
flutter_image 3.0.0
http 0.12.0+4
http_parser 3.1.3
intl 0.16.1
logging 0.11.4
matcher 0.12.6
meta 1.1.8
path 1.6.4
path_provider 1.6.0
pedantic 1.9.0
platform 2.2.1
positioned_tap_detector 1.0.3
quiver 2.1.2+1
rxdart 0.23.1
sky_engine 0.0.99
source_span 1.6.0
sqflite 1.2.0 1.2.1-dev.3
stack_trace 1.9.3
string_scanner 1.0.5
synchronized 2.2.0
term_glyph 1.1.0
transparent_image 1.0.0
tuple 1.0.3
typed_data 1.1.6
uuid 2.0.4
validate 1.7.0
vector_math 2.0.8
Dev dependencies
flutter_test