前言

openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子,这个也是学习 openlayers4 的好素材。

openlayers4 入门开发系列的地图服务基于 Geoserver 发布的,关于 Geoserver 方面操作的博客,可以参考以下几篇文章:

内容概览

1.基于 openlayers4 实现地图空间查询
2.源代码 demo 下载

本篇的重点内容是利用 openlayers4 实现地图空间查询功能,效果图如下:

实现思路

  • 框选工具(多边形以及矩形)
//多边形
$("#polygonButton").bind("click", function () {
DCI.SpatialQuery.clearMap();
DCI.SpatialQuery.InitState();
DCI.SpatialQuery.addInteraction("Polygon");
})
//矩形
$("#rectangleButton").bind("click", function () {
DCI.SpatialQuery.clearMap();
DCI.SpatialQuery.InitState();
DCI.SpatialQuery.addInteraction("Box");
}) addInteraction:function(value){
var geometryFunction;
switch (value) {
case "Box":
value = 'Circle';
geometryFunction = ol.interaction.Draw.createBox();
break;
case "Polygon":
value = 'Polygon';
break;
}
DCI.SpatialQuery.draw = new ol.interaction.Draw({
source: DCI.SpatialQuery.source,
type: value,
geometryFunction: geometryFunction
});
DCI.SpatialQuery.map.addInteraction(DCI.SpatialQuery.draw);
DCI.SpatialQuery.draw.on('drawend',function(evt){
DCI.SpatialQuery.clearMap();
DCI.SpatialQuery.drawEndPlot(evt.feature);
}); }
  • 框选绘制完成,进行 wfs 进行空间查询
/**
* 地图点击完成后函数
* **/
drawEndPlot:function(feature){
var featureRequest = new ol.format.WFS().writeGetFeature({
srsName: bxmap.config.MapConfig.wfs.srsName,
featureNS: bxmap.config.MapConfig.wfs.featureNS,
featurePrefix: bxmap.config.MapConfig.wfs.featurePrefix,
featureTypes: bxmap.config.MapConfig.wfs.featureTypes,
outputFormat: bxmap.config.MapConfig.wfs.outputFormat,
filter:ol.format.filter.intersects(bxmap.config.MapConfig.wfs.geometryName, feature.getGeometry(), bxmap.config.MapConfig.wfs.srsName)
});
fetch(bxmap.config.MapConfig.geoserver_url+bxmap.config.MapConfig.wfs.url, {
method: 'POST',
body: new XMLSerializer().serializeToString(featureRequest)
}).then(function(response) {
return response.json();
}).then(function(json) {
var features = new ol.format.GeoJSON().readFeatures(json);
if(features && features.length>0){
if(DCI.SpatialQuery.spatialLayer)
DCI.SpatialQuery.spatialLayer.getSource().clear();
if(DCI.SpatialQuery.pointLayer)
DCI.SpatialQuery.pointLayer.getSource().clear();
DCI.SpatialQuery.spatialSource.addFeatures(features);
DCI.SpatialQuery.map.getView().fit(DCI.SpatialQuery.spatialSource.getExtent());
$("#spatial-total").html("框选查询共"+features.length+"条结果");
var innerStr = [];
for(var i=0;i<features.length;i++){
var feature = features[i];
//面取中心点
var pointGeometry=DCI.SpatialQuery.creatPointGeometry(feature.getGeometry().getExtent());//面取中心点
var attribute = {
"OBJECTID":features[i].get('OBJECTID'),
"名称":features[i].get('名称'),
"编号":features[i].get('编号'),
"类别":features[i].get('类别'),
"面积":features[i].get('面积'),
};
var feature=new ol.Feature({
geometry: pointGeometry,
attribute:attribute,
id:features[i].get('OBJECTID'),
type:"point"
});
DCI.SpatialQuery.pointLayer.getSource().addFeature(feature); innerStr.push('<div class="left_list_li_box" id="' + features[i].get('OBJECTID') + '" onclick="DCI.SpatialQuery.locationToMap(\'' + features[i].get('OBJECTID') + '\')" >');
innerStr.push('<div class="left_list_li_box_top">');
innerStr.push('<div class="left2_box2">');
innerStr.push('<img class="list_poi_marker" style="" src="' + getRootPath() + 'Content/images/index/poiLocation.png"></img>');
innerStr.push('<div class="left_list_li1">');
innerStr.push('<p>');
innerStr.push('<a style="text-decoration:none">' + features[i].get('名称') + '</a><br/>');
innerStr.push('</p>');
innerStr.push('<p>');
innerStr.push('<a style="text-decoration:none;color:#555;">' + features[i].get('编号') + '</a><br/>');
innerStr.push('</p>');
innerStr.push('<p>');
innerStr.push('<a style="text-decoration:none;color:#555;">' + features[i].get('类别') + '</a><br/>');
innerStr.push('</p>');
innerStr.push('<p>');
innerStr.push('<a style="text-decoration:none;color:#555;">' + features[i].get('面积') + '</a><br/>');
innerStr.push('</p>');
innerStr.push('</div>');
innerStr.push('</div>')
innerStr.push('</div>');
innerStr.push('</div>');
}
$("#showLists").html(innerStr.join(''));
}
else{
$("#showLists").html("框选查询不到相关结果");
}
});
}

