首页 > 解决方案 > 在 Angular + Openlayers 中使用点击事件来检测点击的图层

问题描述

我需要知道我在 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()但我没有让它工作的方法

标签: angularopenlayersgeoserver

解决方案


好的,迈克给出了解决方案

this.map.forEachLayerAtPixel(this.map.getEventPixel(event))获取要使用的图层.getSource().getGetFeatureInfoUrl()


推荐阅读