如何使用Angular 2在传单地图上添加搜索控件?

时间:2018-10-12 13:55:32

标签: javascript angular leaflet esri-leaflet

我正在将leaflet.jsngx-leafletesri-leaflet-geocoder软件包一起使用。

我可以使用普通JavaScript在传单地图上使用搜索框。我需要的只是以下几行:

var searchControl = L.esri.Geocoding.geosearch().addTo(mymap);

但是我无法在Angular中完成此任务。我尝试了以下方法:

layers = [];
searchControl = Geocoding.geosearch();
this.layers.push(this.searchControl); // in the constructor

HTML:

<div style="height: 300px;"
     leaflet
     [leafletOptions]="options"
     [leafletLayersControl]="layersControl"
     [leafletLayers]="layers"
     [leafletFitBounds]="this.polygon.getBounds()"
     (leafletClick)="mapClicked($event)">
</div>

我收到错误提示:

  

错误错误:“提供的对象不是图层。”

我使用了searchControl控制台,纯JavaScript和Angular的结果都是相同的。

1 个答案:

答案 0 :(得分:0)

一种变通办法不能确定它是否是最佳解决方案。

导入插件的CSS:

import 'style-loader!esri-leaflet-geocoder/dist/esri-leaflet-geocoder.css';

在地图准备好后传递地图对象:

<div style="height: 300px;"
     leaflet
     [leafletOptions]="options"
     [leafletLayersControl]="layersControl"
     [leafletLayers]="layers"
     [leafletFitBounds]="polygon.getBounds()"
     (leafletClick)="mapClicked($event)"
     (leafletMapReady)="onMapReady($event)">>
</div>

并将控制器添加到地图中,就像普通的JavaScript:

onMapReady(map: L.Map) {
  setTimeout(() => {
    map.invalidateSize(true);
    this.searchControl.addTo(map);
  }, 0);
}