如何使用Vuelayers与GeoServer提供的WMS切片图层进行交互?

时间:2019-05-26 23:32:17

标签: javascript vue.js openlayers geoserver vuelayers

我正在使用Vuelayers库开发Web地图应用程序,该库是具有OpenLayers功能的 Web map Vue组件

我的模板中包含以下代码:

<vl-map @singleclick="hideOverlay" @postcompose="onMapPostCompose"
 :load-tiles-while-animating="true" ref="map" 
:load-tiles-while-interacting="true" data-projection="EPSG:4326" 
style="height: 900px" @mounted="onMapMounted">
 ....

  <component v-for="layer in layers" :ref="layer.id" overlay
    :is="layer.cmp"  
    :key="layer.id" v-bind="layer">
        <component :is="layer.source.cmp" v-if="layer.visible" v-bind="layer.source">
        </component>
    </component>
     ....
</vl-map>

并且在数据对象中,我具有以下属性:

     layers: [

            {
                id: 'sections',
                title: 'Sections',
                cmp: 'vl-layer-tile',
                visible: true,

                source: {
                    cmp: 'vl-source-wms',
                    url: 'http://localhost:8080/geoserver/sager/wms',
                    layers: 'sections',
                    tiled: true,
                    format: 'image/png',
                    serverType: 'geoserver',
                },
            },
     ....
    ]

所以我想在单击图层时获取图层属性吗?知道vl-tile-layer没有here

所述的@click事件

1 个答案:

答案 0 :(得分:3)

只需将点击处理程序放置在顶级地图组件上,如下所示:

<vl-map @click="mapClick" @singleclick="hideOverlay" @postcompose="onMapPostCompose"
 :load-tiles-while-animating="true" ref="map"
:load-tiles-while-interacting="true" data-projection="EPSG:4326"
style="height: 900px" @mounted="onMapMounted">

</vl-map>

然后在click事件中使用forEachLayerAtPixel函数,该函数对在该屏幕像素处显示的每一层进行操作,并为您提供ol.Layer.Layer对象,您可以在以下对象上调用getProperties()

 methods: {
  mapClick: function(evt){
    evt.map.forEachLayerAtPixel(
        evt.pixel,
        function(layer){ layer.getProperties()},
        function(layer){/*filter layers you want to get property data on*/})
  }
}

仅当服务器上设置了CORS并且可以在vue层在后台使用的OpenLayers层上设置crossOrigin设置时,以上内容才起作用。上面的方法更好,但是如果出现错误提示

"SecurityError: Failed to execute 'getImageData' on 
'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data

然后您可以使用更通用的功能,例如

evt.map.getLayers().item(0)

or

evt.map.getLayers.forEach(
    function(layer){
        layerProps = layer.getProperties()
        if(layerProps.id === "the one you want")
        {
            // You will have to implement your own intersection logic here
            // to see if the click point intersected with the layer.
        }
})