geoflutterfire 2.0.3+5

GeoFlutterFire 🌍

version MIT License PRs Welcome

GeoFlutterFire is an open-source library that allows you to store and query a set of keys based on their geographic location. At its heart, GeoFlutterFire simply stores locations with string keys. Its main benefit, however, is the possibility of retrieving only those keys within a given geographic area - all in realtime.

GeoFlutterFire uses the Firebase Firestore Database for data storage, allowing query results to be updated in realtime as they change. GeoFlutterFire selectively loads only the data near certain locations, keeping your applications light and responsive, even with extremely large datasets.

GeoFlutterFire is designed as a lightweight add-on to cloud_firestore plugin. To keep things simple, GeoFlutterFire stores data in its own format within your Firestore database. This allows your existing data format and Security Rules to remain unchanged while still providing you with an easy solution for geo queries.

Heavily influenced by GeoFireX 🔥🔥 from Jeff Delaney 😎

📺 Checkout this amazing tutorial on fireship by Jeff, featuring the plugin!!

Getting Started #

You should ensure that you add GeoFlutterFire as a dependency in your flutter project.

dependencies:
    geoflutterfire: <latest-version>

You can also reference the git repo directly if you want:

dependencies:
    geoflutterfire:
        git: git://github.com/DarshanGowda0/GeoFlutterFire.git

You should then run flutter packages get or update your packages in IntelliJ.

Example #

There is a detailed example project in the example folder. Check that out or keep reading!

Initialize #

You need a firebase project with Firestore setup.

import 'package:geoflutterfire/geoflutterfire.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

// Init firestore and geoFlutterFire
Geoflutterfire geo = Geoflutterfire();
Firestore _firestore = Firestore.instance;

Writing Geo data #

Add geo data to your firestore document using GeoFirePoint

GeoFirePoint myLocation = geo.point(latitude: 12.960632, longitude: 77.641603);

Next, add the GeoFirePoint to you document using Firestore's add method

 _firestore
        .collection('locations')
        .add({'name': 'random name', 'position': myLocation.data});

Calling geoFirePoint.data returns an object that contains a geohash string and a Firestore GeoPoint. It should look like this in your database. You can name the object whatever you want and even save multiple points on a single document.

Query Geo data #

To query a collection of documents with 50kms from a point

// Create a geoFirePoint
GeoFirePoint center = geo.point(latitude: 12.960632, longitude: 77.641603);

// get the collection reference or query
var collectionReference = _firestore.collection('locations');

double radius = 50;
String field = 'position';

Stream<List<DocumentSnapshot>> stream = geo.collection(collectionRef: collectionReference)
                                        .within(center: center, radius: radius, field: field);

The within function returns a Stream of the list of DocumentSnapshot data, plus some useful metadata like distance from the centerpoint.

stream.listen((List<DocumentSnapshot> documentList) {
        // doSomething()
      });

You now have a realtime stream of data to visualize on a map.

📓 API #

collection(collectionRef: CollectionReference) #

Creates a GeoCollectionRef which can be used to make geo queries, alternatively can also be used to write data just like firestore's add / set functionality.

Example:

// Collection ref
// var collectionReference = _firestore.collection('locations').where('city', isEqualTo: 'bangalore');
var collectionReference = _firestore.collection('locations');
var geoRef = geo.collection(collectionRef: collectionReference);

Note: collectionReference can be of type CollectionReference or Query

Performing Geo-Queries #

geoRef.within(center: GeoFirePoint, radius: double, field: String, {strictMode: bool})

Query the parent Firestore collection by geographic distance. It will return documents that exist within X kilometers of the center-point. field supports nested objects in the firestore document.

Note: Use optional parameter strictMode = true to filter the documents strictly within the bound of given radius.

Example:

// For GeoFirePoint stored at the root of the firestore document
geoRef.within(center: centerGeoPoint, radius: 50, field: 'position', strictMode: true);

// For GeoFirePoint nested in other objects of the firestore document
geoRef.within(center: centerGeoPoint, radius: 50, field: 'address.location.position', strictMode: true);

Each documentSnapshot.data also contains distance calculated on the query.

Returns: Stream<List<DocumentSnapshot>>

Write Data #

Write data just like you would in Firestore

geoRef.add(data)

Or use one of the client's conveniece methods

  • geoRef.setDoc(String id, var data, {bool merge}) - Set a document in the collection with an ID.
  • geoRef.setPoint(String id, String field, double latitude, double longitude)- Add a geohash to an existing doc

Read Data #

In addition to Geo-Queries, you can also read the collection like you would normally in Firestore, but as an Observable

  • geoRef.data()- Stream of documentSnapshot
  • geoRef.snapshot()- Stream of Firestore QuerySnapshot

point(latitude: double, longitude: double) #

Returns a GeoFirePoint allowing you to create geohashes, format data, and calculate relative distance.

Example: var point = geo.point(38, -119)

Getters #

  • point.hash Returns a geohash string at precision 9
  • point.geoPoint Returns a Firestore GeoPoint
  • point.data Returns data object suitable for saving to the Firestore database

