资讯专栏INFORMATION COLUMN

Geoserver+Openlayers拉框查询

番茄西红柿 / 1720人阅读

摘要:注意的版本号,高版本存在中新语法不兼容的情况查询类型点击多边形拉框图层名称,可以是单个或多个重点,不要改变回调函数声明加载函数传参解决跨域的关键回调回调函数使用载入要素用于删除之前的框,表示号,随便取一个删除之前的部分

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>OpenLayers</title>
    <link rel="stylesheet" href="geoserver/ol.css" type="text/css">
    <script src="geoserver/ol.js"></script>
    <script src="bower_components/jquery/dist/jquery.min.js"></script>
    <link rel="stylesheet" href="plugins/layui-v2.4.3/layui/css/layui.css">
    <script src="plugins/layui-v2.4.3/layui/layui.js" charset="utf-8"></script>
    <link rel="stylesheet" href="https://openlayers.org/en/v3.20.1/css/ol.css" type="text/css">
    <#--注意openlayer的版本号,高版本存在es6中新语法不兼容的情况-->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v3.20.1/build/ol.js"></script>
</head>
<style>
    #map {
        height: 600px;
        /*width: 1024px;*/
        /* float: left;*/
    }
</style>
<body>
<div id="map">
<form class="form-inline">
    <label>查询类型</label>
    <select id="type">
        <option value="None">None</option>
        <option value="Point">点击</option>
        <option value="Polygon">多边形</option>
        <option value="Circle">拉框</option>
    </select>
