我正在使用 Leaflet JavaScript 并遵循本教程:
https://www.html5rocks.com/en/tutorials/file/dndfiles/
我正在为我的工作构建一个工具。它允许用户上传包含纬度/经度信息的 CSV 文件,并通过 Leaflet Omnivore 将其绘制在 map 上。
我能够让 html5 示例正常工作。但是,我什么时候处理它读入的数据呢? Leaflet Omnivore 需要上传文件的本地路径,如下所示:
var testCSV = omnivore.csv('path/to/file.csv').addTo(map).on('error', function(error) {
console.log(error);
});
据我所知,出于安全原因这是不可能的。
还有其他方法可以获取文件路径吗?还是我的做法完全错误?
最佳答案
如果您不需要通过 AJAX 调用远程检索文件(例如使用 omnivore.csv
),但您已经拥有 CSV 内容作为字符串,您可以直接使用omnivore.csv.parse
功能:
var layer = omnivore.csv.parse(csvStringContent).addTo(map);
// Executed synchronously, so no need to use the .on('ready') listener.
map.fitBounds(layer.getBounds());
关于html - 使用 HTML5 文件阅读器并将其发送到 Leaflet Omnivore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45359618/