GeoFirestore is a powerful open-source library designed to extend Firestore by enabling you to store and query documents based on their geographic location. This blog provides a user-friendly guide on how to integrate and use GeoFirestore effectively within your applications.
Table of Contents
- Downloading GeoFirestore
- Firebase Dependencies
- Example Usage
- Documentation
- Limitations & Considerations
- Contributing
Downloading GeoFirestore
You can install GeoFirestore easily via npm:
npm install geofirestore
Alternatively, you can use GeoFirestore via CDN:
<script src="https://unpkg.com/geofirestore/dist/geofirestore.js"></script>
Firebase Dependencies
If you’re using this library in a Node.js environment, make sure to install @google-cloud/firestore with a version >= 5.0.0 and < 6.0.0. If you're in a browser environment, install firebase with a version >= 9.0.0 and < 10.0.0. Be mindful that currently, GeoFirestore only works with the Firebase Compat library, but support for the Firebase Modular library is on its way!
Example Usage
Imagine you’re developing an application that lets users rate bars based on their location. By utilizing GeoFirestore, you can easily store each bar’s location and allow users to query for bars nearby. The cool part? GeoFirestore retrieves not just the location data but all bar information, allowing for a more enriched user experience.
Here’s how you can accomplish this:
import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';
import * as geofirestore from 'geofirestore';
// Initialize the Firebase SDK
firebase.initializeApp({...});
// Create a Firestore reference
const firestore = firebase.firestore();
// Create a GeoFirestore reference
const GeoFirestore = geofirestore.initializeApp(firestore);
// Reference a GeoCollection
const geocollection = GeoFirestore.collection('bars');
// Add a GeoDocument to a GeoCollection
geocollection.add({
name: 'MyBar',
score: 100,
coordinates: new firebase.firestore.GeoPoint(40.7589, -73.9851)
});
// Query nearby locations
const query = geocollection.near({
center: new firebase.firestore.GeoPoint(40.7589, -73.9851),
radius: 1000
});
query.get().then((value) => {
console.log(value.docs);
});
The above code sets up your Firestore, integrates GeoFirestore, and allows you to conduct queries for nearby bars based on the user’s location.
Documentation
For more detailed instructions and functionality, visit the GeoFirestore documentation. It closely mirrors Firestore’s capabilities, ensuring a smooth transition between the two libraries.
Limitations & Considerations
Compound Queries
GeoFirestore utilizes multiple geohashes for the queried area, which it combines into a singular response. Unlike Firestore, it currently lacks support for complex queries involving inequalities or additional filtering methods. To learn more about these limitations, check the Firestore documentation.
Data Structure
GeoFirestore requires specific data structures to function correctly. Each GeoDocument is organized as follows:
interface GeoDocumentData {
g: {
geohash: string;
geopoint: GeoPoint;
};
[field: string]: any;
}
The mandatory fields include g.geohash to make geoqueries, as well as g.geopoint for generating the geohash.
Security Rules
Ensure to adjust your Firebase Security Rules to accommodate the new GeoFirestore fields.
limit()
When applying limits to geoqueries, remember that they measure distance from the center. Since GeoFirestore handles multiple aggregated queries, the binding of limits happens on the client-side, which may result in the loading of more documents than intended.
Contributing
If you’re interested in contributing, make sure your code passes all the tests and is well documented. Pull requests should be submitted to the dev branch.
Troubleshooting
If you run into any issues, consider the following troubleshooting tips:
- Ensure that all dependencies are properly installed and updated.
- Double-check your data structure to ensure it aligns with GeoFirestore requirements.
- Verify your Firebase Security Rules to allow the necessary read/write permissions.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

