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
Copy file name to clipboardExpand all lines: README.md
+56-29Lines changed: 56 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# GeoFireX - Realitme GeoLocation Tools for Firestore with RxJS
1
+
# GeoFireX - Realitme GeoLocation with Firestore & RxJS
2
2
3
3
Live Demo
4
4
@@ -8,35 +8,34 @@ Live Demo
8
8
npm install firebase geofirex
9
9
```
10
10
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.
12
12
13
13
```ts
14
14
// Initnalize Firebase
15
-
// var firebase = require('firebase/app')
16
15
import*asfirebasefrom'firebase/app';
17
-
firebase.initializeApp(config);
16
+
firebase.initializeApp(yourConfig);
18
17
19
18
// Initialize the client
20
19
import*asgeofirexfrom'geofirex';
21
-
constgfx=geofirex.init(firebase);
20
+
constgeo=geofirex.init(firebase);
22
21
```
23
22
24
23
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.
`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
59
57
60
-
### `point`
58
+
### `collection`
61
59
62
-
Returns a GeoHash instance
60
+
Returns a GeoFireCollectionRef instance
63
61
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`
69
63
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
72
65
73
66
## Tips
74
67
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
76
82
77
-
I recommend
83
+
```ts
84
+
const radius =newBehaviorSubject(1);
85
+
const cities =db.collection('cities');
86
+
87
+
const points =this.radius.pipe(
88
+
switchMap(rad=> {
89
+
returncities.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
+
asyncfunction getCars {
103
+
const query =geo.collection('cities').within(...)
104
+
const cities =awaitget(query)
105
+
}
106
+
```
78
107
79
108
### Always Order by `[Latitude, Longitude]`
80
109
81
110
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