angular - ngx-leaflet:如何添加自定义控件?

标签 angular typescript leaflet ngx-leaflet

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

绝对没有明确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);

enter image description here

创造传奇也是如此。

任何人都可以指出我正确的方向来尝试使用 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 的事情,但我不想添加圆​​形或形状,而是上面截图中的自定义控件以及图例。

最佳答案

要使此示例工作,您唯一需要做的就是监听 onMapReady 事件并将本教程的所有代码放在其中。 map 引用是您想要的,它是此函数的参数。

具体来说:

<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 的完整示例

关于angular - ngx-leaflet:如何添加自定义控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54734329/

相关文章:

angular - 错误时更改 Material 输入的颜色

javascript - 将 lodash 导入 angular2 + typescript 应用程序

angular - Angular 2 中服务的单个实例

javascript - 如何在Typescript中进行动态签名和返回类型?

javascript - 表达 : typescript: Argument of type 'typeof <express.Router>' is not assignable to parameter of type 'RequestHandlerParams'

r - 在传单中绘制 sfc_POLYGON

angular - 如何将 Chart.js 与 Typescript 一起使用而不会出现可分配的错误?

angular - 如果至少有一个字段具有值,则向响应式(Reactive) FormGroup 的所有控件添加必需的验证

leaflet - Geojson/草坪 : merge multiple polygons to one polygon keeping hole

传单+ gMapCatcher-更改zxy模板