【问题标题】:Passing array variables through functions in javascript通过javascript中的函数传递数组变量
【发布时间】:2013-04-14 20:53:38
【问题描述】:

我正在使用 Google Maps API 和 Instagram API 在地图上显示流行的图像标记。一旦用户选择了一个,我希望在该位置拍摄的图像的缩略图显示在信息气泡中。我让这两个 API 分别工作得很好,但我不知道如何让它们一起工作。我知道我必须面对范围问题才能从 $.(each) 循环中取出变量,但我不知道如何在不使用 return 语句停止每个“循环”的情况下这样做。而且,一旦我返回它,它就不再是一个数组了,不是吗?这是我的 Javascript:

// get instagram info

function requestJSON(urlToRequest) {
        var headElement = document.getElementsByTagName("head")[0];         
        var newScriptTag = document.createElement('script');
        newScriptTag.setAttribute('type','text/javascript');
        newScriptTag.setAttribute('src',urlToRequest);
        headElement.appendChild(newScriptTag);
}

function callThis(responseData) {
    console.log(responseData);
    $.each( responseData.data, function( i, data ) {
        //console.log(item);
        var image = data.images.thumbnail.url;
        var location = data.location;
        if (location) {
            $( "<img/>" ).attr( "src", image ).appendTo( "#showPics" );
            // decimal will need to be rounded to .000000
            var lat = data.location.latitude;
            var lon = data.location.longitude;
            console.log(lat);
            console.log(lon);
        }
        else {
            return;
        }
            });
}

$(document).ready(function() {
    $('p a').on('click',function(e) {
        e.preventDefault();
        requestJSON('https://api.instagram.com/v1/media/popular?client_id=MYCLIENTID&callback=callThis');
    });
});



// get the map info

function initialize() {
    var mapOptions = {
      center: new google.maps.LatLng(20, 0),
      zoom: 2,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map-canvas"),
        mapOptions);

    //hardcoded map info that I want to be an array of locations i.e. lat and long
    var locations = [
          ['Somewhere a picture was taken',  33.86857, -117.8778],

    ];

    var infowindow = new google.maps.InfoWindow();
    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });
      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
}
google.maps.event.addDomListener(window, 'load', initialize);

【问题讨论】:

    标签: javascript jquery api google-maps-api-3 jsonp


    【解决方案1】:

    可能只有一个数组,并在$.each 之前将其声明为全局类似window.locationsArray = [](或可能是普通对象的数组),将其填充到$.each 中,并在迭代完成后开始在 GMap 上绘制包含locationsArray 的点,类似于-

    $.each(locationsArray, function(index, value) {
    
                              var aLatLong = *--*Have the Lat+Longitude*--*;
                              var aContent = "";
                              map_canvas.gmap('addMarker', { 
                              'position':aLatLong  
                               }).click(function() {
                               map_canvas.gmap('openInfoWindow', { content :'<p style="color:black;">'+value[2]+'</p><p style="color:black;"><b>'+'Views: '+aContent+'</b></p>'  }, this);
                                        });
    
                                         });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多