Closed Delky91 closed 4 months ago
finally i found the way to do it in Next JS
"use client";
import React, { useEffect, useRef } from "react";
import "leaflet/dist/leaflet.css";
import "leaflet-better-filelayer/dist/leaflet.betterfilelayer.css";
const Map2 = () => {
const mapRef = useRef();
const mapInstance = useRef(null);
useEffect(() => {
if (!mapInstance.current) {
const L = require("leaflet");
require("leaflet-better-filelayer");
const osm = L.tileLayer(
"http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
{
attribution: "Map data © 2013 OpenStreetMap contributors",
}
);
mapInstance.current = L.map(mapRef.current)
.setView([-23.70078702319699, -46.69719607766831], 16)
.addLayer(osm);
const options = {
button: document.getElementById("input-btn"),
};
console.log(L.Control.betterFileLayer(options));
L.Control.betterFileLayer(options).addTo(mapInstance.current);
}
}, []);
return (
<div>
<div>
<span>Leaflet.BetterFileLayer</span>
<a href='https://github.com/gabriel-russo/Leaflet.BetterFileLayer'>
github.com/gabriel-russo/Leaflet.BetterFileLayer
</a>
<div>
<p>Control Panel</p>
<input
type='file'
id='input-btn'
multiple
accept='.gpx,.kml,.geojson,.json,.csv,.topojson,.wkt,.shp,.shx,.prj,.dbf,.zip'
/>
</div>
</div>
<div
ref={mapRef}
style={{ width: "60vw", height: "90vh" }}></div>
</div>
);
};
export default Map2;
That's correct, that's the way i use in my personal projects too.
Sorry for the lack of documentation, soon i will fix that.
If it helps, here's an example using MaterialUI:
import "leaflet-better-filelayer";
// Others imports.......
// useRef and others declarations....
useEffect(() => {
// ..... code ......
bfl.current = new L.Control.BetterFileLayer({
button: btn.current,
fileSizeLimit: 10240
});
map.on("bfl:layerloaderror", () => { console.log("Your Layer failed to load") });
map.addControl(bfl.current);
// ...... code ......
}, []);
return (
<IconButton color="inherit" {...props}>
<FileUploadIcon />
<input
hidden
type="file"
ref={btn}
multiple
accept=".gpx,.kml,.geojson,.json,.csv,.topojson,.wkt,.shp,.shx,.prj,.dbf,.zip"
/>
</IconButton>
);
ooo i see i have acceses to bfl that could be a good add to the docs, ty it not 100% what i need cause i was in need of a parser but if i modify my map i can still get the data so from the files that i just load, tysm for the library and the example
I'm attempting to integrate a plugin, specifically L.Control.betterFileLayer(options), into a React application using React-Leaflet, but encountering difficulties. Despite installing the plugin via npm and following the documentation, I'm unable to get it to work.
this is a try really bad code and im using nextJS i just delete the react-leaflet try
im really trying to brute force at this moment, i think i will have to transform all the data to geoJSON and then show it at this time, i found u library but still cant make it work, any advice or someething will be appreciate