I am implementing an app that contains a map. In this map, the user can select its location, in order to find items of other users around his radius.
In order to do that, when a user writes an item into Firestore, I am just reverse geocoding the selected location, in a specific language (english), in order to get the name of the address (in english) and store it as a normal string field in a new Firestore doc.
Just like this:
export const GOOGLE_MAPS_GEOCODING_ENDPOINT = (
latitude,
longitude,
key,
language = "en"
) =>
`https://maps.googleapis.com/maps/api/geocode/json?` +
`latlng=${latitude},${longitude}&key=${key}&language=${language}`;
export default async (coordinate) => {
const endpoint = GOOGLE_MAPS_GEOCODING_ENDPOINT(
coordinate.latitude,
coordinate.longitude,
Constants.manifest.ios.config.googleMapsApiKey
);
const res = await fetch(endpoint);
const data = await res.json();
// Get the address components
const addressComponents = data.results[0]?.address_components;
// Build the location object
const location = buildLocation(addressComponents);
return location;
};
I implemented this some years ago, and I have recently read about Firestore GeoQueries (not sure if it was added the past year or recently, but I have never use it).
Currently, I am just performing queries like:
// Fetching items near me
const query = db
.collection("items")
.where("random", "<", randomId)
.where("type", "==", type)
.where("location.city", "==", city)
.orderBy("random", "desc");
As you can see, I am using the country field to perform some kind of "GeoQuery". But I don't like the idea of having to store addresses in the database, with the restriction that, regardless of the internationalization of the app, they must be in English.
Is it currently possible to combine the Firestore GeoQuery feature to perform "complex" queries as the one I described?
I mean, getting random items near me which price is lower than X, or stuff like that (talking about latitude and longitude, not addresses).
Becuase, as I have read in the documentation, we have to order the collection by geohashes... and in my case I need to get random docs inside some bounds. So something like:
const q = db.
.collection("items")
.where("randomId", "<=", randomId)
.orderBy("geohash")
.startAt(b[0])
.endAt(b[1]);
will not work, as the first orderBy should be "randomId". Is there any example or workaround? Should I use sub-collections (containing items with similar bounds (in a range)) instead?