如果你在 Marker 里面加一个 Popup 。

这个东西会自动加一个点击出现的事件。

而我想改成鼠标放上去出现,离开时消失。

 

上代码。(请粘贴到 https://react-leaflet.js.org/ 的编辑器中查看)

const position = [51.505, -0.09]
function DraggableMarker() { 

  const markerRef = useRef(null)
  const popRef= useRef(null)
  const eventHandlers = useMemo(
    () => ({
      mouseover() {
        const marker = markerRef.current        
        marker.openPopup()      
      },
      mouseout() {
        const marker = markerRef.current
        marker.closePopup()
      }
    }),
    [],
  )

  return (
    <Marker ref={markerRef} eventHandlers={eventHandlers} position={position}>
      <Popup ref={popRef} >
        A pretty CSS3 popup. <br /> Easily customizable.
      </Popup>
    </Marker>
  )
}
        
render(
  <MapContainer center={position} zoom={13} scrollWheelZoom={false}>
    <TileLayer
      attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    />
    <DraggableMarker />
  </MapContainer>
)

以上。

 

 

相关文章:

  • 2021-04-14
  • 2022-01-23
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-04
  • 2021-05-17
  • 2021-11-20
  • 2022-12-23
  • 2021-12-03
  • 2021-10-01
相关资源
相似解决方案