我是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?管理这种情况的最好方法是什么?
谢谢!
发布于 2014-12-13 05:32:25
您可以使用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
});发布于 2014-12-13 05:48:00
如果要读取本地文件,则需要使用HTML5 File API。获得CSV数据后,我将对其进行解析并将其转换为JSON。Here就是一个很好的例子,它包含了一个演示链接。
如果您不想使用HTML5 File API,可以将CSV文件上传到服务器并在那里将CSV转换为JSON。请找到solution in PHP here。
我建议您像这样创建一个JSON,因为这很容易在客户端使用JavaScript进行处理:
[
{
"latitude":40.3456455,
"longitude":-74.6558775
},
{
"latitude":40.3456456,
"longitude":-74.6558772
}
] https://stackoverflow.com/questions/27452210
复制相似问题