Skip to content

Commit 91a8102

Browse files
committed
update: general api changes
1 parent 8ba47d5 commit 91a8102

24 files changed

Lines changed: 1801 additions & 769 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
.rpt2_cache/
2+
.rpt2_cache/
3+
dist/

README.md

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# GeoFireX - Realitme GeoLocation Tools for Firestore with RxJS
1+
# GeoFireX - Realitme GeoLocation with Firestore & RxJS
22

33
Live Demo
44

@@ -8,35 +8,34 @@ Live Demo
88
npm install firebase geofirex
99
```
1010

11-
The client is a lighweight wrapper for the Firebase SDK.
11+
The library is a lightweight client for the Firebase SDK that provides tools for handling geolocation data in Firestore.
1212

1313
```ts
1414
// Initnalize Firebase
15-
// var firebase = require('firebase/app')
1615
import * as firebase from 'firebase/app';
17-
firebase.initializeApp(config);
16+
firebase.initializeApp(yourConfig);
1817

1918
// Initialize the client
2019
import * as geofirex from 'geofirex';
21-
const gfx = geofirex.init(firebase);
20+
const geo = geofirex.init(firebase);
2221
```
2322

2423
First, you'll need to add some geolocation data in your databse. A `collection` creates a reference to Firestore (just like the SDK), but with some extra geoquery features. The `geohash` method returns a class that helps you create geolocation data.
2524

2625
```ts
27-
const cities = gfx.collection('cities');
26+
const cities = geo.collection('cities');
2827

29-
const location = gfx.geohash(40, -119);
28+
const point = geo.point(40, -119);
3029

31-
cities.add({ name: 'Phoenix', location });
30+
cities.add({ name: 'Phoenix', position: point.data });
3231
```
3332

34-
Now let's make a query Firestore for _cities.location within 100km radius of a centerpoint_.
33+
Now let's make a query Firestore for _cities.position within 100km radius of a centerpoint_.
3534

3635
```ts
37-
const center = gfx.point(40.1, -119.1);
36+
const center = geo.point(40.1, -119.1);
3837
const radius = 100;
39-
const field = 'location';
38+
const field = 'position';
4039

4140
const query = cities.within(center, radius, field);
4241
```
@@ -45,39 +44,67 @@ The query returns a realtime Observable of the document data + some additional m
4544

4645
```ts
4746
query.subscribe(console.log);
48-
// { ...data, geoQueryData: { distance: 1.23232, bearing: 230.23 } }
47+
// { ...documentData, queryMetadata: { distance: 1.23232, bearing: 230.23 } }
4948
```
5049

5150
## API
5251

53-
### `collection`
52+
Coming soon...
5453

55-
Returns a collection reference with
54+
### `init`
5655

57-
`within()` - Returns an Observable of queried based distance
58-
`data()` - Returns an Observable of queried documents mapped to the data payload
56+
Initializes the GeoFireClient
5957

60-
### `point`
58+
### `collection`
6159

62-
Returns a GeoHash instance
60+
Returns a GeoFireCollectionRef instance
6361

64-
`data` - Returns data in recommended format for database writes
65-
`coords` - Returns [lat, lng]
66-
`hash` - Returns geohash string at precision 9
67-
`geoPoint` Returns a firebase GeoPoint object
68-
`geoJSON` Returns data as a GeoJSON `Feature<Point>`
62+
### `point`
6963

70-
`distance(to)` Calulates the haversine distance to a point.
71-
`bearing(to)` Calulates the bearing to point to a point.
64+
Returns a GeoFirePoint instance
7265

7366
## Tips
7467

75-
### Save the `gfx.point` data as a document field
68+
### Seeing this error: `DocumentReference.set() called with invalid data`
69+
70+
Firestore writes cannot use custom classes, so make sure to call the `data` getter on the point.
71+
72+
```ts
73+
const point = geo.point(40, -50);
74+
// This is an ERROR
75+
ref.add({ location: point });
76+
77+
// This is GOOD
78+
ref.add({ location: point.data });
79+
```
80+
81+
### Making Dynamic Reatime Queries the RxJS Way
7682

77-
I recommend
83+
```ts
84+
const radius = new BehaviorSubject(1);
85+
const cities = db.collection('cities');
86+
87+
const points = this.radius.pipe(
88+
switchMap(rad => {
89+
return cities.within(center, rad, 'point');
90+
})
91+
);
92+
93+
// Now update your query
94+
radius.next(23);
95+
```
96+
97+
### Don't need a realtime stream? Use a Promise with async/await
98+
99+
```ts
100+
import { get } from 'geofirex';
101+
102+
async function getCars {
103+
const query = geo.collection('cities').within(...)
104+
const cities = await get(query)
105+
}
106+
```
78107

79108
### Always Order by `[Latitude, Longitude]`
80109

81110
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.
82-
83-
## Contribute

0 commit comments

Comments
 (0)