// Tutorial //

How To Build Maps in Angular with Leaflet, Part 3: The Popup Service

Published on November 8, 2019 · Updated on March 29, 2021
Default avatar
By Chris Engelsma
Developer and author at DigitalOcean.
How To Build Maps in Angular with Leaflet, Part 3: The Popup Service


Leaflet supports popups. When clicking on markers or specified regions on the map, popups containing text will appear. This provides a way to display additional information on a map.

Note: This is Part 3 of a 4-part series on using Angular and Leaflet.

In this tutorial, you will learn how to add popups to your map using a service to manage the popup logic.


To complete this tutorial, you will need:

  • This tutorial builds directly upon the installation and steps in previous parts.

Step 1 — Creating the Popup Service

At this point, you should have a working implementation of Leaflet in an Angular application.

Use your terminal window to navigate to the project directory. Then, run the following command to generate a new service:

  1. npx @angular/cli generate service popup --skip-tests

This will create a new file: popup.service.ts.

Next, you will add this new service as a provider in your app.module.ts.

Open app.module.ts in your code editor and make the following changes:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { HttpClientModule } from '@angular/common/http';
import { MarkerService } from './marker.service';
import { PopupService } from './popup.service';

import { AppComponent } from './app.component';
import { MapComponent } from './map/map.component';

  declarations: [
  imports: [
  providers: [
  bootstrap: [AppComponent]
export class AppModule { }

Your application now supports your new PopupService.

Step 2 — Displaying the Popups

Open your newly created popup.service.ts in your code editor. And declare a makeCapitalPopup function:

import { Injectable } from '@angular/core';

  providedIn: 'root'
export class PopUpService {
  constructor() { }

  makeCapitalPopup(data: any): string { }

This function will create popups that contain data from the GeoJSON file that was used in Part 2.

Add the capital’s name, state, and population:

import { Injectable } from '@angular/core';

  providedIn: 'root'
export class PopUpService {
  constructor() { }

  makeCapitalPopup(data: any): string {
    return `` +
      `<div>Capital: ${ data.name }</div>` +
      `<div>State: ${ data.state }</div>` +
      `<div>Population: ${ data.population }</div>`

Then, you will need to revisit the MarkerService and import the PopupService:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import * as L from 'leaflet';
import { PopupService } from './popup.service';

And inject the PopupService:

  private http: HttpClient,
  private popupService: PopupService
) { }

By adding the services to the providers stanza within app.modules.ts, you are creating the services as singleton providers to the entire application, so Angular will not complain about dependency.

While still in the MarkerService file, add the bindPopup() method:

makeCapitalCircleMarkers(map: L.map): void {
  this.http.get(this.capitals).subscribe((res: any) => {
    const maxVal = Math.max(...res.features.map(x => x.properties.population), 0);

    for (const c of res.features) {
      const lat = c.geometry.coordinates[0];
      const lon = c.geometry.coordinates[1];
      const circle = L.circleMarker([lon, lat], {
        radius: MarkerService.ScaledRadius(c.properties.population, maxVal)



Save your changes. Then, stop your application and relaunch it. Open the application in your web browser (localhost:4200) and interact with one of the markers:

Screenshot of a map with a popup displaying information about Austin, Texas.

You now have a map that supports popups.


In this post, you created a popup service that constructs popups. You learned how to attach popups to markers with L.bindPopup.

Continue to Part 4 of this series on using Angular and Leaflet.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Hi Chris, thank you for this fantastic guide. There are two little mistakes in your code.

makeCapitalPopup(data: any): string { return `` + <div>Capital: ${ data.name }</div> + <div>State: ${ data.state }</div><^><^> + <div>Population: ${ data.population }</div> }


keep up the great work!

Hi. Thanks for this tutorial. How do we fetch the map markers geojson from the API? Can you please add a code snippet and guide us on that?