I am creating a Ionic React project that uses react-leaflet to display markers on a map. I followed the instructions from the docs to get started and while everything renders perfectly, the default example marker keeps on moving when zooming in/out. Here is the relevant code:
import { MapContainer, Marker as LeafletMarker, Popup, TileLayer } from 'react-leaflet';
...
return (
<MapContainer id="mapId" center={[51.505, -0.09]} zoom={13} scrollWheelZoom={false}>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<LeafletMarker position={[51.505, -0.09]}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</LeafletMarker>
</MapContainer>
);
The CSS for mapId
is this:
#mapId {
width: 100%;
height: 500px;
position: absolute;
z-index: 0;
}
It works well in every other way so it seems like there aren't any steps missing. My dependencies are the following:
"dependencies": {
"@types/react": "^17.0.0",
"@types/react-leaflet": "^2.5.2",
"leaflet": "^1.7.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-leaflet": "^3.0.5",
"typescript": "3.8.3"
...
"devDependencies": {
...
"@types/leaflet": "^1.5.19"
},
and I have included both the CSS and the JS from the leaflet docs:
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
I have looked all over the place for an answer and the thing that keeps popping up is the iconAnchor
and iconSize
issue that users face when trying to use a custom icon for their markers. I even tried to do that with the default marker by creating a custom icon with the properties of the default one. That didn't work either. Your help will be appreciated.
UPDATE
This is what the live project looks like when using default markers (markers are still not correct when not fully zoomed in)