Firestore Geoquery

Fireship · Intermediate ·🌐 Frontend Engineering ·8y ago

Key Takeaways

Query Firestore based on real-time geolocation data using RxJS and GeoFireX

Full Transcript

[Music] real-time geolocation is behind some of the most awesome apps in the world such as uber Instagram and ways to name a few but building these features from scratch requires a lot of complex math and advanced data modeling but fortunately there's tools we can use to build features like this without any trigonometry and we can do it in the next few minutes if you're new here like and subscribe and if you're serious about building apps like this consider becoming a pro member at angular firebase com our goal today is to use firestore and angular google maps to build a real-time geolocation feature so we'll be able to say give us all the documents that are within X kilometers of some center point and have that query update in real time but before we get going I have a couple examples of real-world angular firebase apps that use geolocation features the first one is cipher links which was built by an angular firebase Pro member and is essentially a LinkedIn for coders that allows you to find programmers geographically based on a filtered set of criteria and the next one is pod mapped word which is an open source project using angular and firebase that allows you to locate podcasts from around the world check out those links in the description but the next thing I want to talk about is how geo queries actually work because they're a lot different than a normal query that you would make in firestore so we create this fractal pattern where we have a grid of numbers and letters that segment the globe then we can build out a string value by repeating this pattern over and over again inside of each cell so every additional character in the string will be an additional level of precision for the location we're going to be saving our geohashes as 9 characters which is precise to about four and a half meters squared if you go to movable-type store you can explore the bounding boxes for different geohashes if we want to query firestore for all of the documents inside of a single geo hash it's actually pretty straightforward we can set the starting point as the Geo hash the endpoint as the hash plus a high Unicode character then we can order by the property it has that hash and then do start at end at but here's the problem let's imagine Kathy and Tim our next-door neighbor's and Tim does a search within this geo hash even though Kathy's right next door he's only going to get results inside of his geo hash so to get the true results you would have to get all of the neighboring geohashes as well and then draw a radius around them to find anything within that radius and there's a bunch of other little edge cases that you'll run into as well which is why I decided to just create a library that handles all this stuff under the hood it's called geo fire X and it uses rxjs and fire a store to make these queries possible but don't worry I didn't do any of the trigonometry myself I left all that to an awesome library called turf Jas and it's compatible with both flat earth and rounder theories it's also compatible with any JavaScript framework as long as it's using firebase in rxjs let's go ahead and get started from a brand new angular app and then the first thing we'll do is install the angular Google Maps package which is a great component library if using Google Maps from your firebase project you can go to the Google cloud platform console and enable the Google Maps JavaScript API from there we can install angular Google Maps in our app as well as firebase and geo fire X now at this point if you're using angular or ionic I would recommend using the angular fire to library but the bare minimum thing you need to do is just initialize a firebase app so that should look something like this code here then you'll also want to initialize angular Google Maps with your API key which you can do here in the app module now let's create a basic map with a GM which is super easy the first thing we want to do is go into our CSS and make sure we give it a width and height of something other than zero otherwise you won't be able to see the map at all after that we can just declare an AGM map component and give it a starting the latitude and longitude point we can also drop in a marker and give it a latitude and longitude as well and that will just pop up on the map at that point what we'll do next is loop over a collection of documents and firestore and render them on the map as these markers one of the special thing is that geo Firex does is that it saves data in a format that can be queried in order to make geo queries you need to have an object on the document that contains a geo hash and a geo point the library will format this for you automatically but you can save multiple geo points on a single document so you can do geo queries against multiple parameters so let's go ahead and make a query against this existing data set I'm going to go ahead and import geo Firex as well as our firebase app then we can initialize GFI rx by passing it a reference to our firebase app and then we'll also declare a property called points which is an observable of the actual documents that we query there are several variables that are needed to make a geo query the first one is your center point which can just be a latitude and longitude you can create a geo hash with the library by calling the point method it can do a bunch of other things like return geo JSON and calculate distance and bearing and things like that then the next variable that you'll need is the radius that you'll draw around the center point which will always be in kilometers under the hood the library uses Turf j s to calculate the haversine distance which also takes into account the curvature of the earth and lastly we need to specify the object that contains the geo data because remember a single document can have multiple geo points that can be queried against then the next thing we do is make a reference to the firestore collection this is just a wrapper for the main SDK that provides some additional functionality for geolocation to perform a geo query you can use though within method that we'll take in the center radius and field and that returns an observable that you can just subscribe to and it will stay in sync with any changes that happen in real time to that collection but because we're using rxjs we can do this in a little bit more of a dynamic way for example let's say the user clicks a button that will expand the radius to a different value for that we could set up the radius as a behavior subject which itself isn't observable that can happen new values push to it then we'll define our observable points as the radius behavior subject that has then switch mapped to the actual observable geo query and I would like to point out that within returns a hot observable that is cash to the last value that means you can have multiple subscribers to it and you will only be charged for the initial document read so by setting this up with a switch map we can now change the radius and get a whole new query each time the user clicks a button for example we just need to call radius next and everything else will change reactively now we need to unwrap the observable of our locations in angular we can do this very conveniently with the async pipe but in other frameworks you'd want to call subscribe on the observable and handle it accordingly inside of our AGM marker we'll do an ng for loop and we'll loop over each point of points using the async pipe to unwrap it because it returns an observable array of document data this gives us access to all of the data on each document but it also returns some additional metadata that's specifically designed for building real-time map features for example it returns the distance and bearing that this document was found at relative to the query center point which can only be figured out at runtime you might query a collection of restaurants that are relative to a user's current GPS position on their phone and the query metadata gives you the information needed to show the distance to that point to the user in the UI and that will be by default on the query metadata on the return points I have a premade demo that I'd use for integration testing and every point in the document is a specific distance and bearing from the center and the actual gray circle is the radius of the query and if we click on any of these points we'll also see that query metadata which again is the distance and bearing what's really cool that fire store is everything is just real time by default so imagine we have one of these documents that are being written to every couple of seconds as the users position moves it will update in the UI like we see happening here with the blue marker but as soon as it falls out of range it disappears from the query and also the UI there's a bunch of other little features that I've added to geo by rx specifically for working with tools like map box and Google Maps if you find it useful make sure to star it and please report any issues that you encounter when using it I'm gonna go ahead and wrap things up there thanks so much for watching and I'll talk to you soon [Music]

