我想知道如何遷移下面的 javascript 代碼(插件)以在 React 中運行。它允許將 kml 文件上傳到在 html 中運行的傳單,但在 React-Leaflet 中不起作用
https://www.npmjs.com/package/leaflet-filelayer
我已經在為 React 使用 Leaflet 的一個版本,但它沒有允許在地圖上上傳 kml 文件的對象......
感謝您的指導
創建一個自定義組件,您將在安裝並將其與togeojson
庫一起導入後加載傳單文件層庫。
因此安裝並導入兩個庫
import togeojson from 'togeojson'
import fileLayer from 'leaflet-filelayer'
調用fileLayer(null, L, togeojson)
能夠使用L.Control.fileLayerLoad
(源)
然後創建你在官方庫中看到的組件,並將邏輯放在 useEffect 下
...imports
fileLayer(null, L, togeojson);
...
export default function LeafletFileLayer() {
const map = useMap();
useEffect(() => {
const control = L.Control.fileLayerLoad({
fitBounds: true,
layerOptions: {
style: style,
pointToLayer: function (data, latlng) {
return L.circleMarker(latlng, { style: style });
}
}
});
control.addTo(map);
control.loader.on("data:loaded", function (e) {
var layer = e.layer;
console.log(layer);
});
}, [map]);
return null;
}
像這樣使用它
<MapContainer ...>
...
<LeafletFileLayer />
</MapContainer>
我在沙箱上上傳了來自官方傳單站點的 geojson 和一個 kml 以便能夠對其進行測試。您必須先下載它們。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句