博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mapbox根据多边形选择点要素
阅读量:4028 次
发布时间:2019-05-24

本文共 1510 字,大约阅读时间需要 5 分钟。

Mapbox GL JS 对于按空间选择提供了 queryRenderedfeature函数,但是此函数仅支持按点选和标准矩形的框选,不支持按多边形选择。帮助文档中是这样说的:

查询区域的几何图形:描述边界框的单个点或西南和东北点。

因此,想要查询需要借助其他开源库,本文使用Mapbox推荐的turf.js实现

本文实现效果如下:

核心代码:

map2.addControl(draw);map2.on('draw.create', updateArea);map2.on('draw.delete', updateArea);map2.on('draw.update', updateArea);function updateArea(e) {    var data = draw.getAll();    var answer = document.getElementById('calculated-area');    if (data.features.length > 0) {        var userPolygon = e.features[0];        // generate bounding box from polygon the user drew        var polygonBoundingBox = turf.bbox(userPolygon);        var southWest = [polygonBoundingBox[0], polygonBoundingBox[1]];        var northEast = [polygonBoundingBox[2], polygonBoundingBox[3]];        var northEastPointPixel = map2.project(northEast);        var southWestPointPixel = map2.project(southWest);        var features = map2.queryRenderedFeatures([southWestPointPixel, northEastPointPixel], { layers: ['pointslayer'] });        var filter = features.reduce(function(memo, feature) {            if (! (undefined === turf.intersect(feature, userPolygon))) {                // only add the property, if the feature intersects with the polygon drawn by the user                memo.push(feature.properties.FID);            }            return memo;        }, ['in', 'FID']);        map2.setFilter("selected", filter);    } else {        answer.innerHTML = '';        if (e.type !== 'draw.delete') alert("Use the draw tools to draw a polygon!");    }}

 

转载地址:http://jgxbi.baihongyu.com/

你可能感兴趣的文章
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
jQuery性能优化指南
查看>>
Oracle 物化视图
查看>>
Multi-Task Networks With Universe, Group, and Task Feature Learning-阅读笔记
查看>>
A Survey of Zero-Shot Learning: Settings, Methods, and Applications-阅读笔记
查看>>
Cross-Domain Review Helpfulness Prediction -论文阅读
查看>>
NLP数据增强方法-(一)
查看>>
BERT+实体-百度ERNIE优化了啥
查看>>
NLP数据增强方法-动手实践
查看>>
学习让机器学会学习-Meta Learning课程笔记-1
查看>>
学习让机器学会学习-Meta Learning课程笔记-2
查看>>
RNN及其变种LSTM/GRU/SRU
查看>>
我还不知道Attention有哪些-公式代码都带你搞定
查看>>
自学习-怎么让对话助手越来越强
查看>>
BERT-flow:bert的向量表达是SOTA吗?
查看>>
Preprocessing data-sklearn数据预处理
查看>>
Java实现Oracle到MySQL的表迁移
查看>>
子类A继承父类B, A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是?
查看>>