在javascript中通过函数传递数组变量
我正在使用谷歌地图API和Instagram API在地图上显示流行的图像标记。一旦用户选择了一个,我希望在该位置拍摄的图像的缩略图显示在信息气泡中。我有两个API可以单独工作,但我不知道如何让它们一起工作。我知道为了从$(each)循环中获取变量,我将不得不面对范围问题,但我不知道如何在不使用return语句停止each“循环”的情况下这样做。一旦我返回它,它就不再是一个数组了,是吗?以下是我的Javascript:在javascript中通过函数传递数组变量,javascript,jquery,api,google-maps-api-3,jsonp,Javascript,Jquery,Api,Google Maps Api 3,Jsonp,我正在使用谷歌地图API和Instagram API在地图上显示流行的图像标记。一旦用户选择了一个,我希望在该位置拍摄的图像的缩略图显示在信息气泡中。我有两个API可以单独工作,但我不知道如何让它们一起工作。我知道为了从$(each)循环中获取变量,我将不得不面对范围问题,但我不知道如何在不使用return语句停止each“循环”的情况下这样做。一旦我返回它,它就不再是一个数组了,是吗?以下是我的Javascript: // get instagram info function reques
// 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);
//获取instagram信息
函数requestJSON(urlToRequest){
var headElement=document.getElementsByTagName(“head”)[0];
var newScriptTag=document.createElement('script');
setAttribute('type','text/javascript');
setAttribute('src',urlToRequest);
headElement.appendChild(newScriptTag);
}
函数callThis(responseData){
控制台日志(responseData);
$。每个(响应数据、函数(i、数据){
//控制台日志(项目);
var image=data.images.缩略图.url;
var位置=data.location;
如果(位置){
$(“可能只是有一个数组,并将其声明为全局的-window.locationsArray=[]
(也可能是一个普通对象数组)就在$之前。每个
,将其填充到$中。每个
在迭代完成后,开始在GMap上绘制包含位置光线
的点-
$.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);
});
});
$。每个(位置数组、函数(索引、值){
var aLatLong=*-*具有纬度+经度*-->;
var aContent=“”;
map_canvas.gmap('addMarker',{
“位置”:长
})。单击(函数(){
map_canvas.gmap('openInfoWindow',{content:''+value[2]+'
'+'视图:'+aContent+'
'},这是);
});
});