Openlayers实现WFS服务及单击要素查询
来源:互联网 发布:ubuntu uefi 引导修复 编辑:程序博客网 时间:2024/05/16 03:07
Openlayers实现WFS服务及单击要素查询
作者:郜科科
原文链接:http://blog.csdn.net/u013323965/article/details/52449502
openlayers实现WFS时会出现跨域访问问题,解决方法参考上述链接。在原文基础上进行了WFS服务并融合单击要素查询属性功能,代码如下:
<!doctype html><html> <head> <meta charset="utf-8" http-equiv="Access-Control-Allow-Oringin" content="http://localhost"> <link rel="stylesheet" href="SRC/ol.css" type="text/css"> <style> .map { height: 400px; width: 100%;} </style> <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://openlayers.org/en/v4.2.0/build/ol.js" type="text/javascript"></script> <title>OpenLayers example</title> </head> <body> <div id="map" class="map"></div> <input type="button" name="home" value="home" onclick="select_home()"> <input type="button" name="road" value="road" onclick="select_road()"> <div id="info"></div> <script> function wfs(name){ var wfsParams = { service : 'WFS', version : '1.0.0', request : 'GetFeature', typeName : name, //图层名称 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://localhost:8080/geoserver/wfs'; $.ajax({ url: url, data : $.param(wfsParams), //传参 type : 'GET', dataType: 'jsonp', //解决跨域的关键 jsonpCallback:'loadFeatures' //回调 }); }, strategy: ol.loadingstrategy.tile(new ol.tilegrid.createXYZ({ maxZoom: 25 })), projection: 'EPSG:4326' }); //回调函数使用 window.loadFeatures = function(response) { vectorSource.addFeatures((new ol.format.GeoJSON()).readFeatures(response)); //载入要素 }; var vectorLayer = new ol.layer.Vector({ source: vectorSource }); return vectorLayer; } var wfs_layer = null; function clcall(){ if(wfs_layer){ map.removeLayer(wfs_layer); wfs_layer = null; } } function select_home(){ var name = 'map:home'; clcall(); wfs_layer = wfs(name); map.addLayer(wfs_layer); } function select_road(){ var name = 'map:road'; clcall(); wfs_layer = wfs(name); map.addLayer(wfs_layer); } var baseLayer = new ol.layer.Tile({ source: new ol.source.OSM() }); var map = new ol.Map({ layers: [baseLayer], target: document.getElementById('map'), view: new ol.View({ center: [114.3, 30.6], projection: 'EPSG:4326', maxZoom: 19, zoom: 12 }) });//实现高亮显示 var featureOverlay = new ol.layer.Vector({ source: new ol.source.Vector(), map: map, style: new ol.style.Style({ image: new ol.style.Circle({ radius: 5, fill: null, stroke: new ol.style.Stroke({color: 'red', width: 1}) }) }) }); var highlight; var displayFeatureInfo = function(pixel) { var feature = map.forEachFeatureAtPixel(pixel, function(feature) { return feature; }); var info = document.getElementById('info'); if (feature) { info.innerHTML = feature.getId() + '<br>'; var keys = feature.getKeys(); var properties = feature.getProperties(); for (var i = 0;i<keys.length;i++) { info.innerHTML += keys[i] + ' : '; info.innerHTML += properties[keys[i]] + '<br>'; } } else { info.innerHTML = ' '; } if (feature !== highlight) { if (highlight) { featureOverlay.getSource().removeFeature(highlight); } if (feature) { featureOverlay.getSource().addFeature(feature); } highlight = feature; } }; // map.on('pointermove', function(evt) { // if (evt.dragging) { // return; // } // var pixel = map.getEventPixel(evt.originalEvent); // displayFeatureInfo(pixel); // }); map.on('click', function(evt) { displayFeatureInfo(evt.pixel); }); </script> </body></html>最后贴上一张效果图:
阅读全文
1 0
- Openlayers实现WFS服务及单击要素查询
- 基于Geoserver的WFS服务与Openlayers实现地理查询
- GIS的学习(二十)基于Geoserver的WFS服务与Openlayers实现地理查询
- GIS的学习(二十)基于Geoserver的WFS服务与Openlayers实现地理查询
- openlayers实现wfs属性查询和空间查询
- OpenLayers中WFS服务与HetMap
- wfs服务查询详解
- OpenLayers WFS指定地理范围查询
- 基于OpenLayers 的WFS模糊查询优化
- OpenLayers中基于Filter的WFS查询
- 利用Geoserver的wfs BBOX范围查询及Openlayers解析GML,添加Marker
- 利用Geoserver的wfs BBOX范围查询及Openlayers解析GML,添加Marker
- OpenLayers跨域请求WFS服务在Tomcat环境下的实现
- OpenLayers跨域请求WFS服务在Tomcat环境下的实现cgi
- Openlayers使用WFS服务的跨域问题的解决办法
- openlayers 3加载GeoServer发布的wfs类型服务
- openlayers解析吉奥天地图wfs服务
- OpenLayers+Geoserver点要素,获取查询结果
- JavaScript数据结构(3):单向链表与双向链表
- iOS崩溃 捕获异常处理(二)
- HTTP请求的参数详解
- Ubuntu上搭建robotframework
- 城市列表
- Openlayers实现WFS服务及单击要素查询
- 单链表和双链表的实现
- Mac 修改hostname&computer name
- Typescript+dva搭建开发环境
- Java中的内存处理机制和final、static、final static总结
- 线段树--poj2828 Buy tickets
- Jenkins中节点运行job文件的传递
- 三层交换机 VS 二层交换机
- Maven多模块项目搭建