Here Maps API (JS):GeoJSON 加载的多边形 return 属性字段的空值
Here Maps API (JS): GeoJSON loaded polygons return null value for properties field
我的 HERE 地图 Javascript 实现正在加载 GeoJSON 多边形,其属性字段设置有数据(下面的示例)。当我将 GeoJSON 加载到地图中并附加点击侦听器(“点击”事件)时,我想获取在 GeoJSON 中设置的属性。当我检查 clickevent 目标时,它说属性字段为空,而我希望那里有我的 GeoJSON 值。
一个类似的问题,如果不一样的话,大约 2 年前已经回答了: 提供的解决方案似乎不适用于 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 reader,解析数据并将其添加到地图中:
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());
然后我想为添加到地图的每个多边形附加一个点击监听器(使用 HERE 地图团队 2 年前的响应):
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 的属性字段进行了如下说明:
Auxiliary data that accompanies geometries (the contents of the field properties) is bound to the map object and can be fetched with the method getData() on that object. See H.map.Object#getData.
我尝试使用该信息来探索如何将其转化为他们的 API,但一直无法 运行(例如)在(此处)地图对象本身上获取数据。我还尝试从点击事件目标 (e.target) 获取对象,但也无法获取任何 属性 值。
找到的解决方案:
由于 GeoJSON 不是一个多边形列表,而是一个特征列表,包含 'MultiPolygons',我首先必须找到被点击元素的父元素:
使用 getParentGroup() 将在 addEventListener 的回调中发挥作用:
console.log(e.target.getParentGroup().getData().properties);
getParentGroup() 将确保您可以访问 GeoJSON 属性。默认情况下,属性数据不是 added/copied 到基于 Feature/MultiPolygon GeoJSON 数据的子多边形。
例如,在我的例子中,这将在点击处理程序中获取 GeoJSON 属性 'buurtcode':
e.target.getParentGroup().getData().properties.buurtcode
我的 HERE 地图 Javascript 实现正在加载 GeoJSON 多边形,其属性字段设置有数据(下面的示例)。当我将 GeoJSON 加载到地图中并附加点击侦听器(“点击”事件)时,我想获取在 GeoJSON 中设置的属性。当我检查 clickevent 目标时,它说属性字段为空,而我希望那里有我的 GeoJSON 值。
一个类似的问题,如果不一样的话,大约 2 年前已经回答了:
我使用的 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 reader,解析数据并将其添加到地图中:
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());
然后我想为添加到地图的每个多边形附加一个点击监听器(使用 HERE 地图团队 2 年前的响应):
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 的属性字段进行了如下说明:
Auxiliary data that accompanies geometries (the contents of the field properties) is bound to the map object and can be fetched with the method getData() on that object. See H.map.Object#getData.
我尝试使用该信息来探索如何将其转化为他们的 API,但一直无法 运行(例如)在(此处)地图对象本身上获取数据。我还尝试从点击事件目标 (e.target) 获取对象,但也无法获取任何 属性 值。
找到的解决方案:
由于 GeoJSON 不是一个多边形列表,而是一个特征列表,包含 'MultiPolygons',我首先必须找到被点击元素的父元素:
使用 getParentGroup() 将在 addEventListener 的回调中发挥作用:
console.log(e.target.getParentGroup().getData().properties);
getParentGroup() 将确保您可以访问 GeoJSON 属性。默认情况下,属性数据不是 added/copied 到基于 Feature/MultiPolygon GeoJSON 数据的子多边形。
例如,在我的例子中,这将在点击处理程序中获取 GeoJSON 属性 'buurtcode':
e.target.getParentGroup().getData().properties.buurtcode