New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to add leaflet map to canvas with latest version of react-leaflet? #869
Comments
Hi, This is my first day using react-leaflet so YMMW but it appears that any options not understood by So you should be able to simply do:
Not sure if |
In the same topic, Is there any way to use react-leaflet v3 with a feature like https://github.com/lycheelin/react-leaflet-canvas-markers/blob/master/src/leaflet.canvas-markers.js ? I need to display 1000 markers and the performances are currently pretty bad when the map is scrolled, the idea would be to use Canvas instead of actual SVG. Is there any sample/example that shows the usage of canvas.getContext() on react-leaflet v3 ? It's really hard to find any support with this new version. Many plugins are getting obsolete (e.g full screen) and important features like OverlayCanvas are really hard to implement.. Thanks for any help |
@dngconsulting this is how I've done it. Unfortunately I don't have time to do a complete copy/paste example, but it should get you started: ...
import L from "leaflet";
import reactElementToJSXString from 'react-element-to-jsx-string';
...
const Map = (props) => {
const [myMarkers, setMyMarkers] = useState( L.layerGroup());
const [map, setMap] = useState(null)
const setMapReference = (map) => {
setMap(map);
myMarkers.addTo(map);
setMyMarkers(myMarkers)
map.on('zoomend', function() {
let radius = 8;
const zoom = map.getZoom();
console.log(zoom);
if (zoom < 18) {
radius = 7;
}else if(zoom < 17) {
radius = 6;
}else if(zoom < 16) {
radius = 5;
}else if(zoom < 15) {
radius = 4;
}else if(zoom < 14) {
radius = 1;
}
myMarkers.eachLayer(function (marker) {
marker.setRadius(radius);
});
});
}
const center = {
lat: 58.9809976,
lng: 16.2129849,
};
const refreshLocations = async () => {
myMarkers.clearLayers();
let locs = await getLocations();
if (!locs) {
return false;
}
let latlngpairs = [];
locs.forEach(function (location){
latlngpairs.push([location['latitude'], location['longitude']])
let coordinates = L.latLng(location['latitude'], location['longitude']);
let popup = (<>Marker information here</>);
L.circleMarker(coordinates, {
radius: 8,
fillColor: "FF0000",
fillOpacity: 1,
color: '#fff',
weight: 3,
}).bindPopup(
reactElementToJSXString(popup)
).addTo(myMarkers);
};
})
if (latlngpairs.length > 0) {
const bounds = L.latLngBounds(latlngpairs.map((c) => {
return [c[0], c[1]];
}));
map.fitBounds(bounds);
}
return (
<MapContainer center={center} zoom={13} style={{ height: `calc(100%)` }} whenCreated={setMapReference} scrollWheelZoom={true} preferCanvas={true} renderer={L.canvas()}>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
</MapContainer>
)
} |
Hi, |
I'm trying to add leaflet map to canvas with react-leaflet v3. In latest versions of react-leaflet there isn't Map object there is MapContainer object only. So I can't add property preferCanvas=true to MapContainer. It is noteworthy that in old versions of react-leaflet there was Map object with preferCanvas option but then it was removed. I think another way to add map to canvas exists. Please, help.
The text was updated successfully, but these errors were encountered: