OpenLayers5實現(xiàn)點擊地圖查詢要素信息

WebGIS開發(fā)中,點擊查詢是最常用的一種查詢方式。本文從開源框架的角度,從前臺到服務(wù)端到數(shù)據(jù)庫等多個角度,多種方式實現(xiàn)點擊查詢。

1、map的click事件

該方法,通過鼠標(biāo)在地圖上點擊的坐標(biāo),與當(dāng)前矢量圖層做相交分析查詢,得到查詢的要素及其所屬的Layer對象

<template>
    <div>
        <div id="mapDiv"></div>
    </div>
</template>

<script>
    import 'ol/ol.css';
    import Map from 'ol/Map';
    import View from 'ol/View';
    import TileLayer from 'ol/layer/Tile';
    import OSM from 'ol/source/OSM';
    import {toLonLat,transform,transformExtent} from 'ol/proj';
    import VectorLayer from 'ol/layer/Vector';
    import VectorSource from 'ol/source/Vector';
    import Feature from 'ol/Feature';
    import Point from 'ol/geom/Point';
    import Style from 'ol/style/Style';
    import Fill from 'ol/style/Fill';
    import Stroke from 'ol/style/Stroke';
    import Image from 'ol/style/Image';
    import Icon from 'ol/style/Icon';

    import axios from 'axios'

    export default {
        name: "TestOL",
        data(){
            return{
                ol_map:''
            }
        },
        mounted:function(){
            var _this = this;

            //構(gòu)建map
            var osmTileLayer = new TileLayer({
                source: new OSM()
            });
            this.ol_map = new Map({
                target: 'mapDiv',
                layers: [osmTileLayer],
                view: new View({
                    center: transform([130, 35],'EPSG:4326', 'EPSG:3857'),
                    zoom: 3
                })
            });


            //添加矢量圖層
            var vectorSource = new VectorSource({
                features: []
            });
            var vectorLayer = new VectorLayer({
                source: vectorSource
            });
            _this.ol_map.getLayers().push(vectorLayer);

            var iconStyle = new Style({
                image:new Icon({
                    size:[30,30],
                    anchorXUnits: 'pixels',
                    anchorYUnits: 'pixels',
                    src:'/static/image/car_left.png'
                })
            });
            axios.get("http://localhost:8081/static/json/car.json").then(function(response){
                var res = response.data;
                for(var i=0; i<res.length; i++){
                    var feature = new Feature({
                        geometry: new Point(transform([res[i].lon, res[i].lat],'EPSG:4326', 'EPSG:3857')),
                        name:res[i].name,
                        lon:res[i].lon,
                        lat:res[i].lat
                    });
                    feature.setStyle(iconStyle);
                    vectorSource.addFeature(feature);
                }
            });

            this.ol_map.on('click',this.clickMapHandle);
        },
        methods:{
            clickMapHandle:function(event){
                var pixel = this.ol_map.getEventPixel(event.originalEvent);
                this.ol_map.forEachFeatureAtPixel(pixel, function (feature, layer) {
                    if (feature != undefined) {
                        console.log(feature);
                    }
                })
            }
        }
    }
</script>

<style scoped>
    #mapDiv{
        width:1000px;
        height:700px;
        border:1px solid #ff0000;
    }
</style>
car.json

2、WMS圖層的GetFeatureInfo

對于矢量圖層,我們可以通過第一種方法實現(xiàn)點擊查詢。但是,很多時候我們底圖是wms服務(wù),這時候我們可以通過wms協(xié)議的GetFeatureInfo實現(xiàn)點選查詢。

<template>
    <div>
        <div id="mapDiv"></div>
    </div>
</template>

<script>
    //點擊地圖,通過GetFeatureInfo實現(xiàn)要素信息查詢
    import Map from 'ol/Map.js';
    import View from 'ol/View.js';
    import TileLayer from 'ol/layer/Tile.js';
    import TileWMS from 'ol/source/TileWMS.js';

    import $ from 'jquery'

    export default {
        name: "TestOL",
        data(){
            return{
                tileWMSSource:'',
                tileLayer:'',
                map:''
            }
        },
        mounted:function(){
            this.tileWMSSource = new TileWMS({
                url: 'http://localhost:8080/geoserver/wzf/wms',
                params: {
                    'LAYERS': 'wzf:wafangdianshi_0',
                    'TILED': true,
                    'STYLES':'wfds_style_GB2312'
                },
                serverType: 'geoserver',
                projection: "EPSG:4326"
            });
            this.tileLayer = new TileLayer({
                source: this.tileWMSSource,
            })

            this.map = new Map({
                layers: [this.tileLayer],
                target: 'mapDiv',
                view: new View({
                    center: [122, 40],
                    zoom: 9,
                    projection:"EPSG:4326"       //默認(rèn)的是 'EPSG:3857'橫軸墨卡托投影
                })
            });
            this.map.on('click',this.mapClick);

        },
        methods:{
            mapClick:function(evt){
                var viewResolution = this.map.getView().getResolution();
                var url = this.tileLayer.getSource().getGetFeatureInfoUrl(
                    evt.coordinate, viewResolution, 'EPSG:4326',
                    {
                        'INFO_FORMAT': 'application/json',
                    });
                $.ajax({
                    type: 'GET',
                    url:url,
                    success:function(res){
                        console.log(res);
                    }
                });
            }
        }
    }
</script>

<style scoped>
    #mapDiv{
        width:1000px;
        height:700px;
        border:1px solid #ff0000;
    }
</style>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容