如何正确过滤 yandex 地图上的数据?
Posted
技术标签:
【中文标题】如何正确过滤 yandex 地图上的数据?【英文标题】:How correct filter data on yandex maps? 【发布时间】:2019-02-24 15:36:59 【问题描述】:我正在尝试过滤 yandex 地图上的数据。 CodePen Example
如果基站有多个频段(2G、3G 和 4G),那么如果它的至少一个频段在过滤器中,那么它应该在结果中。
我有 listBoxControl 和 listBoxItems = ['2G', '3G', '4G']
我的过滤函数如下所示:
function getFilterFunction(categories)
return function(obj)
let bsBands = obj.has_bands;
/* How correct filter data? */
return categories['2G'] && categories['3G'] && categories['4G'];
GeoJSON 看起来像这样:
"count": 4,
"next": null,
"previous": null,
"type": "FeatureCollection",
"features": [
"id": 1,
"region_prefix": "97",
"cell_site_number": 1,
"description": "",
"address": "",
"commissioning": "",
"bs_id": "",
"height_asl": 0,
"bands": [
"name": "2G",
"frequency": "900"
,
"name": "2G",
"frequency": "1800"
,
"name": "3G",
"frequency": "2100"
,
"name": "4G",
"frequency": "1800"
,
"name": "4G TDD",
"frequency": "2600"
],
"status": true,
"type": "Feature",
"geometry":
"type": "Point",
"coordinates": [55.755815, 37.613]
,
"properties":
"balloonContentHeader": "balloonContentHeader",
"balloonContentBody": "balloonContentBody",
"balloonContentFooter": "balloonContentFooter",
"clusterCaption": "clusterCaption",
"hintContent": "hintContent",
"iconCaption": "2G 3G 4G"
,
"has_bands": [
"3G",
"2G",
"4G"
]
,
"id": 2,
"region_prefix": "97",
"cell_site_number": 2,
"description": "",
"address": "",
"commissioning": "",
"bs_id": "",
"height_asl": 0,
"bands": [
"name": "2G",
"frequency": "900"
,
"name": "2G",
"frequency": "1800"
,
"name": "3G",
"frequency": "2100"
,
"name": "4G",
"frequency": "1800"
,
"name": "4G TDD",
"frequency": "2600"
],
"status": true,
"type": "Feature",
"geometry":
"type": "Point",
"coordinates": [55.759, 37.613]
,
"properties":
"balloonContentHeader": "balloonContentHeader",
"balloonContentBody": "balloonContentBody",
"balloonContentFooter": "balloonContentFooter",
"clusterCaption": "clusterCaption",
"hintContent": "hintContent",
"iconCaption": "2G 3G 4G"
,
"has_bands": [
"3G",
"2G",
"4G"
]
,
"id": 3,
"region_prefix": "97",
"cell_site_number": 3,
"description": "",
"address": "",
"commissioning": "",
"bs_id": "",
"height_asl": 0,
"bands": [
"name": "3G",
"frequency": "2100"
],
"status": true,
"type": "Feature",
"geometry":
"type": "Point",
"coordinates": [55.7204, 37.6167]
,
"properties":
"balloonContentHeader": "balloonContentHeader",
"balloonContentBody": "balloonContentBody",
"balloonContentFooter": "balloonContentFooter",
"clusterCaption": "clusterCaption",
"hintContent": "hintContent",
"iconCaption": "3G"
,
"has_bands": [
"3G",
]
,
"id": 4,
"region_prefix": "97",
"cell_site_number": 4,
"description": "",
"address": "",
"commissioning": "",
"bs_id": "",
"height_asl": 0,
"bands": [
"name": "4G",
"frequency": "1800"
,
"name": "4G TDD",
"frequency": "2600"
],
"status": true,
"type": "Feature",
"geometry":
"type": "Point",
"coordinates": [55.7704, 37.6119]
,
"properties":
"balloonContentHeader": "balloonContentHeader",
"balloonContentBody": "balloonContentBody",
"balloonContentFooter": "balloonContentFooter",
"clusterCaption": "clusterCaption",
"hintContent": "hintContent",
"iconCaption": "4G"
,
"has_bands": [
"4G"
]
]
【问题讨论】:
预期的输出是什么? @molamk,根据文档tech.yandex.com/maps/doc/jsapi/2.1/ref/reference/…过滤功能。将单个对象添加到 ObjectManager。如果函数返回 true,则对象将被处理。如果为 false,则该对象将被排除在进一步处理之外。字符串也可以作为过滤器传递。对于过滤器,您可以指定返回 true 或 false 的表达式。 【参考方案1】:我自己解决了这个问题。 CodePen Example
function getBand(e, i, a)
let Band = this.valueOf();
return e === Band;
function getFilterFunction(categories)
return function(obj)
let bsBands = obj.has_bands;
let has2G = bsBands.find(getBand, '2G');
let has3G = bsBands.find(getBand, '3G');
let has4G = bsBands.find(getBand, '4G');
return (categories['2G'] && has2G) || (categories['3G'] && has3G) || (categories['4G'] && has4G);
【讨论】:
以上是关于如何正确过滤 yandex 地图上的数据?的主要内容,如果未能解决你的问题,请参考以下文章