使geoJSONLayer能够加载两种数据类型的geojson数据
问题描述
在使用geoJSONLayer加载geojson数据时,官方文档只支持单一类型的geojson数据加载,当一个geojson数据中出现两种类型的数据时可以尝试一下方法进行解决
本场景为:点击图层获取geojson,通过geoJSONLayer加载底图上,然后在通过popup显示当前点击位置的数据,点击位置的要素要高亮
- 对一种数据类型进行渲染
_this.loadGeojson('clickQueryGeojsonPolyline', res.data.json_url)
- 循环geojson数据 判断是否有第二种类型的数据,如果存在第二种类型的数据则进行渲染第二种类型的数据
for (const item of features) {
const {type} = item.geometry
if (type === 'Point') {
hasPoint = true
await _this.loadGeojson('clickQueryGeojsonPoint', res.data.json_url, 'point')
break
}
}
- 渲染完成后是两个不同的图层,点击查询结果并不在一起,现在把两个图层查询的结果整合在一起
- 搜索两个图层下所有的要素(geojson是根据点击查询的结果渲染的,所以每个图层上所有的要素就是查询的结果)
const layerPolyline = _this.map.findLayerById('clickQueryGeojsonPolyline')
const layerPoint = _this.map.findLayerById('clickQueryGeojsonPoint')
let geojsonFeaturesPoint = {}
const geojsonFeaturesPolyline = await layerPolyline.queryFeatures()
/**
* 三种情况
* 如果图层不存在,直接把geojsonFeaturesPoint的features赋空
* 如果图层存在并且是当前加载 则查所有的点要素
* 如果图层存在但是上一次加载的结果,则删除图层并把geojsonFeaturesPoint的features赋空
*/
if (layerPoint) {
if (!hasPoint) {
_this.map.remove(layerPoint)
geojsonFeaturesPoint.features = []
} else {
geojsonFeaturesPoint = await layerPoint.queryFeatures()
}
} else {
geojsonFeaturesPoint.features = []
}
- 把查询的结果合并到一个数组中(根据需求,当前默认高亮点),然后打开弹窗
const geojsonFeatures = [...geojsonFeaturesPoint.features, ...geojsonFeaturesPolyline.features]
_this.view.popup.open({
location: event.mapPoint,
features: geojsonFeatures
})
完整代码
const res = await clickQuery(event.mapPoint.x, event.mapPoint.y)
let features = res.data.features
let hasPoint = false
if (features.length === 0) {
_this.delLayer(['clickQueryGeojsonPolyline', 'clickQueryGeojsonPoint'])
_this.view.popup.close()
return
}
await _this.loadGeojson('clickQueryGeojsonPolyline', res.data.json_url)
for (const item of features) {
const {type} = item.geometry
if (type === 'Point') {
hasPoint = true
await _this.loadGeojson('clickQueryGeojsonPoint', res.data.json_url, 'point')
break
}
}
const layerPolyline = _this.map.findLayerById('clickQueryGeojsonPolyline')
const layerPoint = _this.map.findLayerById('clickQueryGeojsonPoint')
let geojsonFeaturesPoint = {}
const geojsonFeaturesPolyline = await layerPolyline.queryFeatures()
/**
* 三种情况
* 如果图层不存在,直接把geojsonFeaturesPoint的features赋空
* 如果图层存在并且是当前加载 则查所有的点要素
* 如果图层存在但是上一次加载的结果,则删除图层并把geojsonFeaturesPoint的features赋空
*/
if (layerPoint) {
if (!hasPoint) {
_this.map.remove(layerPoint)
geojsonFeaturesPoint.features = []
} else {
geojsonFeaturesPoint = await layerPoint.queryFeatures()
}
} else {
geojsonFeaturesPoint.features = []
}
const geojsonFeatures = [...geojsonFeaturesPoint.features, ...geojsonFeaturesPolyline.features]
_this.view.popup.open({
location: event.mapPoint,
features: geojsonFeatures
}) /**
* 删除图层
* @params layerIdArr 要删除的图层id
*/
delLayer(layerIdArr) {
for(const item of layerIdArr) {
const layer = this.map.findLayerById(item)
if(layer) {
this.map.remove(layer)
}
}
}, /**
* 加载geojson数据
* @param layerID{String} 图层自定义的id.方便查找图层
* @param geometryType{String} geojson类型
* @param url{String} geojson的接口
* @param customRenderer{object} 样式
*/
async loadGeojson(layerID, url, geometryType = 'polyline', customRenderer = '') {
// 弹窗模板
const popupTemplate = {
title: clickQueryPopupTemplate.title[geometryType],
content: clickQueryPopupTemplate.content[geometryType],
}
let geojsonLayer = this.map.findLayerById(layerID)
await this.delLayer([layerID])
geojsonLayer = new GeoJSONLayer({
url: `http://xxx.xxxx.xxx/search_pipeline_info.json`,
copyright: 'RHgis',
id: layerID,
renderer: customRenderer || clickQueryPopupTemplate.renderer[geometryType],
popupTemplate,
geometryType
})
this.map.add(geojsonLayer)
},
使geoJSONLayer能够加载两种数据类型的geojson数据的更多相关文章
- ListView加载两种以上不同的布局
不同的项目布局(item layout) Listview一种单一的item 布局有时候不能完全满足业务需求,我们需要加载两种或两种以上不同的布局,实现方法很简单: 重写 getViewTypeCou ...
- Flask的配置文件加载两种方式
配置文件 1 基于全局变量 2 基于类的方式 配置文件的加载需要将配合文件的相对路径添加到app.config.from_object("文件路径"),类的方式也是一样,需要将类的 ...
- EasyUI使用tree方法生成树形结构加载两次的问题
html代码中利用class声明了easyui-tree,导致easyUI解析class代码的时候先解析class声明中的easyui-tree这样组件就请求了一次url:然后又调用js初始化代码请求 ...
- vue+element ui项目总结点(一)select、Cascader级联选择器、encodeURI、decodeURI转码解码、mockjs用法、路由懒加载三种方式
不多说上代码: <template> <div class="hello"> <h1>{{ msg }}</h1> <p> ...
- jQuery瀑布流从不同方向加载3种效果演示
很实用的一款插件jQuery瀑布流从不同方向加载3种效果演示在线预览 下载地址 实例代码 <section class="grid-wrap"> <ul clas ...
- 实验long raw 和 blob两种数据类型遇到dblink的表现
首先long raw从Oracle 10g开始就不再被建议使用,建议用blob代替.同理,long建议用clob代替. 本文从运维角度实验long raw 和 blob两种数据类型在遇到dblink时 ...
- 【实战问题】【1】@PostConstruct 服务启动后加载两次的问题
@PostConstruct:在服务启动时触发操作(我是用来更新微信的access_token) 解决方法: tomcat文件夹→conf→server.xml→将appBase="weba ...
- IE浏览器中IFrame被加载两次问题的解决-sunziren
本文为作者sunziren原创,首发博客园,转载请注明出处. 昨天遇到了一个问题,先上代码. var content = '<iframe src="www.baidu.com&quo ...
- js异步执行 按需加载 三种方式
js异步执行 按需加载 三种方式 第一种:函数引用 将所需加载方法放在匿名函数中传入 //第一种 函数引用 function loadScript(url,callback){ //创建一个js va ...
随机推荐
- 微信小程序 转盘抽奖 倒计时 整点
xml: <view id="luckdraw_box"> <view id="luckdraw_back"> <image st ...
- C# - 习题04_分析代码写出结果i1、i2、c.i、str、c.str
时间:2017-08-23 整理:byzqy 题目:分析如下代码,写出程序输出结果. 文件:Class1.cs using System; namespace Interview3 { class C ...
- 正整数a、b、c、d满足ab=cd,则a+b+c+d必定为合数。
正整数a.b.c.d满足ab=cd,则a+b+c+d必定为合数. 证法一:记s=a+b+c+d.如果四个数全为1,s=4,显然是合数.考虑四个数非全1的情形,由对称性,不妨令a>1. 设p是a的 ...
- Linux centos7 nginx 平滑升级
2021-08-19为了方便读者的阅读,该文通篇使用绝对路径,各位朋友们在实际上操作中可以根据实际情况编写路径(#^.^#)1. 当前环境 # system cat /etc/redhat-relea ...
- vue 之 v-model
v-model虽然很像使用了双向数据绑定的 Angular 的 ng-model,但是 Vue 是单项数据流,v-model 只是语法糖而已: <input v-model="sth& ...
- Spring系列.Environment接口
Environment 接口介绍 在 Spring 中,Environment 接口主要管理应用程序两个方面的内容:profile 和 properties. profile 可以简单的等同于环境,比 ...
- git02
Git Gui的使用 Ssh key 介绍及使用 Ssh key介绍 我理解的就是每台电脑上会产生出一个ssh key,然后自己有一个远程账户,但是自己有可能有很多台电脑, 包括家里的电脑还有公司的电 ...
- Spring Cloud Zuul 学习+实践
首先有必要了解一下什么是Zuul,它和Spring Cloud有什么关系. Zuul在Spring Cloud中承担着网关的职责,可以理解为客户端和服务端交互中的唯一通道.所有的客户端请求都会首先发送 ...
- Spring Cloud Eureka 实践(一)
Spring Cloud Eureka是Spring Cloud Netflix微服务套件中的一部分,主要在Spring Cloud架构中提供服务注册发现的功能.那么是不是可以尝试在本地搭一个单例Eu ...
- BFD协议简介
1. 背景 双向转发检测BFD(Bidirectional Forwarding Detection)是一种全网统一的检测机制,用于快速检测.监控网络中链路或者IP路由的转发连通状况. 为了保护关键应 ...