Nearby GeoPoints with firestore

2

The issue is that I want a firebase function, in which a geopoint is passed (latitude and longitude) and from this, I find the nearby places that I have in the BBDD.

The only thing I have is this equation:

(lonBD−lonCliente)^2+(latBD−latCliente)^2 < (20Km)^2

Where 20km does not go in km, there are units that are 111 km each. Therefore they are 20/11 = 0'180 period

(lonBD−lonCliente)^2+(latBD−latCliente)^2 < (0'180)^2

The issue is that I do not want to go through all the rows in the database. I think it would be best if this formula were the where of the query. But I am unable to find the Math.pow or how to multiply to build the where in firestore.

For now I have this, which of course, does not work:

module.exports = functions.https.onRequest((req, res) => {
   var firestore = admin.firestore();
   var latCliente = req.query.lat;
   var lonCliente = req.query.lon;
   firestore.collection('sitios')
     .where("(geo_coor.getLongitude()−lonCliente) * (geo_coor.getLongitude()−lonCliente) + (geo_coor.getLatitude−latCliente) * (geo_coor.getLatitude−latCliente)", "<", 0.03314)
     .get()
     .then(function(querySnapshot) {
         //console.log(querySnapshot.docs[0].data())
         res.jsonp(querySnapshot.docs.map(function (documentSnapshot) {
            return documentSnapshot.data();
         }));
     });
 });

And the content of the BD is:

Thank you very much in advance.

    
asked by Txmx 15.02.2018 в 21:00
source

1 answer

1

At the moment you can not perform the query as you want to do, in the documentation GeoPoint type, only allows the equalization function:

link

Just as a reference there is this answer: link

I recommend you use this lib: link (findNearest method), however as you mention, you will have to go through the whole data set.

UPDATE (sample code):

import * as geolib from 'geolib';
        //...
        const sitioActual = {latitude:req.query.lat, longitude:req.query.lon};
        console.log("sitioActual",sitioActual);

        const coords:geolib.PositionAsDecimal = sitioActual;

        console.log("coords",coords);

        db.collection('sitios').get()
        .then((snapshot) => {

            const sitiosActuales:geolib.PositionAsDecimal[] = [];

            snapshot.forEach((doc) => {
                sitiosActuales.push( doc.data().coords );
            });

            console.log("sitiosActuales",sitiosActuales);

            const lugaresMasCercanos:geolib.Distance = geolib.findNearest(coords, sitiosActuales, 1);
            console.log("lugaresMasCercanos",lugaresMasCercanos);

            if(lugaresMasCercanos){

                console.log("Aquí verás los lugares Mas Cercanos",lugaresMasCercanos);

            }else{
                console.log('No existen lugares cercanos');
             }

        })
        .catch((err) => {
            console.log('Error en consulta: ', err);
            //Maneja tu error de la consulta aquí
        });
    
answered by 30.04.2018 / 22:56
source