GETTING STARTED WITH NGX-LEAFLET

Updated 2020 — Supports Angular v9

In a previous post, we introduced @asymmetrik/ngx-leaflet — a compact and extensible library for integrating Leaflet maps into Angular.io applications. Now, we’ll show you how you can integrate it into your @angular/cli projects.

GETTING AND RUNNING THIS TUTORIAL

If you just want to see a working example, you can find the code for this tutorial on Github. All you need to do is clone the repo, install the dependencies, and run the application using the following commands:

For a complete walkthrough, read on! The following sections walk you through installing the ngx-leaflet plugin, creating a simple map, and adding customer layers and markers.

USING NGX-LEAFLET WITH ANGULAR CLI

The following walkthrough demonstrates how to create a new project, import and configure the @asymmetrik/ngx-leaflet plugin, and use the plugin to add maps. As previously mentioned, you can find the code for this tutorial on Github.

Step 1: Creating the @angular/cli Project

The first step is to install @angular/cli and create a new project.

Once you’ve created the project, run it using ng serve.

To see the running site, hit the url: http://localhost:4200. You should see the welcome page:

As you make changes to the code, @angular/cli rebuilds and redeploys the code automagically. If it gets out of sync, just kill the process and rerun ng serve. You can read more about @angular/cli on their Github page.

Step 2: Install and configure the dependencies

@asymmetrik/ngx-leaflet depends on Leaflet. Also, you’ll need to import the TypeScript Declaration File for Leaflet since @angular/cli leverages TypeScript.

Note: @asymmetrik/ngx-leaflet@7 supports Angular.io v9. To use earlier versions of Angular.io, check the Changelog section of the README.

Once all the dependencies are installed, the next step is to add the ngx-leaflet module to the Angular project. To do this, add the LeafletModule to AppModule in src/app/app.module.ts as shown below:

For the map to render correctly, you have to add the Leaflet stylesheet to the Angular CLI global stylesheet configuration. To do this, add the following to angular.json under both the "build" and "test" sections:

And, if you want markers to show up correctly on your maps, you need to configure Angular CLI to expose the Leaflet assets to your application. To do this, make the following additional changes to angular.json under both the "build" and "test" sections:

Step 3: Create a Map!

Now that all that setup is out of the way, you can get to the fun part. The next step is to add a map to the application. First, replace the contents of src/app/app.component.html with a single div containing the leaflet directive:

Next, define the component to go with the template. In order to create a map, you need to provide a center point and an initial zoom level. And, since the map is pretty boring without any base layers, go ahead and provide one of those too. In src/app/app.component.ts:

The last thing you need to do to get this all working is to add some styles to make sure the map expands to fit the whole screen. It’s actually pretty simple, but you need to modify two different stylesheets because some of the styles are local to the component and some are global to the application.

First, style the app component so it expands to fit its entire container by adding the following to src/app/app.component.scss:

Second, add a global style to make the html and body elements full screen by modifying src/styles.scss:

At this point, you should be able to run the application. Launch it with the ng serve command. Then, navigate to http://localhost:4200. You should see a map. Exciting, right?

Step 4: Adding stuff to the map

Once you’ve got a map showing up in your application, you can start to do useful things with it. For this tutorial, you are going to display a popular Mt. Ranier climbing route on the map using a polyline and two markers. And, you’re going to add a layers control so users can turn layers on and off and switch between map and satellite baselayers.

In src/app/app.component.ts, create the new layers and the layers control object:

In src/app/app.component.html, add the binding for the layers control:

At this point, you should see a path and a couple of markers on the map.

So, that’s super awesome, and other than the boilerplate setup, it wasn’t that difficult. But, what if you want to zoom and pan so that the user can actually see the route and the markers? It’s actually pretty easy using the fitBounds function in leaflet. The next section demonstrates how.

Step 5: Working with the map

The ngx-leaflet plugin exposes some commonly used functionality (like fitBounds) through Angular input bindings. Alternatively, ngx-leaflet exposes a reference to the map instance using the leafletMapReady output binding. This way, you can do whatever you need to do to the map directly. Since the latter is more flexible, you should learn to use it first. All you need to do is define a callback function in src/app/app.component.ts:

Next, add the leafletMapReady output binding to src/app/app.component.html:

When the map is created, the ngx-leaflet directive calls onMapReady passing a reference to the map as an argument. The function used map.fitBounds(...) to zoom and center the map around a bounding box derived from the path. The result is a map that shows a popular climbing route from the Paradise parking area to the summit of Mt. Ranier.

LEARN MORE

This tutorial walked you through a lot of the basic functionality of the @asymmetrik/ngx-leaflet plugin. At Asymmetrik, we’re working on more tutorials that show off some of the advanced functionality of ngx-leaflet, including how to extend the plugin, how to integrate with other bundling frameworks, and how to do some more advanced map interaction and configuration. In the interim, feel free to ask questions or report issues on the ngx-leaflet Github project page.

Asymmetrik develops modern enterprise software and data analytics solutions for clients in government and healthcare