javascript - Leaflet customcontrol - Angular 2+

标签 javascript angular typescript leaflet ngx-leaflet

我需要在我的 Angular2+ 组件中集成自定义表单控件:

export class HeatMapComponent {
...
  onMapReady(map: L.Map) {
    let div = L.DomUtil.create('div', 'info');
    let info = new L.Control();
    info.onAdd = () => {
      return div;
    };
    info.addTo(map);
  }
} 

像这样的 .hmtl 文件:

    <div class="map container-fluid"
     leaflet [leafletOptions]="options"
     [leafletLayersControl]="layersControl"
     (leafletMapReady)="onMapReady($event)">
</div>

.css:

...

    .info {
      padding: 2rem 1rem;
      margin-left: 70rem;
      margin-right: 15rem;
      font: 1rem/1.5rem Arial, Helvetica, sans-serif;
      background: white;
      background: rgba(255,255,255,0.8);
      box-shadow: 0 0 15px rgba(0,0,0,0.2);
      border-radius: 5px;
    }

在传单文档中,它是用 JS 编写的,如下所示:

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);

为什么我不能在我的 onMapReady 中集成 addTo 方法?我没有收到任何错误,但我的 map 上没有显示任何内容。

最佳答案

我解决了这个问题,解决方案是:

onMapReady(map: leaflet.Map) { 
let div = leaflet.DomUtil.create('div', 'infoControl'); 
let info = new leaflet.Control(); info.onAdd = () => { 
return div; 
   }; 
console.log(div); 
info.addTo(map); 
}

关于javascript - Leaflet customcontrol - Angular 2+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47608576/

相关文章:

使用 HttpClient 模块的 Angular 4 设置 header

angular - 如何在不使用 JQuery 函数的情况下以 angular 4 进行深度复制?

按下错误的键时javascript减去计数?

javascript - 添加月份到 javascript 的 setMonth 显示 0

javascript - 为什么我的 jQuery Ajax GET 请求没有收到服务器的响应?

javascript - Angular 5 URL 路由,单击按钮并保留 url 中的查询字符串参数

具有多个键到一个值的 Javascript Map 对象

Angular 4 : Detect idle user using angular-user-idle

javascript - NestJS 如何使用 async/await 配置中间件?

javascript - 在嵌入式 Calendly 小部件中安排事件时如何运行代码?