搜索插件、代码仓与 APP 分类

插件详情v2.3.15

geoflutterfire2

GeoFlutterFire2 是一个开源库,可让您根据地理位置存储和查询 Firestore 文档。

AndroidiOSWebmacOSWindows
近 30 天下载
4370
喜欢数
118
Pub 分数
140

安装插件

flutter pub add geoflutterfire2

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

GeoFlutterFire2 是一个开源的 Flutter 包,使开发者能够使用地理定位数据存储和查询 Firestore 文档。它扩展了 Firebase Firestore 的地理空间功能,支持高效的基于位置的查询,例如查找附近的兴趣点。该库专为跨平台使用而设计,支持 Android、iOS、Web、macOS 和 Windows,非常适合需要实时位置服务的应用程序。该库可无缝集成到现有的 Firebase 项目中,并简化了在 Flutter 应用程序中处理地理定位的工作。

适合场景

  • 查找附近用户或位置
  • 基于位置的实时通知
  • 移动应用中的地理围栏
  • 位置感知搜索功能
  • 映射与空间数据可视化

主要特点

  • Firestore 中的地理空间查询
  • 跨平台支持
  • 与 Firebase 的轻松集成
  • 支持基于距离的筛选
  • 轻量且高效

适合对象

  • 基于位置的应用开发
  • Firebase 驱动的应用程序带地图
  • 实时地理定位服务
  • 带有空间数据的移动和网络应用
  • 使用 Flutter 和 Firestore 的开发人员

使用建议

  • 需要 Firebase Firestore 设置
  • 仅限 Firestore 的地理空间功能
  • 可能需要对大型数据集进行额外处理
  • 依赖于底层的 Firebase SDK
  • 不适用于仅离线的地理位置

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

GeoFlutterFire2 :earth_africa:

NB! GeoFlutterFire2 is the revisited & updated version of 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.

Heavily influenced by GeoFireX :fire::fire: from Jeff Delaney :sunglasses:

:tv: 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:
  geoflutterfire2: <latest-version>

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

dependencies:
  geoflutterfire2:
    git: git://github.com/beerstorm-net/GeoFlutterFire2.git

You should then run flutter pub 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:geoflutterfire2/geoflutterfire2.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

Warning Querying locations creates 9 separated stream listeners on firestore. One for central geo hash and 8 surrounding it. within function creates a convenient broadcast stream, if you need more than one subscriber for the locations. But it also causes memory leak, because underlying stream listeners are not cancelled when you cancel StreamSubscription from within method.

It's much safer to use withinAsSingleStreamSubscription which you can cancel, and it will also cancel 9 underlying streams as well.

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

: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);
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.
Acknowledgements

NB! GeoFlutterFire2 is a revisited and updated version of GeoFlutterFire
The work originates from GeoFlutterFire by Darshan Narayanaswamy

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目