更多的详情见GIS之家小专栏

文章尾部提供源代码下载,对本专栏感兴趣的话,可以关注一波

openlayers4 入门开发系列之地图空间查询篇(附源码下载)的更多相关文章

  1. openlayers4 入门开发系列结合 echarts4 实现散点图(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  2. leaflet-webpack 入门开发系列三地图分屏对比(附源码下载)

    前言 leaflet-webpack 入门开发系列环境知识点了解: node 安装包下载webpack 打包管理工具需要依赖 node 环境,所以 node 安装包必须安装,上面链接是官网下载地址 w ...

  3. arcgis api 3.x for js 入门开发系列十三地图最短路径分析(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  4. openlayers4 入门开发系列之地图属性查询篇(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  5. cesium 入门开发系列矢量瓦片加载展示(附源码下载)

    前言 cesium 入门开发系列环境知识点了解:cesium api文档介绍,详细介绍 cesium 每个类的函数以及属性等等cesium 在线例子 内容概览 cesium 实现矢量瓦片加载效果 源代 ...

  6. arcgis api 3.x for js 入门开发系列十叠加 SHP 图层(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  7. openlayers4 入门开发系列之地图模态层篇(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  8. openlayers4 入门开发系列之地图工具栏篇(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  9. openlayers4 入门开发系列之地图导航控件篇(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

随机推荐

  1. js基础--获取浏览器当前页面的滚动条高度的兼容写法

    欢迎访问我的个人博客:http://www.xiaolongwu.cn 前言 在开发中,兼容性问题是最常见的,今天就来介绍一下关于获取滚动条高度的兼容性写法,宽度同理,我在这里就不一一解释了 各浏览器 ...

  2. css基础--深入理解opacity和rgba的区别

    欢迎访问我的个人博客:http://www.xiaolongwu.cn 前言 首先这两个都与透明度有关,那么他们之间有什么具体的区别呢?在实际工作中我们需要注意什么呢?请您接着往下看 语法 1. rg ...

  3. SSM-SpringMVC-20:SpringMVC中处理器方法之返回值void篇

      ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 处理器的方法我们之前做过,返回值为String的,返回值为ModelAndView的,我们这个讲的这个返回 ...

  4. SSM-Spring-22:Spring+Mybatis+JavaWeb的整合

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 众所周知,框架Spring来整合别的框架,但是Mybatis出现的晚,Spring就没有给他提供支持,那怎么办 ...

  5. 概率与统计推断第一讲homework

    1. 假设在考试的多项选择中,考生知道正确答案的概率为$p$,猜测答案的概率为$1-p$,并且假设考生知道正确答案答对题的概率为1,猜中正确答案的概率为$\frac{1}{m}$,其中$m$为多选项的 ...

  6. python日期格式化操作

    1.将字符串的时间转换为时间戳 方法: a = "2013-10-10 23:40:00" #将其转换为时间数组 import time timeArray = time.strp ...

  7. Go-技篇第一 技巧杂烩

    Go-技篇第一 技巧杂烩 一句话技巧 把你面向对象的大脑扔到家里吧,去拥抱接口.@mikegehard 学习如何使用Go的方式做事,不要把别的的编程风格强行用在Go里面.@DrNic 多用接口总比少用 ...

  8. bzoj5253 [2018多省省队联测]制胡窜

    后缀自动机挺好毒瘤的题. 我们考虑哪些切点是不合法的.肯定是所有的匹配串都被切了. 我们考虑第一个切口的位置. 当第一个切口在第一个出现位置前时,第二个切口必须切掉所有的串. 当第一个切口在$l_{i ...

  9. bzoj1547 周末晚会

    我们要求方案数,还是旋转同构的,想burnside,如果我们能计算出转i位不变的满足条件的数量,那么这道题我们就解决了. 考虑转i位时,设tmp=gcd(i,n),那么就共有tmp个循环节. 当tmp ...

  10. 【爆料】-《悉尼科技大学毕业证书》UTS一模一样原件

    ☞悉尼科技大学毕业证书[微/Q:2544033233◆WeChat:CC6669834]UC毕业证书/联系人Alice[查看点击百度快照查看][留信网学历认证&博士&硕士&海归 ...