You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The library is a lightweight client for the Firebase SDK that provides tools for handling geolocation data in Firestore.
26
+
The library is a lightweight client for the Firebase Web SDK that provides tools for wrangling geolocation data in Firestore. You need a [Firebase project](https://firebase.google.com/docs/storage/web/start) to get started.
24
27
25
28
```ts
26
29
// Init Firebase
@@ -32,9 +35,9 @@ import * as geofirex from 'geofirex';
32
35
const geo =geofirex.init(firebase);
33
36
```
34
37
35
-
####Write Geo Data
38
+
### Write Geo Data
36
39
37
-
First, you'll need to add some geolocation data in your database. A `collection` creates a reference to Firestore (just like the SDK), but with some extra geoquery features. The `point` method returns a class that helps you create geolocation data.
40
+
Next, add some geolocation data in your database. A `collection` creates a reference to Firestore (just like the SDK), but with some extra geolocation tools. The `point` method returns a class that helps you create geolocation data.
38
41
39
42
```ts
40
43
const cities =geo.collection('cities');
@@ -44,9 +47,13 @@ const point = geo.point(40, -119);
Calling `point.data` returns an object that contains a [geohash string](https://www.movable-type.co.uk/scripts/geohash.html) and a [Firestore GeoPoint](https://firebase.google.com/docs/reference/android/com/google/firebase/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.
@@ -122,8 +133,55 @@ Example: `const point = geo.point(38, -119)`
122
133
-`point.distance(latitude, longitude)` Haversine distance to a point
123
134
-`point.bearing(latitude, longitude)` Haversine bearing to a point
124
135
136
+
## :pizza: Additional Features
137
+
138
+
The goal of this package is to facilitate rapid feature development with tools like MapBox, Google Maps, and D3.js. If you have an idea for a useful feature, open an issue.
139
+
140
+
### `toGeoJSON` Operator
141
+
142
+
A custom RxJS operator that transforms a collection into a [GeoJSON FeatureCollection](https://macwright.org/2015/03/23/geojson-second-bite.html#featurecollection). Very useful for tools like [MapBox](https://blog.mapbox.com/real-time-maps-for-live-events-fad0b334e4e) that can use GeoJSON to update a realtime data source.
143
+
144
+
```ts
145
+
const query =geo.collection('cars').within(...)
146
+
147
+
query.pipe( getGeoJSON() )
148
+
149
+
// Emits a single object typed as a FeatureCollection<Geometry>
150
+
{
151
+
"type": "FeatureCollection",
152
+
"features": [...]
153
+
}
154
+
```
155
+
156
+
#### Promises with `get`
157
+
158
+
Don't need a realtime stream? Convert any query observable to a promise by wrapping it with `get`.
159
+
160
+
```ts
161
+
import { get } from'geofirex';
162
+
163
+
asyncfunction getCars {
164
+
const query =geo.collection('cars').within(...)
165
+
const cars =awaitget(query)
166
+
}
167
+
```
168
+
125
169
## :zap: Tips
126
170
171
+
### Scale to Massive Collections
172
+
173
+
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 make a regular Firestore query on collection by passing a callback as the second argument, then all geoqueries will scoped these contstraints.
### Don't need a realtime stream? Use a Promise with async/await
157
-
158
-
```ts
159
-
import { get } from'geofirex';
160
-
161
-
asyncfunction getCars {
162
-
const query =geo.collection('cars').within(...)
163
-
const cars =awaitget(query)
164
-
}
165
-
```
166
-
167
214
### Always Order by `[Latitude, Longitude]`
168
215
169
216
The GeoJSON spec formats coords as `[Longitude, Latitude]` to represent an X/Y plane. However, the Firebase GeoPoint uses `[Latitude, Longitude]`. For consistency, this libary will always require you to use the latter format.
0 commit comments