Leaflet map with react-leaflet-markercluster cluster spiderfy's or closes onClick
Asked Answered
S

0

6

I have a Leaflet map in React using react-leaflet and react-leaflet-markercluster

Clustering seems to work beautifully, but if I click on one of the cluster CircleMarker or Tooltip it opens a new link (which I have as an onClick) but unfortunately the cluster spiderfy closes, so if the user wants to click other links, they need to reopen (and re-spiderfy) the cluster, and rinse and repeat. Is there any way to keep the spiderfy open onClick, and close it on zoom out (the latter which already behaves correctly)?

E.g. clicking "Huckberry" will open a new link, close the tooltip:

enter image description here

But unfortunately the cluster will also close and spiderfy after clicking the onClick link:

enter image description here

And here is my code:

        <Map
          style={{ height: "480px", width: "100%", opacity: "0.9" }}
          zoom={screensizeZoom}
          maxZoom={20}
          center={[37.7687477, -99.6820275]}
          attributionControl={false}>
          <TileLayer url="https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}{r}.png"
            attribution="Map by <a href='http://stamen.com' target='_blank'>Stamen Design</a> | &copy; <a href='https://www.openstreetmap.org/copyright' target='_blank'>OpenStreetMap</a> contributors"
          />

          <AttributionControl position="bottomright" prefix={false} />

          <MarkerClusterGroup
            spiderfyDistanceMultiplier={1}
            showCoverageOnHover={false}
            maxClusterRadius={35}
          >
            {this.state.dataMaps.map((dataItem, k) => {
              let { coordinates, company, url, loc } = dataItem;
              return (
                <CircleMarker onClick={() => { window.open(url) }}
                  key={k}
                  center={[coordinates[0], coordinates[1]]}
                  position={[coordinates[0], coordinates[1]]}
                >
                  <Tooltip direction="right" offset={[-8, -2]} opacity={1}>
                    <span><a href={url}>{company}</a></span>
                    <span>{loc}</span>
                  </Tooltip>
                </CircleMarker>);
            })}
          </MarkerClusterGroup>

        </Map>

Thanks!

Sphygmomanometer answered 8/3, 2020 at 18:36 Comment(1)
try to use popupRef.bindPopup instead of <tooltip> and pass html link as a string like "<a href='url' target='_blank'></a>" as the popup first paramDeniable

© 2022 - 2024 — McMap. All rights reserved.