This problem is occurring in the older version try using "react-leaflet": "^3.2.1", "leaflet": "^1.7.1", and it would resolve, if you are using react 17 and upper .
https://codesandbox.io/s/cluster-mapping-leaflet-9bkes
import { TileLayer, Tooltip, Marker, MapContainer } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import MarkerClusterGroup from "react-leaflet-markercluster";
import { customMarker } from "./constants";
import L from "leaflet";
const Cluster = () => {
let data = {
minLat: -6.1751,
maxLat: 55.7558,
minLong: 37.6173,
maxLong: 139.6917
};
const centerLat = (data.minLat + data.maxLat) / 2;
var distanceLat = data.maxLat - data.minLat;
var bufferLat = distanceLat * 0.05;
const centerLong = (data.minLong + data.maxLong) / 2;
var distanceLong = data.maxLong - data.minLong;
var bufferLong = distanceLong * 0.05;
const zoom = 4;
const cities = [
{ position: [22.583261, 88.412796], text: "A" },
{ position: [22.58289, 88.41366], text: "B" }
];
return (
<MapContainer
style={{ height: "480px", width: "100%" }}
zoom={zoom}
center={[centerLat, centerLong]}
bounds={[
[data.minLat - bufferLat, data.minLong - bufferLong],
[data.maxLat + bufferLat, data.maxLong + bufferLong]
]}
scrollWheelZoom={true}
>
<TileLayer url="http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<MarkerClusterGroup>
{cities?.map((mark, i) => (
<Marker
style={{ color: "red" }}
key={i}
position={mark?.position}
icon={customMarker}
>
<Tooltip direction="top" offset={[10, 0]}>
<span style={{ fontSize: 14, fontWeight: "bold" }}>
{mark?.text}
</span>
</Tooltip>
</Marker>
))}
</MarkerClusterGroup>
</MapContainer>
);
};
export default Cluster;
export const customMarker = new L.Icon({
iconUrl: "https://unpkg.com/[email protected]/dist/images/marker-icon.png",
iconSize: [25, 41],
iconAnchor: [10, 41],
popupAnchor: [2, -40]
});
Map
toMapContainer
could work. There is no named export withMap
- could be an API change. – Alberthaalberti