Search in Firebase based on distance

firebase geoquery
firebase location query
firestore geopoint query
flutter firestore geoquery
geofire firestore

I'm migrating an application that was using Mysql to firebase, and I managed to migrate it almost all, just missing the part of search, I did not quite understand how it works. I'm trying to do this query below in firebase.

SELECT *, (6371 * acos( cos(radians(?)) * cos(radians(latitude)) * cos(radians(?) - radians(longitude)) + sin(radians(?)) * sin(radians(latitude)) )) AS distance FROM usuario ORDER BY distance

What I'm trying to do is pass the latitude and longitude of my User to firebase and he returns me all users ordering the nearest to the farthest.

Note: This is not the same query that I use in my application, that I just find on google and do not know if this work, but you can get an idea of what I'm trying to do. Note ²: English is not my native language :P

The example SQL query you provided uses a capability of SQL that does not exist in Firebase: the ability to calculate an expression using the column values for each row in the table and then use the value of that expression to filter and sort the query result.

I don't see a way to use Firebase to perform the type of query you want.

You might want to take look at the Geofire library. I have not used it and its capabilities seem to be related to proximity filtering, not the sort-by-distance feature you need, but maybe you can adjust your requirements to make use of its features.

Can Firestore return GeoPoints sorted by distance? : Firebase, Anyone know how/if I can query them and sort by distance without using a third management and the costs, and I can't seem to find a great breakdown of that. products and services built using Firebase from the people who built them? To enable full text search of your Cloud Firestore data, use a third-party search service like Algolia. Consider a note-taking app where each note is a document: // /notes/${ID} { owner: "{UID}", // Firebase Authentication's User ID of note owner text: "This is my first note!"

I concur with Qbix and would normally post this as a comment, but I want to make sure the Geohash comment doesn't get lost in the small-text shuffle.

GeoFire uses Geohash codes to create its keys and allow range-matching. For basic apps this may be fine, but the second you go beyond the US this creates a lot of trouble because it doesn't work well around the equator and Prime Meridian (UK). See the Wikipedia Page on Geohash for details, specifically: Edge case locations close to each other but on opposite sides of the 180 degree meridian will result in Geohash codes with no common prefix (different longitudes for near physical locations)..

Firebase is an amazing product, but not a one-size-fits-all tool. If you need good Geo-based search/matching, use a tool like ElasticSearch, MySQL, Algolia, etc. that support it directly. Reducing the number of components in cases like this doesn't decrease complexity, it increases it.

Interface: LatLng, Calculate distance between two LatLng points in distance (meters). Parameter. other. rules.LatLng. The other point. Value must not� The app needs two location-based features: Localized search, to find planes that are located within the preset radius from the tower. As planes enter and leave the airspace, the search results must change too. Real-time location tracking, to ensure plane locations in the app are up-to-date.

You can use Geofirestore library

here is the example used:

import * as firebase from 'firebase/app';
import 'firebase/firestore';
import { GeoCollectionReference, GeoFirestore, GeoQuery, GeoQuerySnapshot } from 'geofirestore';

// Initialize the Firebase SDK
  // ...

// Create a Firestore reference
const firestore = firebase.firestore();

// Create a GeoFirestore reference
const geofirestore: GeoFirestore = new GeoFirestore(firestore);

// Create a GeoCollection reference
const geocollection: GeoCollectionReference = geofirestore.collection('restaurants');

// Create a GeoQuery based on a location
const query: GeoQuery = geocollection.near({ center: new firebase.firestore.GeoPoint(40.7589, -73.9851), radius: 1000 });

// Get query (as Promise)
query.get().then((value: GeoQuerySnapshot) => {
  console.log(; // All docs returned by GeoQuery

Realtime GeoQueries with Firestore, As planes enter and leave the airspace, the search results must change distance calculation with latitude-longitude pairs, implement efficient and perform location based searches that reflect location updates in real-time. Does the new firestore database from firebase natively support location based geo queries? i.e. Find posts within 10 miles, or find the 50 nearest posts? I see that there are some existing projects for the real-time firebase database, projects such as geofire- could those be adapted to firestore as well?

GeoFire: Location Queries for Fun and Profit, Place Search returns a list of places based on a user's location or search string. text-based geographic searches, returning suggested queries as users type. Import & sync data between Firebase & Algolia and build search on top of your Firebase app. - Searching Firebase Realtime Database - Sending and Managing Data Algolia: across 15 languages, read up on Algolia concepts, get access to tutorials with concrete use-cases and sample datasets, or explore our API Reference.

Overview | Places API, The Distance Matrix API is a service that provides travel distance and time for a matrix of origins and destinations, based on the recommended route between� A special feature of GeoFireX is that we can call point.queryMetadata.distance to display the point’s relative distance from the query center point, while also accounting for the curvature of the earth (thanks Turf.js).

Get Started | Distance Matrix API, GeoFirestore uses the Firebase Cloud Firestore for data storage, allowing query results and you want to add the possibility for users to search for bars in their vicinity. Limits on geoqueries are applied based on the distance from the center. There are two ways to retrieve data stored in Cloud Firestore. Either of these methods can be used with documents, collections of documents, or the results of queries:

  • Thanks for the answer. I think this lib does not work anymore, because I was trying to implement it and it is asking to implement "Firebase.setAndroidContext ()" which is the old way of using Firebase.
  • You're right. An upgrade is underway but it's not ready yet.