Original Description

Query Firestore based on realtime geolocation data using RxJS. https://github.com/codediodeio/geofirex - PRO https://angularfirebase.com/pro - CyferLinx https://www.cyferlinx.com/ - PodMap https://podmap.org/ - AGM https://angular-maps.com/ - Firestore https://firebase.google.com/docs/firestore/ - GeoHash Viewer https://www.movable-type.co.uk/scripts/geohash.html
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fireship · Fireship · 0 of 60

← Previous Next →
1 Angular 4 Development and Production Environments with Firebase
Angular 4 Development and Production Environments with Firebase
Fireship
2 OAuth with Angular and Firebase Tutorial
OAuth with Angular and Firebase Tutorial
Fireship
3 Anonymous Authentication with Angular and Firebase - Lazy Registration
Anonymous Authentication with Angular and Firebase - Lazy Registration
Fireship
4 Angular Router Guards for Firebase Users
Angular Router Guards for Firebase Users
Fireship
5 Angular Firebase CRUD App with NoSQL Database Tutorial
Angular Firebase CRUD App with NoSQL Database Tutorial
Fireship
6 Upload Files from Angular to Firebase Storage
Upload Files from Angular to Firebase Storage
Fireship
7 How to Deploy an Angular App to Firebase Hosting
How to Deploy an Angular App to Firebase Hosting
Fireship
8 Sharing Data between Components in Angular
Sharing Data between Components in Angular
Fireship
9 Loading Spinners for Asynchronous Firebase Data
Loading Spinners for Asynchronous Firebase Data
Fireship
10 Angular 4 Transactional Email with Google Firebase Cloud Functions
Angular 4 Transactional Email with Google Firebase Cloud Functions
Fireship
11 Firebase Database Rules Tutorial
Firebase Database Rules Tutorial
Fireship
12 Autocomplete Search with Angular4 and Firebase
Autocomplete Search with Angular4 and Firebase
Fireship
13 Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Fireship
14 Angular Drag-and-Drop File Uploads to Firebase Storage
Angular Drag-and-Drop File Uploads to Firebase Storage
Fireship
15 Text Translation with Firebase Cloud Functions onWrite and Angular 4
Text Translation with Firebase Cloud Functions onWrite and Angular 4
Fireship
16 Custom Usernames with Firebase Authentication
Custom Usernames with Firebase Authentication
Fireship
17 Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Fireship
18 Simple Pagination with Firebase and Angular 4
Simple Pagination with Firebase and Angular 4
Fireship
19 How to Connect Firebase Users to their Data - 3 Methods
How to Connect Firebase Users to their Data - 3 Methods
Fireship
20 Add Toast Message Notifications to your Angular App
Add Toast Message Notifications to your Angular App
Fireship
21 Facebook-Inspired Reactions System with Angular and Firebase
Facebook-Inspired Reactions System with Angular and Firebase
Fireship
22 Learn NgModule in Angular with Examples
Learn NgModule in Angular with Examples
Fireship
23 Lazy Loading Components in Angular 4
Lazy Loading Components in Angular 4
Fireship
24 Stripe Checkout Payments with Angular and Firebase - Part 1
Stripe Checkout Payments with Angular and Firebase - Part 1
Fireship
25 Process Stripe Payments with Firebase Cloud Functions - Part 2
Process Stripe Payments with Firebase Cloud Functions - Part 2
Fireship
26 Selling Digital Content in Angular with Stripe Payments - Part 3
Selling Digital Content in Angular with Stripe Payments - Part 3
Fireship
27 Angular 4 Full Text Search with Algolia - Part 1
Angular 4 Full Text Search with Algolia - Part 1
Fireship
28 Algolia with Firebase Cloud Functions - Part 2
Algolia with Firebase Cloud Functions - Part 2
Fireship
29 Firebase Phone Authentication in Angular 4
Firebase Phone Authentication in Angular 4
Fireship
30 Top 7 RxJS Concepts for Angular Developers
Top 7 RxJS Concepts for Angular Developers
Fireship
31 Learn Angular Animations with 5 Examples
Learn Angular Animations with 5 Examples
Fireship
32 Advanced Firebase Data Filtering (Multi-Property)
Advanced Firebase Data Filtering (Multi-Property)
Fireship
33 Realtime Maps with Mapbox + Firebase + Angular
Realtime Maps with Mapbox + Firebase + Angular
Fireship
34 Angular Reactive Forms with Firebase Database Backend
Angular Reactive Forms with Firebase Database Backend
Fireship
35 Send Push Notifications in Angular with Firebase Cloud Messaging
Send Push Notifications in Angular with Firebase Cloud Messaging
Fireship
36 Top 7 Ways to Debug Angular 4 Apps
Top 7 Ways to Debug Angular 4 Apps
Fireship
37 Infinite Scroll with Angular and Firebase
Infinite Scroll with Angular and Firebase
Fireship
38 Use TypeScript with Firebase Cloud Functions
Use TypeScript with Firebase Cloud Functions
Fireship
39 Realtime Graphs and Charts with Plotly and Firebase
Realtime Graphs and Charts with Plotly and Firebase
Fireship
40 Role-Based User Permissions in Firebase
Role-Based User Permissions in Firebase
Fireship
41 User Presence System in Realtime - Online, Offline, Away
User Presence System in Realtime - Online, Offline, Away
Fireship
42 Location-based Queries with GeoFire and Angular Google Maps
Location-based Queries with GeoFire and Angular Google Maps
Fireship
43 Angular ngrx Redux Quick Start Tutorial
Angular ngrx Redux Quick Start Tutorial
Fireship
44 Angular Ngrx Effects with Firebase Database
Angular Ngrx Effects with Firebase Database
Fireship
45 Progressive Web Apps with Angular
Progressive Web Apps with Angular
Fireship
46 Angular Ngrx with Firebase Google OAuth User Authentication
Angular Ngrx with Firebase Google OAuth User Authentication
Fireship
47 RxJS Quick Start with Practical Examples
RxJS Quick Start with Practical Examples
Fireship
48 Send SMS Text Messages with Twilio and Firebase
Send SMS Text Messages with Twilio and Firebase
Fireship
49 Firebase Database Performance Profiling
Firebase Database Performance Profiling
Fireship
50 Native Desktop Apps with Angular and Electron
Native Desktop Apps with Angular and Electron
Fireship
51 Subscription Payments with Stripe, Angular, and Firebase
Subscription Payments with Stripe, Angular, and Firebase
Fireship
52 Firestore with AngularFire5 Quick Start Tutorial
Firestore with AngularFire5 Quick Start Tutorial
Fireship
53 Angular HTTP Client Quick Start Tutorial
Angular HTTP Client Quick Start Tutorial
Fireship
54 Google Sign-In with Firestore Custom User Data
Google Sign-In with Firestore Custom User Data
Fireship
55 Star Review System from Scratch with Firestore + Angular
Star Review System from Scratch with Firestore + Angular
Fireship
56 Angular Chatbot with Dialogflow (API.ai)
Angular Chatbot with Dialogflow (API.ai)
Fireship
57 Learn @ngrx/entity and Feature Modules
Learn @ngrx/entity and Feature Modules
Fireship
58 Infinite Scroll Pagination with Firestore
Infinite Scroll Pagination with Firestore
Fireship
59 Faster Firestore via Data Aggregation
Faster Firestore via Data Aggregation
Fireship
60 Contentful - CMS for Angular Progressive Web Apps
Contentful - CMS for Angular Progressive Web Apps
Fireship

Related Reads

📰
React Introduction
Learn the basics of ReactJS and how to build dynamic user interfaces with this popular JavaScript library
Dev.to · Karthick (k)
📰
Why SnapDOM Beats html2canvas for DOM-to-Image Capture
Learn why SnapDOM outperforms html2canvas for DOM-to-image capture and how to use it in your frontend projects
Dev.to · Juan Martin
📰
I built 42 landing page templates as single HTML files (no npm, no build step)
Learn how to create simple landing page templates as single HTML files without relying on npm or build steps, and why this approach matters for efficient web development
Dev.to · Segcam spa
📰
Part 7B — Section 2 — React Event Handling Explained: Forms, Event Object & User Input.
Learn React event handling for forms and user input to improve your frontend skills
Medium · JavaScript
Up next
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →