在Angular + Openlayers中使用点击事件来检测点击的图层

Use click event in Angular + Openlayers to detect the layer clicked

我需要知道我在 openlayers 地图中单击了哪个层,以便稍后获取它的一些数据。

我以前做过,但只用了一层。现在我有不止一个,我想知道我正在点击哪一层来从地理服务器获取它的信息。

capa1WMS:TileLayer;
map:Map;

...

getDatosCapaWMS(url:string): Observable<any>{
    return this.http.get(url);
}

clickMapa(event:any):void{
    if(this.capa1WMS.getSource()!=null){
      var url = this.capa1WMS.getSource().getGetFeatureInfoUrl(
        this.map.getEventCoordinate(event), this.view.getResolution(), this.view.getProjection(),
        {'INFO_FORMAT': 'application/json'});
      this.getDatosCapaWMS(url).subscribe(data => { 

        //stuff

    });
    }   
}  

还有我的HTML代码

<div id="map" class="map" (click)="clickMapa($event)"></div>

我看到 openlayers 有一个方法叫做 forEachFeatureAtPixel() 但我没有让它工作

好的,Mike 给出了解决方案

this.map.forEachLayerAtPixel(this.map.getEventPixel(event)) 获取要用于 .getSource().getGetFeatureInfoUrl()

的图层