Javascript 无法通过$.ajax使用Zomato API数据填充infowindows

Javascript 无法通过$.ajax使用Zomato API数据填充infowindows,javascript,jquery,ajax,google-maps,Javascript,Jquery,Ajax,Google Maps,我试图让infowindows填充ZomatoAPI数据,但我一直在获取对象中的最后一项。我尝试了一个带有闭包的for循环和一个带有“let”而不是“var”的for循环,但这并没有帮助。任何能推动我前进的建议都将不胜感激 var映射; var标记=[]; 各种菜系、名称、机构、地点、菜单、照片、评级、信息内容; 变量位置=[ {名称:'Ghirardelli广场的酒吧',latlng:{lat:37.8063722222,lng:-122.422888889}, {名称:'爱尔兰银行',拉丁

我试图让infowindows填充ZomatoAPI数据,但我一直在获取对象中的最后一项。我尝试了一个带有闭包的for循环和一个带有“let”而不是“var”的for循环,但这并没有帮助。任何能推动我前进的建议都将不胜感激

var映射;
var标记=[];
各种菜系、名称、机构、地点、菜单、照片、评级、信息内容;
变量位置=[
{名称:'Ghirardelli广场的酒吧',latlng:{lat:37.8063722222,lng:-122.422888889},
{名称:'爱尔兰银行',拉丁语:{拉丁语:37.7902750000,拉丁语:-122.4048472222},
{姓名:“流氓旧金山公屋”,LATLNG:{LAT:37.8001440000,LNG:- 122.4104550000 },
{名称:'Chieftain Irish Restaurant&Pub',拉丁语:{拉丁语:37.781490000,拉丁语:-122.4051510000},
{名称:'Kennedy's Irish Pub and Curry House',拉丁语:{拉丁语:37.8042510000,拉丁语:-122.4156040000},
{name:'Murphy's Pub',latlng:{lat:37.7901916667,lng:-122.4038472222}
];
//从Google Maps API创建地图实例
//抓取对“地图”id的引用以显示地图
//设置贴图选项对象属性
函数initMap(){
map=new google.maps.map(document.getElementById(“map”){
中心:{
拉脱维亚:37.7884162,
液化天然气:-122.4127457
},
缩放:14
});
var infowindow=new google.maps.infowindow();
var标记;
对于(变量i=0;i

地图

下面指示的行是错误的,请删除它,您的代码即可工作:

marker.addListener('click', function() {
  populateInfoWindow(this, infowindow);
  // infowindow.setContent(infoContent); <<<<<<<<<<<<  REMOVE
});

对这个答案的发现是理解范围的一个很好的练习。我还发现,我的数据对象可以在标记的for循环中使用相同的迭代器

//create instance of a map from the Google Maps API
//Grab the reference to the "map" id to display the map
//Set the map options object properties
function initMap() {
  map = new google.maps.Map(document.getElementById("map"), {
   center: {
   lat: 37.7884162, 
   lng: -122.4127457
 },
 zoom: 14
});

var marker;

//$.ajax call 
$.ajax({
method: "GET",
crossDomain: true,
url: "https://developers.zomato.com/api/v2.1/search?count=6&lat=37.79161&lon=-122.42143&establishment_type=6",
dataType: "json",
async: true,
headers: {
  "user-key": "0a661374a6b58eb2fa84142d27fe81ca"
}, 
success: function(data) {
  passZomatoData(data);
}, 
error: function() {
  infoContent = "<div>Sorry, data is not coming through. Refresh and try again.</div>";
}
});//end of $.ajax call

//function passZomatoData()
function passZomatoData(data) {
var infowindow = new google.maps.InfoWindow();
pubs = data.restaurants;
console.log(data);

for(var i = 0; i < locations.length; i++) {
  (function() {
    // get the position fronm the locations array
    var position = locations[i].latlng;
    var title = locations[i].name;
    var cuisine = pubs[i].restaurant.cuisines;
    var address = pubs[i].restaurant.location.address;
    console.log(address);
    //create a marker per location and put into markers array
    var marker = new google.maps.Marker({
      map: map,
      position: position,
      title: title,
      cuisine: cuisine, 
      address: address,
      animation: google.maps.Animation.DROP
    });

    //push the marker to our array of markers
    markers.push(marker);

    //extend the boundaries of the map for each marker
    marker.addListener('click', function() {
      populateInfoWindow(this, infowindow);
      infowindow.setContent('<div><b>Pub name:<b> ' + marker.title + '</div><div><b>Address:<b> ' + marker.address + '</div><div><b>Cuisine:<b> ' + marker.cuisine + '</div>');
     });
   })(i);//end of closure
  }//end of for loop
 }

}; //end initMap()

function populateInfoWindow(marker, infowindow) {
  //check to make sure the infowindow is not already opened in this marker
  if (infowindow.marker != marker) {
  infowindow.marker = marker;
  //infowindow.setContent('<div>' + marker.title + '</div>' + marker.infoContent);
  infowindow.open(map, marker);
  //Make sure the marker property is cleared if the infowindow is closed
  infowindow.addListener('closeclick', function() {
  infowindow.setMarker = null;
  });
 }
}// end of populateInfoWindow
//从Google Maps API创建地图实例
//抓取对“地图”id的引用以显示地图
//设置贴图选项对象属性
函数initMap(){
map=new google.maps.map(document.getElementById(“map”){
中心:{
拉脱维亚:37.7884162,
液化天然气:-122.4127457
},
缩放:14
});
var标记;
//$.ajax调用
$.ajax({
方法:“获取”,
跨域:是的,
url:“https://developers.zomato.com/api/v2.1/search?count=6&lat=37.79161&lon=-122.42143和机构类型=6“,
数据类型:“json”,
async:true,
标题:{
“用户密钥”:“0a661374a6b58eb2fa84142d27fe81ca”
}, 
成功:功能(数据){
passZomatoData(数据);
}, 
错误:函数(){
infoContent=“很抱歉,数据未通过。请刷新并重试。”;
}
});//结束$.ajax调用
//函数passZomatoData()
函数passZomatoData(数据){
var infowindow=new google.maps.infowindow();
酒吧=数据餐厅;
控制台日志(数据);
对于(变量i=0;i