【发布时间】:2014-02-16 11:56:51
【问题描述】:
我正在尝试遍历图像的 json 数组,将图像添加到地图上的标记中。由于 javascript 是异步的,它给我带来了问题,我想在将图像添加到我的地图之前等待图像加载,并且在循环完成之前无法加载我的图像。这是否可以实现,因为我尝试使用回调实现但无法使其工作。
for (var i = 0; i < jsonObj.potholes.length; i++)
{
var image = new Image();
image.src = "data:image/png;base64," + jsonObj.potholes[i].image;
image.onload = function()
{
//alert("image loaded");
EXIF.getData(image, function()
{
otn = parseInt(EXIF.getTag(image, "Orientation"));
dataURL = drawCanvas(otn, image).toDataURL();
var circle = L.circle([jsonObj.potholes[i].lat, jsonObj.potholes[i].lon], 18, {
color: 'yellow',
fillColor: 'red',
fillOpacity: 0.5
}).addTo(markersLayerGroup).bindPopup("Pothole ID " + jsonObj.potholes[i].id
+ "<br />Location " + city[i] + "," + street[i] +
"<image src = '" + dataURL + "'></image>");
});
}
【问题讨论】:
标签: javascript jquery loops asynchronous onload