OpenLayers6(2):Vue中使用ol-ext插件中的LayerSwitcher图层控制控件

1.版本

  • OpenLayers:6.4.3

  • ol-ext:3.2.22(条件:"ol": ">= 5.3.0")

2.配置ol-ext

// 1、安装ol-ext
npm i ol-ext

// 2、在main.js中引入样式
import 'ol-ext/dist/ol-ext.min.css';

3.使用LayerSwitcher控件

3.1 先看LayerSwitcher构造函数的参数

LayerSwitcher控件的构造函数参数
selectionenable layer selection when click on the title
displayInLayerSwitcherfunction that takes a layer and return a boolean if the layer is displayed in the switcher, default test the displayInLayerSwitcher layer attribute
show_progressshow a progress bar on tile layers, default false
mouseovershow the panel on mouseover, default false
reorderingallow layer reordering, default true
trashadd a trash button to delete the layer, default false
oninfocallback on click on info button, if none no info button is shown DEPRECATED: use on(info) instead
extentadd an extent button to zoom to the extent of the layer
onextentcallback when click on extent, default fits view to extent
drawDelaydelay in ms to redraw the layer (usefull to prevent flickering when manipulating the layers)
collapsedcollapse the layerswitcher at beginning, default true
layerGroupa layer group to display in the switcher, default display all layers of the map
noScrollprevent handle scrolling, default false
onchangeCheckoptional callback on click on checkbox, you can call this method for doing operations after check/uncheck a layer
allwaysOnToptrue to force layer stay on top of the others while reordering, default false
noSwitcherDeleteto prevent layer deletion (w. trash option = true), default false

3.2 图层属性配置

添加图层时需进行相应的属性配置(以下内容摘抄自ol-ext官方案例):

  • The title or name layer's property is used to named the ol.layer in the switcher.
  • It shows use of the displayInLayerSwitcher layer's property to cause it to not display in the LayerSwitcher
  • The baseLayer layer's property manage exclusive visibility on layers (only one base layer is shown at a time).
  • The allwaysOnTop layer's property make a layer to stay on top of others.
  • Layers in an ol.layer.Group are grouped together and can be managed all together (visibility/opacity/ordering).
  • The openInLayerSwitcher property of an ol.layer.Group is used to code the visibility of the sublayers.
  • The show_progress option display a progress bar on tiled layers (only once per map).
  • You can use the setHeader() function to add a header to the layer list.
  • You can use the drawlist event handle the current line display (add button, change list style, etc.)
  • It trigger a toogle event when shown/hidden
// ol-ext LayerSwitcher Options
export function olextLayerOptions(title, noSwitcherDelete, allwaysOnTop, displayInLayerSwitcher, baseLayer, openInLayerSwitcher) {
    return {
        title: title || "图层",
        noSwitcherDelete: noSwitcherDelete || true, // true表示不能通过控件删除
        allwaysOnTop: allwaysOnTop || false,
        displayInLayerSwitcher: displayInLayerSwitcher || true,
        baseLayer: baseLayer || false,
        openInLayerSwitcher: openInLayerSwitcher || true,
    }
}

const layer = new VectorLayer({
    ...olextLayerOptions(...........), 
    source: source,
    style: style,
});
this.Map.addLayer(layer);

3.3 使用LayerSwitcher控件

import OlExtLayerSwitcher from 'ol-ext/control/LayerSwitcher'

const ctrl = new OlExtLayerSwitcher({
                collapsed: true,
                trash: true,
            });
this.Map.addControl(ctrl);

4.效果

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在 Vue 使用 OpenLayers 实现图控制控件,可以按照以下步骤进行操作: 1. 安装 OpenLayersVue: ``` npm install ol vue ``` 2. 在 Vue 引入 OpenLayers: ```javascript import ol from 'ol' import 'ol/ol.css' ``` 3. 创建地图容器: ```html <template> <div ref="map" class="map"></div> </template> ``` 4. 在 Vue 的 mounted 钩子函数创建地图: ```javascript mounted() { // 创建地图容器 const map = new ol.Map({ target: this.$refs.map, layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([116.3975, 39.9085]), zoom: 12 }) }); this.map = map; } ``` 5. 创建图控制控件: ```html <template> <div ref="map" class="map"> <div class="ol-control ol-custom-control"> <div class="ol-custom-control-header">图控制</div> <div class="ol-custom-control-body"> <div v-for="(layer, index) in layers" :key="index"> <input type="checkbox" :id="layer.name" v-model="layer.visible"> <label :for="layer.name">{{ layer.name }}</label> </div> </div> </div> </div> </template> ``` 6. 在 Vue 的 data 定义图数据和控件的状态: ```javascript data() { return { map: null, layers: [ { name: 'OSM', visible: true, layer: new ol.layer.Tile({ source: new ol.source.OSM() }) }, { name: 'Bing Maps', visible: false, layer: new ol.layer.Tile({ source: new ol.source.BingMaps({ key: 'your-bingmaps-api-key', imagerySet: 'Road' }) }) } ] } } ``` 7. 在 Vue 的 watch 监听图状态的变化并更新地图: ```javascript watch: { layers: { deep: true, handler(layers) { const map = this.map; map.getLayers().clear(); layers.forEach(layer => { if (layer.visible) { map.addLayer(layer.layer); } }); } } } ``` 通过以上步骤就可以在 Vue 实现图控制控件了。需要注意的是,在实际应用,可以根据需要自定义控件的样式和布局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碰碰qaq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值