DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. JavaScript
  4. Implementing Dynamic Heat Maps In Angular Applications

Implementing Dynamic Heat Maps In Angular Applications

Heat visualization implementation in less then 5 minutes.

Maksym Mostovyi user avatar by
Maksym Mostovyi
·
Nov. 22, 21 · Tutorial
Like (3)
Save
Tweet
Share
7.27K Views

Join the DZone community and get the full member experience.

Join For Free

In the article, I would like to describe a way to easily implement a simple heat map in an Angular application. For that, I will use two external libraries, which are going to be Leaflet.js and Heatmaps.js. Before the implementation of the heat map, I prefer to begin with a brief introduction to the libraries I mentioned above.

Leaflet.js

It is one of the most popular JavaScript libraries for interactive maps. It has over 30k stars on GitHub. There is an alternative to it such as OpenLayers. But in my example, I picked a leaflet.

Heatmap.js

JavaScript library for heat maps implementation. It is very easy to use and it takes only several lines of code to bring heat visualization. Most important in our case is that this library contain the plugin for a leaflet.

Implementation

Time to start the implementation of dynamic heat maps. First, we need to create a simple angular project by using the following command.

ng new heatmap

Now, we need to install the required libraries.

npm install leaflet 

npm install heatmap.js

To be able to represent map tiles properly we need to add links to leaflet styles in the angular.json file. Besides that, links to libraries' JavaScript code also should be added.

JSON
 
"styles": [
	"./node_modules/leaflet/dist/leaflet.css",
	"src/styles.scss"
],
"scripts": [
	"./node_modules/leaflet/dist/leaflet.js",
	"./node_modules/heatmap.js/build/heatmap.js",
	"./node_modules/heatmap.js/plugins/leaflet-heatmap/leaflet-heatmap.js"
]


The next step is to implement map representation in the app. For that, we need to add a div in the app.component.html file with id="map". Then we can style it by assigning width: 100vw and height: 100vh.

Inside the app.component.ts, we need to write the following code:

TypeScript
 
import {Component, OnInit} from '@angular/core';
import * as leaflet from 'leaflet';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  private map: any;

  ngOnInit(): void {
    this.initMap();
  }

  private initMap(): void {
    // Initialising map with center point by using the coordinates
    // Setting initial zoom to 3
    this.map = leaflet.map('map', {
      center: [ 39.8282, -98.5795 ],
      zoom: 3
    });

    // Initialising tiles to the map by using openstreetmap
    // Setting zoom levels
    const tiles = leaflet.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      maxZoom: 18,
      minZoom: 3,
      attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
    });

    // Adding tiles to the map
    tiles.addTo(this.map);
  }
}


To be able to show heat layers, we need to add some data. For that, I have created a separate file in the project which contains a randomly generated data set.

TypeScript
 
export const heatData: any = {
  data: [
    {lat: 48.37, lng: 31.16, count: 143},
    {lat: 49.37, lng: 36.16, count: 133},
    {lat: 43.32, lng: 37.13, count: 163},
    {lat: 39.33, lng: 38.42, count: 143},
    {lat: 42.31, lng: 34.65, count: 133},
    {lat: 43.39, lng: 32.10, count: 163},
  ]
}


Finally, we can implement heat layers by enhancing the logic in app.component.ts. That is how the whole component looks in the end: 

TypeScript
 
import {Component, OnInit} from '@angular/core';
import * as leaflet from 'leaflet';
import 'heatmap.js';
import {heatData} from "./data";

declare const HeatmapOverlay: any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  private map: any;

  ngOnInit(): void {
    this.initMap();
  }

  private initMap(): void {
    // Initialising map with center point by using the coordinates
    // Setting initial zoom to 3
    this.map = leaflet.map('map', {
      center: [ 39.8282, -98.5795 ],
      zoom: 3
    });

    // Initialising tiles to the map by using openstreetmap
    // Setting zoom levels
    const tiles = leaflet.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      maxZoom: 18,
      minZoom: 3,
      attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
    });

    // Adding tiles to the map
    tiles.addTo(this.map);

    // Setting up heat layer config
    const heatLayerConfig = {
      "radius": 5,
      "maxOpacity": .8,
      "scaleRadius": true,
      // property below is responsible for colorization of heat layer
      "useLocalExtrema": true,
      // here we need to assign property value which represent lat in our data
      latField: 'lat',
      // here we need to assign property value which represent lng in our data
      lngField: 'lng',
      // here we need to assign property value which represent valueField in our data
      valueField: 'count'
    };

    // Initialising heat layer and passing config
    const heatmapLayer = new HeatmapOverlay(heatLayerConfig);

    //Passing data to a layer
    heatmapLayer.setData(heatData);

    //Adding heat layer to a map
    heatmapLayer.addTo(this.map);
  }
}


And here is how the app looks: 

Result Heat Map in Angular Application

Summing-Up

As you see it took only several lines of code to have heat maps visualization. In practice, it can be used for different purposes, such as climate change representation, the volume of events on location, or even for movement tracking. For example, FIFA uses heat maps to represent football player movement.

Hope it is helpful. The source code of a demo application you can find in my GitHub.

HEAT (software) application AngularJS JavaScript library

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Complete Guide to AngularJS Testing
  • What Should You Know About Graph Database’s Scalability?
  • Why Every Fintech Company Needs DevOps
  • Java Development Trends 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: