当鼠标覆盖细分时,如何从geo json图层获取json属性值?

时间:2016-03-18 03:03:57

标签: openlayers-3 geojson

我注意到json的格式如下:

{
   "type": "FeatureCollection",
   "features": 
    [
      {
        "type": "Feature",
        "id": "310230",
        "properties": 
        {
            "name": "XXX District",
            "cp": [141.5637,61.5383 ],
            "childNum": 1
        },

        "geometry": 
        {
            "type": "Polygon",
            "coordinates": 
            [.....]
        }
      }
    ]
 }

当鼠标覆盖相应管理员细分中的一个点时,如何获取[features:properties:name](此处为“XXX District”)的值?

1 个答案:

答案 0 :(得分:1)

请查看此示例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');
相关问题