search

Home  >  Q&A  >  body text

How to add click event handler for objectManager in React yandex maps?

I'm using the react-yandex-maps (https://github.com/R1ZEN/react-yandex-maps) library, and I need to handle click events for a single landmark, but I don't know how to do it. How do I add an onClick event on the render object on the map?

<YMaps>
    <Map className="w-full h-full"
        state={{ center: center, zoom: 12 }}
        onBoundsChange={handleBoundsChange}
    >
        <ObjectManager
            options={{
                clusterize: true,
            }}
            objects={{
                preset: "islands#greenDotIcon",
            }}
            clusters={{
                preset: "islands#redClusterIcons",
            }}
            defaultFeatures={data.result.data.map((el: APZ) => ({
                type: "Feature",
                id: el.id,
                geometry: {
                    type: "Point",
                    coordinates: [el.lat, el.long],
                },
            }
            ))}
        />
    </Map>
</YMaps>
P粉450079266P粉450079266439 days ago579

reply all(1)I'll reply

  • P粉083785014

    P粉0837850142023-09-17 11:35:06

    Here’s how to do it.

    <ObjectManager
      //其余代码相同
       onObjectClick={(e) => {
            const id = e.get('objectId'); // 获取Id 
            console.log(`Id is: ${id}`);
          }}
    
    />

    reply
    0
  • Cancelreply