You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Contents
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
Location Queries With Firebase GeoFire and Angular Google Maps (AGM)
Episode 42 written by Jeff DelaneyQuerying data based on its proximity to a user’s GPS location is critical for location-driven apps.
In this lesson, we are going to use Firebase GeoFire to make GPS location queries in Angular, then display the results using the Google Maps JavaScript API with some help from the Angular Google Maps package. The end result is a feature that will (1) determine the GPS location of the user and (2) display the location of datapoints within a certain radius of the user’s location.
Prerequisites
This tutorial assumes you already have Firebase and AngularFire2 configured in an Angular 2+ project. If you need to get up to speed, follow the initial setup guide in the AngularFire2 docs.
Setting Up Google Maps in Angular
Let’s get this lesson started by building a basic Google Map with the user’s current location.
Installing Angular Google Maps (AGM)
We will be taking advantage of Angular Google Maps (AGM) - a package designed to help us build Google Maps quickly in Angular. It wraps common Google Maps elements into Angular components and directives, along with the ability to customize their properties and events. In this example, we will be using AGM to handle the map itself, location markers, and popup windows.
npm install @agm/core --save |
Obtain your Google Maps API Key
Log into the Google Cloud Platform Console, then click on APIs and Services. From here, click on Enable APIs and Services and select the Google Maps JavaScript API. Once enabled, you can retrieve the API key from the credentials page.
In Angular, it’s a good practice to keep this API key in an environment file.
environment.ts
export const environment = { |
app.module.ts
Now we can add AGM to the app module with the Google Maps API key.
|
Build a Basic Map with AGM
Let’s start by building a component that will display the map.
ng generate component google-map |
google-map.component.html
AGM’s built-in components make it easy to flesh out a map in the HTML. To start, we will center the map on the user’s current location, then add a marker to their exact GPS coordinates. We will also add a popup window that appears when the marker is clicked. The agm-info-window
uses Angular transclusion to allow custom HTML to be nested inside of it.
<div *ngIf="lat && lng"> |
google-map.component.ts
In the component, we can obtain the user’s current location using the global navigator.geolocation
object - learn more. This data is then used to define lat
and lng
variables on the component.
import { Component, OnInit } from '@angular/core'; |
Now we have a solid starting map to build upon with GeoFire in the next section.
Working with GeoFire
First, install GeoFire via NPM.
npm install geofire --save |
The main advantages of working with Firebase GeoFire include its ability to:
- Query items within a specific geographic radius
- Calculate distance between points
- Set GPS coordinates in a standardized format
geo.service.ts
All GeoFire query and update logic will be handled in a service.
ng generate service geo --module app |
A GeoFire query will return the key, coordinates, and distance from a given starting point. Imagine you have a database filled with coffee shop locations in San Francisco. If you query all coffee shops within 100km of a given point, GeoFire returns the keys for the coffee shops within this radius, as well as their distance from the starting point.
In this example, I am saving the GeoFire query data in a BehaviorSubject
so it can be observed and updated throughout the app.
import { Injectable } from '@angular/core'; |
Seeding the Database
To demonstrate GeoFire, I am seeding the database with some dummy GPS coordinates. Here’s a simple script you can use to seed your database with some initial data.
private seedDatabase() { |
Displaying GeoFire Data in Google Maps
Now we can use our current user’s location in the component as the starting point for making a query. Once we have the current user’s location, we can run the GeoFire query that will update the markers
.
import { Component, OnInit, OnDestroy } from '@angular/core'; |
In the component html, we simply loop over the markers with *ngFor
and display the relative distance from the current user when they click the marker.
<agm-marker *ngFor="let marker of markers" |
That’s it for GeoFire with Angular Google Maps. I Let me know what you think in the comments.