0
votes

I am using the heatmap.js library with @asymmetrik/ngx-leaflet in an Angular project in order to display a heatmap of the user's locations.

I use the bounds of the map to create a polygon and with it bring the aggregated count of the positions inside it. I am able to bring and display the data, but I would like for the radius to change accordingly with the zoom.

I've set scaleRadius to true but it is not working as I expected: If I choose a radius like 1 for example, when I zoom in the radius becomes too big. If I change to a small radius when zooming out it becomes tiny. So, I've done a few tests and come up with this values (I've set a min zoom and a max zoom):

zoom    radius
5       0.5
6       0.3
7       0.1
8       0.05
9       0.03
10      0.01
11      0.007
12      0.005
13      0.003
14      0.002
15      0.001

I thought maybe I can use the event handler of leaflet 'zoomend' to detect a change in the zoom and change the radius somehow. Am I complicating things too much? Is it possible to do something like this? Thank you.

My code is:

Template:

<div class="map"
     leaflet
     [leafletOptions]="options"
     (leafletMapReady)="onMapReady($event)">
</div>

Component:

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

declare var L;
declare var HeatmapOverlay;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  data = {
    data: []
  };

  heatmapLayer = new HeatmapOverlay({
    radius: 0.01,
    maxOpacity: 0.8,
    scaleRadius: true,  // <------------------ Set to true
    blur: .75,
    latField: 'lat',
    lngField: 'lng',
    valueField: 'count'
  });

  options = {
    layers: [
      tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
        {
          maxZoom: 15,
          minZoom: 5
        }),
      this.heatmapLayer
    ],
    zoom: 10,
    center: latLng([45.116177, 7.742615])
  };

    onMapReady(map: MapLeaflet): void {

    console.log('onMapReady');
    this.map = map;

    this.map.addLayer(this.editableLayers);

    this.map.on('draw:created', this.onMapDrawed, this);
    this.map.on('moveend', this.onChangedMap, this);
    this.map.on('zoomend', this.changeRadius(), this);  // <-------change radius
    this.map.on('draw:deleted', this.onDeletedFromMap, this);
    console.log('Map done');

    this.getPositions();

  }

  getPositions() {

    const coords: LatLng [] = [];
    this.data.data = [];

    const bounds = this.map.getBounds();
    const sw = bounds.getSouthWest();
    const se = bounds.getSouthEast();
    const nw = bounds.getNorthWest();
    const ne = bounds.getNorthEast();

    coords.push(nw);
    coords.push(ne);
    coords.push(se);
    coords.push(sw);
    coords.push(nw);

    const mypoly = new Polygon(coords);

    this.positionService.getPositionInPolygon(JSON.stringify(mypoly.toGeoJSON().geometry))
    .subscribe(
      result => {

      result.forEach(
        item => {

          this.data.data.push({
            lat: item.coordinates[1],
            lng: item.coordinates[0],
            count: item.count
          });

        });

        this.heatmapLayer.setData(this.data);
    },
      error => {
        console.log(error);
      },
      () => {
        // 'onCompleted' callback.
      }

    );
  }
}

I used this tutorial: https://github.com/Asymmetrik/ngx-leaflet-tutorial-plugins/tree/master/heatmap.js

2

2 Answers

0
votes

The solution to this problem was that I assign manually to each data point the radius. So, I converted the table with the zoom and the radius into a function that was called each time the user changed the zoom of the map (use event listener of leaflet map).

0
votes

Yes you can do this for every datapoint. But you also can do it for the whole heatmap. I didn't do any performance measures, though.

   onMapZoom($event) { 
     this.heatmapLayer.cfg.radius = this.getRadiusByZoomlevel($event.target.getZoom());
   }

   getRadiusByZoomlevel(radius) {    
     if (radius <= 5) {
       return 0.5;
     } else 
     if(radius > 5){
       return 0.3
     }    
   }