geoflutterfire
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.
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
final geo = geoflutterfire();
final _firestore = firebasefirestore.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.
:notebook: 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 documentsnapshotgeoref.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 9point.geopoint
returns a firestore geopointpoint.data
returns data object suitable for saving to the firestore database
geo calculations
point.distance(latitude, longitude)
haversine distance to a point
:zap: tips
scale to massive collections
it’s possible 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 constraints 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);
Comments are closed.