Fetch the Property Value of GeoJSON Polygon on Click
Problem
=======
Sometimes it is necessary to detect a GeoJSON polygon click and read its custom property values. For detailed documentation on GeoJSON, see the HERE Maps for JavaScript API Reference.
Solution
========
To detect a polygon click, use the following GeoJSON input:<br /> { "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": { "customProp": "heyImACustomProperty" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 16.45477294921875, 43.51619059561274 ], [ 16.450481414794922, 43.50772499687011 ], [ 16.470909118652344, 43.5019975949657 ], [ 16.481552124023438, 43.51021500212034 ], [ 16.475543975830078, 43.518306809754804 ], [ 16.45477294921875, 43.51619059561274 ] ] ] } }]}<br />
To read property values, use the following code:<br /> reader = new H.data.geojson.Reader('/path/to/geojson/file.json'); reader.parse(); map.addLayer(reader.getLayer()); reader.getLayer().getProvider().addEventListener("tap", function(e) { if(e.target instanceof H.map.Polygon) { console.log('Custom property value: ', e.target.getData().properties.customProp); } });<br />