【问题标题】:ngx-leaflet: How to add a custom control?ngx-leaflet:如何添加自定义控件?
【发布时间】:2019-02-17 14:41:17
【问题描述】:

我已经尝试了一段时间使用ngx-leaflet 来实现这个leaflet tutorial

在按照教程进行操作时,对于如何实现自定义控件或图例,绝对没有明确的 documentation

var info = L.control();

info.onAdd = function (map) {
    this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
    this.update();
    return this._div;
};

// method that we will use to update the control based on feature properties passed
info.update = function (props) {
    this._div.innerHTML = '<h4>US Population Density</h4>' +  (props ?
        '<b>' + props.name + '</b><br />' + props.density + ' people / mi<sup>2</sup>'
        : 'Hover over a state');
};

info.addTo(map);

创建图例也是如此。

能否指出我正确的方向,尝试使用 ngx-leaflet lib 在 Angular 7 中实现它?

import { control, featureGroup, geoJson, icon, latLng, LatLngExpression, Map, Marker, marker, popup, tileLayer } from 'leaflet';

  onMapReady(map: Map) {
    this.map = map;

    // create info control
    let info = control(
      {
        onAdd: map => {

        }
      }
    )
    info.addTo(map);

我知道您需要执行类似this 的操作,但我不想添加圆​​形或形状,而是添加上面屏幕截图中的自定义控件以及图例。

【问题讨论】:

  • 一般来说,你会按照 ngx-leaflet 自述文件来获取地图的参考。然后您可以像教程一样将自定义控件添加到地图中。
  • 问题是没有关于如何添加自定义控件的明确文档。我尝试了很多方法,但最终导致自定义控件出现运行时错误。
  • 你不能让 info = L.control();例如,因为这不能正常工作。也不确定要使用哪些导入。

标签: angular typescript leaflet ngx-leaflet


【解决方案1】:

要使这个示例工作,您唯一需要做的就是监听 onMapReady 事件并将教程的所有代码放在那里。地图引用是您想要的,它作为该函数的参数提供。

具体来说:

<div
  style="height: 100vh"
  leaflet
  [leafletOptions]="options"
  (leafletMapReady)="onMapReady($event)"
></div>

ts:

onMapReady(map) {
 // place all the tutorial code inside here
 ...

// control that shows state info on hover
let info = L.control();

// here you want the reference to be info, therefore this = info
// so do not use es6 to access the the class instance
info.onAdd = function (map) {
  this._div = L.DomUtil.create('div', 'info');
  this.update();
  return this._div;
};

// also here you want the reference to be info, therefore this = info
// so do not use es6 to access the class instance
info.update = function (props) {
  this._div.innerHTML = '<h4>US Population Density</h4>' + (props ?
    '<b>' + props.name + '</b><br />' + props.density + ' people / mi<sup>2</sup>'
    : 'Hover over a state');
};

info.addTo(map);
...
const legend = L.control({ position: 'bottomright' });

legend.onAdd = map => {

  let div = L.DomUtil.create('div', 'info legend'),
    grades = [0, 10, 20, 50, 100, 200, 500, 1000],
    labels = [],
    from, to;

  for (var i = 0; i < grades.length; i++) {
    from = grades[i];
    to = grades[i + 1];

    labels.push(
      '<i style="background:' + getColor(from + 1) + '"></i> ' +
      from + (to ? '&ndash;' + to : '+'));
  }

  div.innerHTML = labels.join('<br>');
  return div;
};

legend.addTo(map);
}

查看 demo 以获取 Angular 7 中使用 ngx-leaflet 的完整示例

【讨论】:

  • 这确实有效,谢谢。我认为会有一种更“有角度”的方式来处理这个问题。同样非常重要的是: import * as L from "leaflet" 部分。
猜你喜欢
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-22
  • 2014-11-03
  • 1970-01-01
  • 2019-01-11
  • 2019-10-29
相关资源
最近更新 更多