0

There is a geojson:

{
  "features": [
    {
      "id": "f6105acf-065e-499b-b23d-85298dd84108",
      "geometry": {
        "coordinates": [
          55.094384525,
          51.810150862,
          106.179835026
        ],
        "type": "Point"
      },
      "properties": {
        "createDate": "2022-11-05T18:42:40.83",
        "createUser": "sight1",
        "extendedData": "{\"fields\": {\"type\": 1}}",
        "inventoryObjectType": 63,
        "panoId": 17927769,
        "status": 1,
        "taskId": "18f596db-912a-4c1f-b343-9d1e920579d0",
        "trackid": 9727
      },
      "type": "Feature"
    }
  ],
  "type": "FeatureCollection"
}

I catch the map click when user clicks over feature:

   const feature = this.mapRef.forEachFeatureAtPixel(
      evt.pixel,
      function (feature) {
        return feature;
      }
    );

    if (feature) {
      debugger;
      return;
    }

Why do I get features array inctead one feature (screen 1):

enter image description here

1 Answer 1

2

Your question is not complete. From .forEachFeatureAtPixel function result it's obvious that you vector layer source is cluster, so upon click you get cluster feature, where features that comprise the cluster are available through .get('features') method. That's what you see under values_.features property.

So to display those features in the console code could look like:

const feature = this.mapRef.forEachFeatureAtPixel(
  evt.pixel,
  function (feature) {
    return feature;
  }
);

if (feature) {
  console.log(feature.get('features'));
}
3
  • When I zoom in I get one element of cluster - feature. Alwasys I got features as array because as you meantored it is cluster. Okay. Then I need to open cluster, zoom and always to get the first element of array, right? Commented Apr 11, 2023 at 13:40
  • I have tried this: const clusterFeatures = feature.get('features'); if (clusterFeatures.length > 1) { clusterFeatures.forEach((feature) => { extend(extent, feature.getGeometry().getExtent()); }); this.mapRef .getView() .fit(extent, { duration: 1000, padding: [50, 50, 50, 50] }); Commented Apr 11, 2023 at 13:50
  • The above is answer to your question. It's customary on GIS SE site to mark it as accepted, to let others with similar questions/problems that question was answered. If you have new/additional question, please post it as a separate question.
    – TomazicM
    Commented Apr 11, 2023 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.