Geo Calculations #

  • point.distance(latitude, longitude) Haversine distance to a point

⚡ Tips #

Scale to Massive Collections #

It's possibe to build Firestore collections with billions of documents. One of the main motivations of this project was to make geoqueries possible on a queried subset of data. You can pass a Query instead of a CollectionReference into the collection(), then all geoqueries will be scoped with the contstraints of that query.

Note: This query requires a composite index, which you will be prompted to create with an error from Firestore on the first request.

Example:

var queryRef = _firestore.collection('locations').where('city', isEqualTo: 'bangalore');
var stream = geo
              .collection(collectionRef: queryRef)
              .within(center: center, radius: rad, field: 'position');

Usage of strictMode #

It's advisable to use strictMode = false for smaller radius to make use of documents from neighbouring hashes as well.

As the radius increases to a large number, the neighbouring hash precisions fetch documents which would be considerably far from the radius bounds, hence its advisable to use strictMode = true for larger radius.

Note: filtering for strictMode happens on client side, hence filtering at larger radius is at the expense of making unnecessary document reads.

Make Dynamic Queries the RxDart Way #

var radius = BehaviorSubject<double>.seeded(1.0);
var collectionReference = _firestore.collection('locations');

stream = radius.switchMap((rad) {
      return geo
          .collection(collectionRef: collectionReference)
          .within(center: center, radius: rad, field: 'position');
    });

// Now update your query
radius.add(25);

Limitations #

  • range queries on multiple fields is not suppoerted by cloud_firestore at the moment, since this library already uses range query on geohash field, you cannot perform range queries with GeoFireCollectionRef.
  • limit() and orderBy() are not supported at the moment. limit() could be used to limit docs inside each hash individually which would result in running limit on all 9 hashes inside the specified radius. orderBy() is first run on geohashes in the library, hence appending orderBy() with another feild wouldn't produce expected results. Alternatively documents can be sorted on client side.

2.0.3+5 #

  • upgraded dependencies
  • fix for iOS build errors
  • fixes for breaking changes from 2.0.3 for stream builders
  • added a bug-fix for supporting stream builders

2.0.2 #

  • added support for filtering documents strictly/easily with respect to radius

2.0.1+1 #

  • bumped up the versions of kotlin-plugin and gradle.
  • Support for GeoPoints nested inside the firestore document

2.0.0 #

  • Breaking change. Migrate from the deprecated original Android Support Library to AndroidX. This shouldn't result in any functional changes, but it requires any Android apps using this plugin to also migrate if they're using the original support library.
  • reverted to flutter stable channel from master.

1.0.2 #

  • Refactored code to adhere to best practices(again)

1.0.1 #

  • Refactored code to adhere to best practices

1.0.0 #

  • Initial Release

example/README.md

geoflutterfire_example #

