0

我的 HERE Maps Javascript 实现正在加载 GeoJSON 多边形,其属性字段设置为数据(下面的示例)。当我将 GeoJSON 加载到地图中并附加一个单击侦听器(“tap”事件)时,我想获取在 GeoJSON 中设置的属性。当我检查 clickevent 目标时,它说属性字段为空,而我希望我的 GeoJSON 值在那里。

一个类似的,如果不一样,问题已在近 2 年前得到回答:HereMaps GeoJSON Polygon click read properties提供的解决方案似乎不适用于 Here Maps 的 V3.1 API。

我使用的 GeoJSON 示例:

{
"type": "FeatureCollection",
"name": "cities",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "fid": 1234, "buurtcode": "BU123456", "buurtnaam": "Centrum", "wijkcode": "XYZ", "jaarstatcode": "2089" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ .... ] ] ] } },
[and so on]

我正在使用的 Javascript 代码:

我设置了一个 GeoJSON 阅读器,解析数据并将其添加到地图中:

var reader = new H.data.geojson.Reader( undefined, {...} );
reader.parseData(myGeoJsonDataVariable); // It's a variable I use, not a (remote) file
map.addLayer(reader.getLayer());

然后我想为添加到地图中的每个多边形附加一个单击侦听器(使用 2 年前 HERE Maps 团队的响应):HereMaps GeoJSON Polygon click read properties

reader.getLayer().getProvider().addEventListener("tap", function(e) {
    if(e.target instanceof H.map.Polygon) {
        
        // This is where the magic should happen:

        // The console.log doesn't show the propertie value from the GeoJSON (e.g. 'fid' or 'buurtcode')
        // Logging e.target.getData() by itself already returns 'undefined'. 
        console.log('Custom property value: ', e.target.getData().properties.customProp);

        // The toGeoJSON method works, but shows the properties, again, as 'null'
        console.log( e.target.toGeoJSON() )
    }
}

HERE 地图提供的文档对 GeoJSON 的属性字段进行了如下说明:

几何图形附带的辅助数据(字段属性的内容)绑定到地图对象,并且可以使用该对象上的 getData() 方法获取。请参阅 H.map.Object#getData。

来源:https ://developer.here.com/cn/documentation/maps/hls-chn/topics_api/h-data-geojson-reader.html#h-data-geojson-reader__class-description

我尝试使用该信息来探索如何将其转换为他们的 API,但无法在(此处)地图对象本身上运行(例如)getData。我还尝试从单击事件目标(e.target)中获取对象,但也无法获取任何属性值。

4

1 回答 1

0

找到的解决方案:

由于 GeoJSON 不是多边形列表,而是包含“MultiPolygons”的特征列表,我首先必须找到单击元素的父元素:

使用getParentGroup()将在 addEventListener 的回调中发挥作用:

console.log(e.target.getParentGroup().getData().properties);

getParentGroup() 将确保您可以访问 GeoJSON 属性。默认情况下,属性数据不会添加/复制到基于 Feature/MultiPolygon GeoJSON 数据的子多边形。

例如,在我的例子中,这将在点击处理程序中获取 GeoJSON 属性“buurtcode”:

e.target.getParentGroup().getData().properties.buurtcode
于 2020-12-30T10:39:42.817 回答