问题描述

  在使用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数据的更多相关文章

  1. ListView加载两种以上不同的布局

    不同的项目布局(item layout) Listview一种单一的item 布局有时候不能完全满足业务需求,我们需要加载两种或两种以上不同的布局,实现方法很简单: 重写 getViewTypeCou ...

  2. Flask的配置文件加载两种方式

    配置文件 1 基于全局变量 2 基于类的方式 配置文件的加载需要将配合文件的相对路径添加到app.config.from_object("文件路径"),类的方式也是一样,需要将类的 ...

  3. EasyUI使用tree方法生成树形结构加载两次的问题

    html代码中利用class声明了easyui-tree,导致easyUI解析class代码的时候先解析class声明中的easyui-tree这样组件就请求了一次url:然后又调用js初始化代码请求 ...

  4. vue+element ui项目总结点(一)select、Cascader级联选择器、encodeURI、decodeURI转码解码、mockjs用法、路由懒加载三种方式

    不多说上代码: <template> <div class="hello"> <h1>{{ msg }}</h1> <p> ...

  5. jQuery瀑布流从不同方向加载3种效果演示

    很实用的一款插件jQuery瀑布流从不同方向加载3种效果演示在线预览 下载地址 实例代码 <section class="grid-wrap"> <ul clas ...

  6. 实验long raw 和 blob两种数据类型遇到dblink的表现

    首先long raw从Oracle 10g开始就不再被建议使用,建议用blob代替.同理,long建议用clob代替. 本文从运维角度实验long raw 和 blob两种数据类型在遇到dblink时 ...

  7. 【实战问题】【1】@PostConstruct 服务启动后加载两次的问题

    @PostConstruct:在服务启动时触发操作(我是用来更新微信的access_token) 解决方法: tomcat文件夹→conf→server.xml→将appBase="weba ...

  8. IE浏览器中IFrame被加载两次问题的解决-sunziren

    本文为作者sunziren原创,首发博客园,转载请注明出处. 昨天遇到了一个问题,先上代码. var content = '<iframe src="www.baidu.com&quo ...

  9. js异步执行 按需加载 三种方式

    js异步执行 按需加载 三种方式 第一种:函数引用 将所需加载方法放在匿名函数中传入 //第一种 函数引用 function loadScript(url,callback){ //创建一个js va ...

随机推荐

  1. wpf 的style

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  2. spring框架学习日志一

    一.简介 1.对spring框架的简单理解 可以理解为它是一个管理对象的创建.依赖.销毁的容器 Spring 是一个开源框架. Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单 ...

  3. 使用Operator State方式

    使用 operator state的方式有以下几种: 方式一: stateful function(RichFunction) 实现 CheckpointFunction 接口 必须实现两个方法:Vo ...

  4. oracle中常用函数

    1.oracle中 trunc 是截取的函数,用在日期类型上,就是截取到的日或时间. select trunc(sysdate) from dual   默认是截取系统日期到日,得到 2012-12- ...

  5. jwt三种方式

    package library.book.demo.config.loginconfig; import com.alibaba.fastjson.JSON; import com.sun.org.a ...

  6. redis《三》连接池配置参数

    参数 值 setTestWhileIdle() 在空闲时检查有效性 true setMinEvictableIdleTimeMillis() 连接最小空闲时间 1800000L setTimeBetw ...

  7. Red Hat Enterprise Linux 7.2修改主机名(hostname)

    Red Hat Enterprise Linux 7.2在安装的时候,会默认生成主机名:localhost. 那么如何修改成自己想要的自己名? //格式为:用户名@主机名 比如: [root@loca ...

  8. idea导出jar包及坑

    导出基本步骤 1.打开项目结构,在artifact新建一个jar 2.然后填写主类和依赖 3.这里的坑: 4.查看 5.点击编译输出 6.得到jar包

  9. JS 之 每日一题 之 算法 ( 有多少小于当前数字的数字 )

    给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目. 换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j != i 且 ...

  10. (1)RabbitMQ在Docker上安装

    1.简介 在来学习RabbitMQ时候,我觉得很有必要先把它的环境先搭建起来,这样后面的示例才能进行.因为之前自己手动在Linux服务器上搭建过Elasticsearch,当时踩过太多坑了,浪费太多时 ...