Nearby Location Queries with Cloud Firestore
Use Geohashing to retrieve nearby locations in Google’s Cloud Firestore.
Querying for nearby places is an almost essential feature to many web and mobile applications. Let’s dive into how you could implement this feature with Google Cloud Firestore.
The Problem
Typically when searching for nearby locations, you would need to create a range of latitude and longitude coordinates that could be used to capture a list of restaurants, stores, etc. However, Cloud Firestore queries can only perform range filtering on a single field. This means that we are unable to query on both latitude and longitude as separate fields. So, can we combine them?
Yes, and luckily an algorithm for this already exists! Someone else has already solved this problem for us — it’s called Geohashing.
Essentially, Geohashing encodes latitude and longitude coordinates into a single string of characters — a Geohash. These strings of characters are formatted in such a way that nearby coordinates will generate similar Geohashes. This is very useful for something like a range filter in a Firestore query.
Getting Started
Let’s start by pulling in the Geohashing library.npm install ngeohash --save
Also, be sure that you have installed the Google Cloud Firestore library.npm install @google-cloud/firestore --save
Firestore Document Structure
In order to use Geohashing in your query, you’ll need to ensure that all of the documents — that represent places in your database — have a precalculated Geohash based on the corresponding latitude and longitude. We can calculate this Geohash using the library’s encode
method.
It is important that these documents have a geohash
field before performing the query. This goes back to solving our original problem of only being able to perform range filters on one field in Firestore queries.
So let’s say, for example, that we’re querying for nearby restaurants. The given Firestore documents should look similar to this.
Performing the Nearby Query
The following example illustrates how you could perform a nearby location query from a given latitude, longitude, and distance in miles. I’ve included a utility function that generates two hashes — one for the upper boundary and one for the lower boundary.
I’ve also included the code necessary to retrieve the client’s current coordinates using the Geolocation API.
This should be all you need to perform nearby location queries using Cloud Firestore! I hope this post helps you in your endeavors of creating applications and allows you to better understand the basics of Geohashing and why we need to use it when using Firestore queries.
If you have any questions at all, feel free to leave a response or message me, and I would love to help. I hope this post brings you value, and I would greatly appreciate if you could take the time to share this with anyone that could benefit from this knowledge. Have a great day!
Using the Geolocation API in React Native
Also, if you are interested, I recently wrote up this modern tutorial on how you could go about using Geolocation in React Native using React Hooks.