在我的代码中,我正在尝试上传xlsx文件,该文件包含Latitutde和Longitude数据,我想在googlemap上显示。
在handleFiles函数中,我能够上传xlsx文件,然后将xlsx文件作为结果读入json数组。我从结果json数组中选择Lat Long数据到另一个名为" out"的数组中。我正试图让#34; out"我的handleFile函数之外的数组并将其添加到initMap()函数中,以便使用lat long值在我的地图上添加标记。但是我遇到了麻烦,因为如果我将out变量设为全局变量,我得到一个空数组。谁能帮我 ?
function handleFile(e) {
//Get the files from Upload control
var files = e.target.files;
var i, f;
var x = [];
var y = [];
var out = [];
//Loop through files
for (i = 0, f = files[i]; i != files.length; ++i) {
var reader = new FileReader();
var name = f.name;
reader.onload = function (e) {
var data = e.target.result;
var result;
var workbook = XLSX.read(data, { type: 'binary' });
var sheet_name_list = workbook.SheetNames;
sheet_name_list.forEach(function (y) { /* iterate through sheets */
//Convert the cell value to Json
var roa = XLSX.utils.sheet_to_json(workbook.Sheets[y]);
if (roa.length > 0) {
result = roa;
}
});
//Get Long, values from results array and save to
for (i=0; i<result.length; i++) {
obj = result[i];
y.push(obj.Latitude);
x.push(obj.Longitude);
}
for (i=0;i<x.length;i++){
out.push([x[i],y[i]]);
};
};
reader.readAsArrayBuffer(f);
}
}
//Change event to dropdownlist
$(document).ready(function(){
$('#files').change(handleFile);
});
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 48.7758459, lng: 9.1829321},
zoom: 8
});
}