我是 JavaScript 新手,正在制作在线 map 可视化。我需要将地理坐标的本地 csv 文件作为数组读取。
例如,我有:
var data = [
new google.maps.LatLng(40.3456455, -74.6558775),
new google.maps.LatLng(40.3456456, -74.6558772),
];
我希望能够使用 CSV 文件中的坐标来填充它。
我查看了jquery-csv,但它要求将csv作为字符串传入,并且传递文件路径并让Javascript读取文件是不可能的。我应该对字符串进行硬编码,还是尝试在 Python 中读取它并以某种方式将字符串传递给 Javascript?管理这个问题的最佳方法是什么?
谢谢!
最佳答案
您可以使用 Jquery get 函数示例读取文件及其内容:
//the path to your csv file
var filePath ="pathToYourCSVFile";
$.get(filePath,function(data){
//do something with the data
//whatever you do with the data needs to been done inside this function
//example of what you could do
//array to hold the lines of your data file
//data string is split up by each new line
var lines = data.split("\n");
//you could then loop through each line and populate the values you need for each line
});
关于javascript - 从 Javascript 读取硬编码 csv 文件的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27452210/