Auto open markers popup on react-leaflet map
Asked Answered
D

2

4

I use some markers on react-leaflet map to show various text.
But I can not find a flag for the autoOpen tooltip.

I get (position, children, onOpen, onClose) as available attributes.

render() {
    return (
        <div className={'ShapeLayer'}>
            {
                this.shapes.map(shape => {
                    return (
                        <Marker key={shape['id']} position={shape['coordinates']} draggable={false} opacity={1}>
                            <Popup>
                                <span>{shape['text']}</span>
                            </Popup>
                        </Marker>
                    );

                })
            }
        </div>
    )
}

This is done with this code on native leaflet

var marker = L.marker(shapess[i]['coordinates'], {
        opacity: 0.01
    }).bindTooltip(shapess[i]['text'],
        {
            permanent: true,
            className: "shapesText" + i,
            offset: [0, 0],
            direction: "center"
        }
    ).openTooltip().addTo(mymap);

How can I do the same on react_leflet

Deckhouse answered 9/1, 2018 at 15:25 Comment(2)
You're trying to open multiple Popups automatically when the map is shown?Cornel
Yes :) I just do that on native Leaflet.Deckhouse
D
8

You can use Tooltip instead of a Popup if it's just for text and then use permanent attribute on the Tooltip.

 <Marker key={shape['id']} position={shape['coordinates']} draggable={false} opacity={1}>
    <Tooltip permanent>
          <span>{shape['text']}</span>
    </Tooltip>
</Marker>

Here is the source for more examples :

react-leaflet/example/components/tooltip.js

Deferential answered 9/1, 2018 at 19:23 Comment(3)
Is there a way to have the tooltip open or closed based on a state value?Rogovy
Yes, just use open={this.state.isOpen}Deferential
open={this.state.isOpen} does not appear to work inside the Tooltip tag.Rogovy
B
0

Update: 2023, Use setTimeout to access the openPopup() in the marker.

import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet';
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
import { useRef } from 'react';

const Location = () => {
  const position = [6.632264, 124.597816];
  const laptopMarker = L.icon({
    iconUrl: './icons/laptop.gif',
    iconSize: [40, 40],
    popupAnchor: [0, -20],
  })

  const marker = useRef(null);
  return (
    <MapContainer whenReady={() => {
      setTimeout(() => {
        marker.current.openPopup();
      }, 3000);
    }} center={position} zoom={9} scrollWheelZoom={false} >
      <TileLayer
        attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
      />
      <Marker ref={marker} position={position} icon={laptopMarker}>
        <Popup maxWidth={250}>
          Hi! <img src='./icons/wave.gif' width='15' height='15' className='inline-block mb-1 mr-1' /> are you interested where I live?
          You can find me here!
        </Popup>
      </Marker>
    </MapContainer >
  )
}

export default Location;
Boldface answered 19/3, 2023 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.