如何在鼠标悬停在细分上时从 geo json 图层获取 json 属性值?
How to get json attribute value from geo json layer when mouse overring a subdivision?
我注意到 json 的格式如下:
{
"type": "FeatureCollection",
"features":
[
{
"type": "Feature",
"id": "310230",
"properties":
{
"name": "XXX District",
"cp": [141.5637,61.5383 ],
"childNum": 1
},
"geometry":
{
"type": "Polygon",
"coordinates":
[.....]
}
}
]
}
如何在鼠标悬停在对应的admin subdivision中的某个点上时获取[features:properties:name](这里是"XXX District")的值?
查看此示例 http://openlayers.org/en/v3.14.2/examples/vector-layer.html,它准确地演示了您正在尝试执行的操作:加载 GeoJSON 并在鼠标悬停在要素上时显示其属性之一。
您要找的活动是ol.Map#pointermove
活动。要获取特定像素的特征,请使用 ol.Map#forEachFeatureAtPixel
方法
这是一个片段:
map.on('pointermove', function(evt) {
if (evt.dragging) {
return;
}
var pixel = map.getEventPixel(evt.originalEvent);
displayFeatureInfo(pixel);
});
并在 displayFeatureInfo
方法内部,获取像素处的特征:
var feature = map.forEachFeatureAtPixel(pixel, function(feature) {
return feature;
});
并从特征中获取 属性:
feature.get('name');
我注意到 json 的格式如下:
{
"type": "FeatureCollection",
"features":
[
{
"type": "Feature",
"id": "310230",
"properties":
{
"name": "XXX District",
"cp": [141.5637,61.5383 ],
"childNum": 1
},
"geometry":
{
"type": "Polygon",
"coordinates":
[.....]
}
}
]
}
如何在鼠标悬停在对应的admin subdivision中的某个点上时获取[features:properties:name](这里是"XXX District")的值?
查看此示例 http://openlayers.org/en/v3.14.2/examples/vector-layer.html,它准确地演示了您正在尝试执行的操作:加载 GeoJSON 并在鼠标悬停在要素上时显示其属性之一。
您要找的活动是ol.Map#pointermove
活动。要获取特定像素的特征,请使用 ol.Map#forEachFeatureAtPixel
方法
这是一个片段:
map.on('pointermove', function(evt) {
if (evt.dragging) {
return;
}
var pixel = map.getEventPixel(evt.originalEvent);
displayFeatureInfo(pixel);
});
并在 displayFeatureInfo
方法内部,获取像素处的特征:
var feature = map.forEachFeatureAtPixel(pixel, function(feature) {
return feature;
});
并从特征中获取 属性:
feature.get('name');