GeoFlutterFire는 지리적 위치 데이터를 사용하여 Firebase Firestore 문서를 저장하고 쿼리할 수 있게 해주는 오픈소스 플러터 패키지입니다. 근처의 위치를 찾거나 특정 점이 영역 내에 있는지 확인하는 등의 공간 쿼리를 가능하게 함으로써 Firestore의 기능을 확장합니다. Firebase와의 원활한 통합을 위해 설계되었으며, Android, iOS, Web, macOS 플랫폼을 지원합니다. 이 라이브러리는 모바일 및 웹 애플리케이션에서 지리공간 작업을 간단하게 만들어 주며, 위치 기반 서비스에 적합합니다.
사용 사례
근처 사용자 또는 관광지 찾기
위치 인식 소셜 앱 만들기
배송 또는 라이드셰어링 기능 구현
지오패싱 트리거 생성
실시간 위치 데이터 매핑
주요 기능
Firestore에서 지리공간 쿼리
위도/경도 좌표 지원
Firebase Realtime Database 통합
효율적인 근접 검색
크로스 플랫폼 호환성
적합한 대상
위치 기반 앱을 개발하는 개발자
지역 정보와 Firebase를 사용하는 팀이 필요로 하는 것
실시간 근접 감지가 필요한 앱
지도 기능을 갖춘 모바일 및 웹 애플리케이션
고려 사항
Firebase Firestore 설정이 필요합니다
자주 쿼리를 수행하면 비용이 더 높아질 수 있습니다
정확한 결과를 얻으려면 정확한 GPS 데이터가 필요합니다
지원되는 플랫폼(안드로이드, iOS, 웹, macOS)에 한정
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
GeoFlutterFire :earth_africa:
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.
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 geoFirePointGeoFirePoint center = geo.point(latitude: 12.960632, longitude: 77.641603);
// get the collection reference or queryvar 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.
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
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
Using Firestore withConverter method
If you want to use the collections or queries with this method, you should use GeoFlutterFire().collectionWithConverter<T>({ required Query<T> collectionRef }) instead of GeoFlutterFire().collection({ required Query<Map<String, dynamic>> collectionRef }).
Despite the different initializers, the only difference is in the within method, where in the former initialization you have a extra param that needs to return the GeoPoint from the data type in the query.
: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 supported 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.