簡體   English   中英

通過javascript中的函數傳遞數組變量

[英]Passing array variables through functions in javascript

我正在使用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);

可能只是在$.each之前有一個數組,並將其聲明為全局like- 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);
                                    });

                                     });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM