openlayers实现点击图层弹出相应信息

openlayers:4.6.5

vue-cli:3.0

openlayers官网:https://openlayers.org/

天地图官网:https://www.tianditu.gov.cn/

1.准备lmap.js并初始化地图

let lmap = {
    map: null,
    init() {
        this.map = new ol.Map({
               target: 'olMap',
               logo: false,
               view: new ol.View({
               center: ol.proj.transform([113.2637, 23.1314], 'EPSG:4326', 'EPSG:4490'),
                    zoom: 7,
                    minZoom: 4,
                    maxZoom: 18,
                    enableRotation: false,
                    projection: 'EPSG:4490'
                }),
                layers: [
                    new ol.layer.Tile({
                        zIndex: 0,
                        source: new ol.source.XYZ({
                            projection: 'EPSG:4326',
                            url: 'http://t{1-7}.tianditu.gov.cn/DataServer?T=vec_c&x={x}&y={y}&l={z}&tk=d6da39f0bfd91bdf9c4941541e18669e'
                        })
                    })
                ]
         })

    }

}

export default = lmap
导入lmap.js:

import lmap from './lmap.js'


mounted() {
     // 初始化地图:
     lmap.init()
    
     if (lmap.map) {
        // 初始化图层
        this.setLayer()
     }
}

2.创建图层

setLayer () {
    let params = {
          LAYERS: '项目规定的字符串',
          FORMAT: 'image/png',
          VERSION: '1.1.1'
        }

    this.layers = new ol.layer.Image({
          source: new ol.source.ImageWMS({
            url: '输入项目图层资源地址',
            params,
            serverType: '图层资源的服务器类型'
          }),
          visible: true,
          zIndex: 6
        })

    lmap.map.addLayer(this.layers)
}


 3.地图点击

mapClick () {
    if (lmap.map) {
        let self = this;
        lmap.map.on('click', function(e) {
            self.getMapClick(e)
        })
    }

}

getMapClick (e) {
    let viewResolution = lmap.map.getView().getResolution()
    let url = this.layers.getSource().getGetFeatureInfoUrl(
        e.coordinate, viewResolution, 'EPSG:4490', { // 我项目用的是4490
        'INFO_FORMAT': 'application/json',
        'QUERY_LAYERS': '项目图层类型',
        'exceptions': 'application/vnd.ogc.se_inimage'
    })
    
    this.axios.get(url).then(res => {
        if (res.features.length > 0) {
          // 这里就是异步获取到图层点击对应的内容
        }
    })
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 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实现图层控制控件了。需要注意的是,在实际应用中,可以根据需要自定义控件的样式和布局。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值