我是JavaScript的新手,正在进行在线地图可视化。我需要在地理坐标的本地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?管理这个的最佳方法是什么?
谢谢!
答案 0 :(得分:1)
您可以使用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
});
答案 1 :(得分:0)
如果要阅读本地文件,则需要使用HTML5 File API。 拥有CSV数据后,我会解析它并转换为JSON。 Here是一个很好的例子,包括演示的链接。
如果您不想使用HTML5文件API,可以将CSV文件上传到服务器并将CSV转换为JSON。请找solution in PHP here。
我建议您创建一个这样的JSON,因为在客户端使用JavaScript很容易处理:
[
{
"latitude":40.3456455,
"longitude":-74.6558775
},
{
"latitude":40.3456456,
"longitude":-74.6558772
}
]