1
我在尝试从每个循环内将数据传递给函数时遇到问题。我粗略地粘贴了下面的代码。基本上,函数A获取JSON数据,并将响应传递给函数B.然后,我想将每个响应项传递给functionC,以便将它们添加到数组中以使用Google地图绘制标记。任何帮助,将不胜感激。谢谢从每个循环内运行函数(ajax.response)
if(app == undefined) var app = {};
app.Application = function() {
this.functionA = function(){
var self = this;
var urlHash = location.hash;
accessToken = urlHash.split('=')[1];
if (!accessToken) {
return false;
} else {
$.getJSON(instaAPIuri+"users/" + id + "/media/recent?access_token=" + accessToken + "&callback=?", self.functionB);
};
};
this.functionB = function(response){
var self = this;
//Error codes
if (response.meta.code == 400) {
alert(response.meta.error_message);
}
//Create picture elements with basic information and image
$.each(response.data, function (i, item) {
//If item.location == null, while trying to get geolocation = error
if (item.location != null) {
functionC(item.location.latitude, item.location.longitude, item.images.thumbnail.url, item.user.username);
}
});
};
this.functionC = function(latitude, longitude, imgurl, user) {
var self = this;
var latLngPosition = new google.maps.LatLng(latitude, longitude);
//Create marker with custom assets
marker = new google.maps.Marker({
position:latLngPosition,
icon: new google.maps.MarkerImage(imgurl,
new google.maps.Size(110, 110),
new google.maps.Point(0,0),
new google.maps.Point(32, 32)),
title: user,
map:map
});
//Push in array to delete later
markersArray.push(marker);
};
this.init();
};
$(function() {
var app = new app.Application();
});
也许尝试使用self.functionC而不是简单的functionC? – 2013-04-07 21:42:06
我试过,但自我指的是在每个循环中的项目。 TypeError:self.functionC不是函数 – jjkilpatrick 2013-04-07 21:59:29
我设法修复它。必须将匿名回调函数传递给$ .getJSON。另外它会失败,因为我没有包含我的else语句 – jjkilpatrick 2013-04-07 23:43:29