</form>
</div>
<script>
    var map = new ol.Map({
        target: 'map',
        view: new ol.View({
            projection: 'EPSG:4326',
            center: [104.07, 30.72],
            zoom: 7
        })
    });
    var wfsParams = {
        service: 'WFS',
        version: '1.1.1',
        request: 'GetFeature',
        typeName: 'map_dz:tl_lx_g',  //图层名称,可以是单个或多个
        outputFormat: 'text/javascript',  //重点,不要改变
        format_options: 'callback:loadFeatures'  //回调函数声明
    };
 
    var vectorSource = new ol.source.Vector({
        format: new ol.format.GeoJSON(),
        loader: function (extent, resolution, projection) {  //加载函数
            var url = 'http://192.168.1.113:8080/geoserver/map_dz/wms';
            $.ajax({
                url: url,
                data: $.param(wfsParams),   //传参
                type: 'GET',
                dataType: 'jsonp',   //解决跨域的关键
                jsonpCallback: 'loadFeatures'  //回调
 
            });
        },
        strategy: ol.loadingstrategy.tile(new ol.tilegrid.createXYZ({
            maxZoom: 20
        })),
        projection: 'EPSG:4326'
    });
    //回调函数使用
    window.loadFeatures = function (response) {
        vectorSource.addFeatures((new ol.format.GeoJSON()).readFeatures(response));  //载入要素
    };
    var vectorLayer = new ol.layer.Vector({
        source: vectorSource,
        style: new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: 'rgba(0, 0, 255, 1.0)',
                width: 2
            })
        })
    });
    map.addLayer(vectorLayer);
    var draw;
    var typeSelect = document.getElementById('type');
    var value;
    var num=10;//用于删除之前的框,表示号,随便取一个
    function addInteraction() {
        if (value !== 'None') {
            if (value === 'Polygon') {
                draw = new ol.interaction.Draw({
                    source: vectorLayer.getSource(),
                    style: new ol.style.Style({
                        stroke: new ol.style.Stroke({
                            color: '#ffcc33',
                            width: 2
                        }),
                        image: new ol.style.Circle({
                            radius: 7,
                            fill: new ol.style.Fill({
                                color: '#ffcc33'
                            })
                        })
                    }),
                    type: value
                });
            } else if (value === 'Circle') {
                draw = new ol.interaction.Draw({
                    source: vectorLayer.getSource(),
                    style: new ol.style.Style({
                        stroke: new ol.style.Stroke({
                            color: '#ffcc33',
                            width: 2
                        }),
                        image: new ol.style.Circle({
                            radius: 7,
                            fill: new ol.style.Fill({
                                color: '#ffcc33'
                            })
                        })
                    }),
                    type: value,
                    geometryFunction: ol.interaction.Draw.createBox()
                });
            } else if (value === 'Point') {
                draw = new ol.interaction.Draw({
                    source: vectorLayer.getSource(),
                    style: new ol.style.Style({
                        stroke: new ol.style.Stroke({
                            color: '#ffcc33',
                            width: 2
                        }),
                        image: new ol.style.Circle({
                            radius: 7,
                            fill: new ol.style.Fill({
                                color: '#ffcc33'
                            })
                        })
                    }),
                    type: value
                });
            }
 
            map.addInteraction(draw);
            //删除之前draw的部分
            draw.on('drawstart',function(evt) {
                var featureAdd=vectorLayer.getSource().getFeatureById(num);
                if(featureAdd!=null){
                    vectorLayer.getSource().removeFeature(featureAdd);
                }
            });
            //绘图结束,处理选中部分
            draw.on('drawend',function(e){
                e.feature.setId(num);
                var geom=e.feature.getGeometry();
                var coor = geom.v;
                mapSelect(coor);
            });
        }
    }
    //选择事件
    typeSelect.onchange = function() {
        value = typeSelect.value;
        map.removeInteraction(draw);
        addInteraction();
    };
 
    //draw图像与原始数据相交
    function mapSelect(coor) {
        if(value=='Point') {
            coor = [coor[0]-0.0001,coor[1]-0.0001,coor[0]+0.0001,coor[1]+0.0001];
        }
        var FILTER = '<Filter xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><BBOX><PropertyName>geom</PropertyName><gml:Envelope srsName="EPSG:4326"><gml:lowerCorner>' + coor[0] + '  ' + coor[1] + '</gml:lowerCorner><gml:upperCorner>' + coor[2] + '  ' + coor[3] + '</gml:upperCorner></gml:Envelope></BBOX></Filter>';
        getFeature({
            typename: 'map_dz:tl_lx_g',//查询的服务图层名称
            filter: FILTER,//查询条件
            callback: 'getIdentifyroadGrid'//查询的回调函数
        });
    }
    var selectNum=[];
    var geojsonFormat = new ol.format.GeoJSON({defaultDataProjection: "EPSG:4326"});
    function getIdentifyroadGrid(res) {
        var queryData = [];
        var features = geojsonFormat.readFeatures(res);
 
       for (var nu = 0; nu<selectNum.length;nu++) {
            var featureSelect=vectorLayer.getSource().getFeatureById(selectNum[nu]);
            if(featureSelect!=null) {
                featureSelect.setStyle(
                        new ol.style.Style({
                            stroke: new ol.style.Stroke({
                                color: 'rgba(0, 0, 255, 1.0)',
                                width: 2
                            })
                        }));
            }
        }
        selectNum=[];
        for (var i = 0; i < features.length; i++) {
            var feature = features[i];
            console.log(feature);
            selectNum.push(feature.f);
            var featureSelectCurrent=vectorLayer.getSource().getFeatureById(feature.f);
            featureSelectCurrent.setStyle(
                    new ol.style.Style({
                        stroke: new ol.style.Stroke({
                            color: '#ff4118',
                            width: 2
                        })
                    }));
            var lxmc = feature.H["lxmc"];
            var ldbm = feature.H["ldbm"];
            var lxbh = feature.H["lxbh"];
            var result = {
                "lxmc": lxmc,
                "ldbm": ldbm,
                "lxbh": lxbh,
                "setindex": i
            };
            queryData.push(result)
        }
        console.log(selectNum);
        var tableIns=null;
        var dataTable = "<table class="layui-table" lay-filter="demo" id="joinTab"></table>";
        layui.use(['table', 'layer'], function () {
            var table = layui.table;
            var layer = layui.layer;
            var index = layer.open({
                type: 1 //Page层类型
                , title: '要素属性'
                , shade: 0 //遮罩透明度
                , anim: 0 //0-6的动画形式,-1不开启
                , content: dataTable
                , offset: ['250px', '290px']
                , zIndex: 1
                , scrollbar: false
                , resize: false
                , skin: 'layui-layer-molv'
                , closeBtn: 2
                , btn: ["关闭"]
                , yes: function (index) {
                    layer.close(index);
                }
                , cancel: function () {
                }
            });
            tableIns = table.render({
                elem: "#joinTab",
                width: "auto",
                height: "auto",
                data: queryData,
                initSort: {field: 'lxbh', type: 'desc'},
                cols: [[
                    {field: 'lxmc', title: '路线名称', width: 150, align: 'center', sort: true},
                    {field: 'ldbm', title: '路线编号', width: 150, align: 'center', sort: true},
                    {field: 'lxbh', title: '路段编码', width: 150, align: 'center', sort: true}
                ]]
            });
            layer.style(index, {
                opacity: 0.8
            });
        });
 
        if (tableIns) {
            tableIns.reload({
                data: queryData
            });
        } else {
            // console.log("Do Nothing!");
        }
    }
 
    //请求wfs数据
    function getFeature(options) {
        $.ajax(/*'http://192.168.1.113:8080/geoserver/map_dz/wms', */{
            type: 'GET',
            url: 'http://192.168.1.113:8080/geoserver/map_dz/wms',
            data: {
                service: 'WFS',
                version: '1.1.1',
                request: 'GetFeature',
                typename: options.typename,
                srsname: options.srid,
                outputFormat: 'text/javascript',
                viewparams: options.viewparams,
                bbox: (options.extent === undefined) ? undefined : options.extent.join(',') + ',' + options.srid,
                filter: options.filter
            },
            dataType: 'jsonp',
            jsonp: 'format_options',
            jsonpCallback: 'callback:' + options.callback
        });
 
    }
 
