javascript - 从每个循环中运行函数(ajax.response)

标签 javascript google-maps-api-3 each getjson

我在尝试从每个循环内将数据传递到函数时遇到问题。我大致将我的代码粘贴在下面。基本上,functionA 获取 JSON 数据,将响应传递给 functionB。然后我想将每个响应项传递给 functionC,以便可以将它们添加到数组中以使用谷歌地图绘制标记。任何帮助,将不胜感激。谢谢

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();

});

最佳答案

正在使用 $.ajax() 设置对象的上下文调用 functionB。您可以使用 $.proxy() 将上下文更改为 app.Application:

  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=?", $.proxy(self.functionB, self));
    };

};

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) {
            self.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();

});

关于javascript - 从每个循环中运行函数(ajax.response),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15868118/

相关文章:

javascript - 如果在 Promise 链中的某个点抛出错误,它是否会自动沿着链向下传播?

php - 使用信息窗口设置标记聚类器标记

javascript - 如何禁用谷歌地图自动完成功能

javascript - underscore _.each 和 forEach 有什么区别?

javascript - 多边形SVG的不同笔画

javascript - 如何在javascript中一段时间​​后清除sessionstage

javascript - 如何在 ReactJS 的 ListView 中插入一个新项目

javascript - Javascript 和 C# 之间来回通信

jquery - 查找每个元素的最后一个子元素

javascript - 使用 $.each 循环 JQuery 将 td 附加到行