MichaelSolati / geofirestore-js

Location-based querying and filtering using Firebase Firestore.

Home Page:https://geofirestore.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Near method returns no values for negative center coordinates.

mszubiczuk opened this issue · comments

  • Operating System version: macOS
  • Firebase library: "firebase-admin": "^8.10.0",
  • GeoFirestore version: 4.1.0, previously 4.0.1

Hi, I've been having issues with fetching data with negative coordinates ( South, West ) set for center parameter.
It works just fine, when center has positive longitude and latitude values.
Down below there is a code snippet of how data are being saved in firestore, and also how near query is implemented.

functions.firestore
    .document(`${CollectionNames.restaurants}/{restaurantId}`)
    .onUpdate(async (change, context) => {
        const newValue = change.after.data();
        const id = context.params['restaurantId'];
        
        const geoFirestore = new GeoFirestore(firestore());
        const documentReference = geoFirestore
            .collection(CollectionNames.restaurants)
            .doc(id);
        const document = newValue;

        document['coordinates'] = new firestore.GeoPoint(newValue.latitude, newValue.longitude);

        return documentReference.set(document);
    });

This code updates documents correctly and results in geofirestore related fields being added:
Screenshot 2020-07-03 at 13 00 46

Anyway, when longitude and latitude are positive, there are no issues whatsoever.

However, code below returns no results when lat or long or both are negative.
Also, if one or both restaurant coordinates is negative, these documents are not returned even if radius is set to extra large number ( 10000000000000000000 etc. ).

import { GeoFirestore } from 'geofirestore';
import {firestore} from "firebase-admin";

const geoFirestore = new GeoFirestore(firestore());

const geoCollection = geoFirestore.collection(CollectionNames.restaurants);

    const geoLocationSnapshot = await geoCollection
        .near({
            center: new firestore.GeoPoint(lat, long),
            radius,
        })
        .get();

So I'm partially able to recreate this issue, and partially unable...

Here is a StackBlitz where I have coordinates of [1, 1], [10, 10], [1, -1], [10, -10], [-1, -1], [-10, -10], [-1, 1], [-10, 10] (2 points for each of the 4 quadrants of a plane) and I'm able to query from [0, 0] and get all 8 of them when the radius is up to 8587 KM. Geoqueries fall apart with a radius greater than 8587 KM.

Keep in mind geohashes aren't a perfect fix for the lack of proper geoqueries in Firestore. I think I need to know what the center and radius are for your query, as well as what geopoints that you'd expect to have returned.

For reference this distance between San Francisco to London is 8611 KM, just outside of the range supported. Mind you, that's a massive space. I give that as a reference as to how much space can be covered by 8587 KM.