【问题标题】:Change radius using heatmap.js plugin with ngx-leaflet map in Angular project在 Angular 项目中使用带有 ngx-leaflet 地图的 heatmap.js 插件更改半径
【发布时间】:2018-10-30 23:06:32
【问题描述】:

我在 Angular 项目中使用带有 @asymmetrik/ngx-leaflet 的 heatmap.js 库来显示用户位置的热图。

我使用地图的边界来创建一个多边形,并用它带来其中位置的聚合计数。我能够带来和显示数据,但我希望半径随着缩放而相应改变。

我已将 scaleRadius 设置为 true,但它没有按预期工作:例如,如果我选择像 1 这样的半径,当我放大半径时,半径会变得太大。如果我变小 缩小时半径变得很小。所以,我做了一些测试并得出了这个值(我设置了最小缩放和最大缩放):

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

我想也许我可以使用传单“zoomend”的事件处理程序来检测缩放的变化并以某种方式改变半径。我是不是把事情复杂化了?有可能做这样的事情吗?谢谢。

我的代码是:

模板:

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

组件:

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.
      }

    );
  }
}

我用过这个教程:https://github.com/Asymmetrik/ngx-leaflet-tutorial-plugins/tree/master/heatmap.js

【问题讨论】:

    标签: angular leaflet ngx-leaflet


    【解决方案1】:

    这个问题的解决方案是我手动为每个数据点分配半径。因此,我将带有缩放和半径的表格转换为每次用户更改地图缩放时调用的函数(使用传单地图的事件监听器)。

    【讨论】:

      【解决方案2】:

      是的,您可以对每个数据点执行此操作。但您也可以为整个热图执行此操作。不过,我没有进行任何绩效衡量。

         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
           }    
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-12
        • 1970-01-01
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多