Nearby GeoPoints with firestore


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 =;
   var lonCliente = req.query.lon;
     .where("(geo_coor.getLongitude()−lonCliente) * (geo_coor.getLongitude()−lonCliente) + (geo_coor.getLatitude−latCliente) * (geo_coor.getLatitude−latCliente)", "<", 0.03314)
     .then(function(querySnapshot) {
         res.jsonp( (documentSnapshot) {

And the content of the BD is:

Thank you very much in advance.

asked by Txmx 15.02.2018 в 21:00

1 answer


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


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 = {, longitude:req.query.lon};

        const coords:geolib.PositionAsDecimal = sitioActual;


        .then((snapshot) => {

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

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


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


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

                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