Demonstrates how to use the geoflutterfire plugin.

   import 'package:flutter/material.dart';
   import 'package:geoflutterfire/geoflutterfire.dart';
   import 'package:cloud_firestore/cloud_firestore.dart';
   import 'package:geoflutterfire/src/point.dart';
   import 'package:google_maps_flutter/google_maps_flutter.dart';
   import 'package:rxdart/rxdart.dart';
   
   void main() => runApp(MaterialApp(
         title: 'Geo Flutter Fire example',
         home: MyApp(),
         debugShowCheckedModeBanner: false,
       ));
   
   class MyApp extends StatefulWidget {
     @override
     _MyAppState createState() => _MyAppState();
   }
   
   class _MyAppState extends State<MyApp> {
     GoogleMapController _mapController;
     TextEditingController _latitudeController, _longitudeController;
   
     // firestore init
     Firestore _firestore = Firestore.instance;
     Geoflutterfire geo;
     Stream<List<DocumentSnapshot>> stream;
     var radius = BehaviorSubject(seedValue: 1.0);
   
     @override
     void initState() {
       super.initState();
       _latitudeController = TextEditingController();
       _longitudeController = TextEditingController();
   
       geo = Geoflutterfire();
       GeoFirePoint center = geo.point(latitude: 12.960632, longitude: 77.641603);
       stream = radius.switchMap((rad) {
         var collectionReference = _firestore.collection('locations');
   //          .where('name', isEqualTo: 'darshan');
         return geo
             .collection(collectionRef: collectionReference)
             .within(center: center, radius: rad, field: 'position');
   
         /*
         ****Example to specify nested object**** 
         
         var collectionReference = _firestore.collection('nestedLocations');
   //          .where('name', isEqualTo: 'darshan');
         return geo.collection(collectionRef: collectionReference).within(
             center: center, radius: rad, field: 'address.location.position');
             
         */
       });
     }
   
     @override
     void dispose() {
       super.dispose();
       radius.close();
     }
   
     @override
     Widget build(BuildContext context) {
       return MaterialApp(
         home: Scaffold(
           appBar: AppBar(
             title: const Text('GeoFlutterFire'),
             actions: <Widget>[
               IconButton(
                 onPressed: _mapController == null
                     ? null
                     : () {
                         _showHome();
                       },
                 icon: Icon(Icons.home),
               )
             ],
           ),
           body: Container(
             child: Column(
               crossAxisAlignment: CrossAxisAlignment.center,
               children: <Widget>[
                 Center(
                   child: Card(
                     elevation: 4,
                     margin: EdgeInsets.symmetric(vertical: 8),
                     child: SizedBox(
                       width: MediaQuery.of(context).size.width - 30,
                       height: MediaQuery.of(context).size.height * (1 / 3),
                       child: GoogleMap(
                         onMapCreated: _onMapCreated,
                         initialCameraPosition: const CameraPosition(
                           target: LatLng(12.960632, 77.641603),
                           zoom: 15.0,
                         ),
                       ),
                     ),
                   ),
                 ),
                 Padding(
                   padding: const EdgeInsets.only(top: 8.0),
                   child: Slider(
                     min: 1,
                     max: 200,
                     divisions: 4,
                     value: _value,
                     label: _label,
                     activeColor: Colors.blue,
                     inactiveColor: Colors.blue.withOpacity(0.2),
                     onChanged: (double value) => changed(value),
                   ),
                 ),
                 Row(
                   mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                   children: <Widget>[
                     Container(
                       width: 100,
                       child: TextField(
                         controller: _latitudeController,
                         keyboardType: TextInputType.number,
                         textInputAction: TextInputAction.next,
                         decoration: InputDecoration(
                             labelText: 'lat',
                             border: OutlineInputBorder(
                               borderRadius: BorderRadius.circular(8),
                             )),
                       ),
                     ),
                     Container(
                       width: 100,
                       child: TextField(
                         controller: _longitudeController,
                         keyboardType: TextInputType.number,
                         decoration: InputDecoration(
                             labelText: 'lng',
                             border: OutlineInputBorder(
                               borderRadius: BorderRadius.circular(8),
                             )),
                       ),
                     ),
                     MaterialButton(
                       color: Colors.blue,
                       onPressed: () {
                         double lat = double.parse(_latitudeController.text);
                         double lng = double.parse(_longitudeController.text);
                         _addPoint(lat, lng);
                       },
                       child: Text(
                         'ADD',
                         style: TextStyle(color: Colors.white),
                       ),
                     )
                   ],
                 ),
                 MaterialButton(
                   color: Colors.amber,
                   child: Text(
                     'Add nested ',
                     style: TextStyle(color: Colors.white),
                   ),
                   onPressed: () {
                     double lat = double.parse(_latitudeController.text);
                     double lng = double.parse(_longitudeController.text);
                     _addNestedPoint(lat, lng);
                   },
                 )
               ],
             ),
           ),
         ),
       );
     }
   
     void _onMapCreated(GoogleMapController controller) {
       setState(() {
         _mapController = controller;
   //      _showHome();
         //start listening after map is created
         stream.listen((List<DocumentSnapshot> documentList) {
           _updateMarkers(documentList);
         });
       });
     }
   
     void _showHome() {
       _mapController.animateCamera(CameraUpdate.newCameraPosition(
         const CameraPosition(
           target: LatLng(12.960632, 77.641603),
           zoom: 15.0,
         ),
       ));
     }
   
     void _addPoint(double lat, double lng) {
       GeoFirePoint geoFirePoint = geo.point(latitude: lat, longitude: lng);
       _firestore
           .collection('locations')
           .add({'name': 'random name', 'position': geoFirePoint.data}).then((_) {
         print('added ${geoFirePoint.hash} successfully');
       });
     }
   
     //example to add geoFirePoint inside nested object
     void _addNestedPoint(double lat, double lng) {
       GeoFirePoint geoFirePoint = geo.point(latitude: lat, longitude: lng);
       _firestore.collection('nestedLocations').add({
         'name': 'random name',
         'address': {
           'location': {'position': geoFirePoint.data}
         }
       }).then((_) {
         print('added ${geoFirePoint.hash} successfully');
       });
     }
   
     void _addMarker(double lat, double lng) {
       var _marker = MarkerOptions(
         position: LatLng(lat, lng),
         icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueViolet),
       );
       setState(() {
         _mapController.addMarker(_marker);
       });
     }
   
     void _updateMarkers(List<DocumentSnapshot> documentList) {
       documentList.forEach((DocumentSnapshot document) {
         GeoPoint point = document.data['position']['geopoint'];
         _addMarker(point.latitude, point.longitude);
       });
     }
   
     double _value = 20.0;
     String _label = '';
   
     changed(value) {
       setState(() {
         _value = value;
         _label = '${_value.toInt().toString()} kms';
         _mapController.clearMarkers();
       });
       radius.add(value);
     }
   }

Use this package as a library

1. Depend on it

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


dependencies:
  geoflutterfire: ^2.0.3+5

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

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

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.3

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
cloud_firestore ^0.12.5 0.12.7+1
flutter 0.0.0
rxdart ^0.22.0 0.22.0
Transitive dependencies
collection 1.14.11
firebase_core 0.4.0+8
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8

Admin