</script>
</body>
</html>


文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/1130.html

相关文章

  • 比较好的网站推荐

    摘要:小程序开发网站推荐微信小程序支付宝小程序百度智能小程序字符跳动小程序小程序组件化开发框架开发小程序框架语法开发小程序框架,渐进式应用渐进式应用教程博客大牛推荐张鑫旭个人网站阮一峰网络日志阮一峰教程廖雪峰官方网站吕大豹的博客司徒正美博客编程教 小程序开发网站推荐 微信小程序:https://developers.weixin.qq.... 支付宝小程序:https://open.al...

    Corwien 评论0 收藏0
  • web前端技术体系大全

    以下为个人目前接触到的前端技术,欢迎大家补充。 一、前端技术框架 1、Vue.js 官网:https://cn.vuejs.org/ Vue CLI:https://cli.vuejs.org/ 菜鸟教程:http://www.runoob.com/w3cnote... Nuxt.js:https://zh.nuxtjs.org/ 桌面应用Electron:https:...

    RaoMeng 评论0 收藏0
  • 基于Python实现GeoServer矢量文件批量发布

      小编写这篇文章的主要目的,主要是来给大家做个详细的解答,解答的内容主要是Python的相关内容,包括利用Python实现GeoServer矢量文件的批量发布,具体是怎么样进行操作呢?下面就给大家详细解答下。  0.前言  由于矢量图层文件较多,手动发布费时费力,python支持的关于geoserver包(geoserver-restconfig)又由于年久失修,无法在较新的geoserver版...

    89542767 评论0 收藏0
  • Vue下的cesium使用GeoServer的wms服务跨域问题

    摘要:两种方案中间件我没搞成功把文件放进去之后改好了报,这个我没搞成功就不多说了,需要的可以自行搜索一波。需要监听的项目所在端口号就比如我的项目启动端口是,这里就写地址,本地和都可以项目中的路径这个意思就是在项目中访问的时候把请求转到是转发地址 两种方案 coess中间件(我没搞成功) 把class文件放进去之后改好了web.xml报504,这个我没搞成功就不多说了,需要的可以自...

    Seay 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<