Google 地图 api 中的 infoWindow 数组 [英] An array of infoWindow in Google maps api

查看:16
本文介绍了Google 地图 api 中的 infoWindow 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查找错误但找不到.由于某种原因,任何谷歌地图信息窗口总是显示相同的信息.

I looked for error and I couldn't find it. Any google maps infoWindow always shows the same information for some reason.

这是一个咖啡脚本代码

infowindow = new google.maps.InfoWindow()
for company in companiesData
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(company.latitude, company.longitude)
    map: map
  })

  #debugger ---> each company.name is different!
  google.maps.event.addListener(marker, 'click', ()->
    infowindow.setContent(company.name)
    infowindow.open(map,this)
  )

我对其进行了调试,发现每个 company.name 都不相同.

I debugged it and saw that each company.name was different.

输出的javascript

The output javascript

  infowindow = new google.maps.InfoWindow();
  _results = [];
  for (_j = 0, _len1 = companiesData.length; _j < _len1; _j++) {
    company = companiesData[_j];
    marker = new google.maps.Marker({
      position: new google.maps.LatLng(company.latitude, company.longitude),
      map: map
    });
    _results.push(google.maps.event.addListener(marker, 'click', function() {
      infowindow.setContent( company.name);
      return infowindow.open(map, this);
    }));
  }
  return _results;
});

那么哪里出错了?

推荐答案

你需要获取变量 company 的闭包.您可以通过在单独的函数中创建标记来做到这一点.例如:

you need to obtain closure on the variable company. You can do that by creating your markers in a separate function. for example:

infowindow = new google.maps.InfoWindow()
for(var n = 0 ; n < n companiesData.length ;n++){
  createMarker(companiesData[n]);
}

function createMarker(data){
  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(parseFloat(data.latitude), parseFloat(data.longitude)),
    map: map
  })

  #debugger ---> each company.name is different!
  google.maps.event.addListener(marker, 'click', function(){
    infowindow.setContent(data.name);
    infowindow.open(map,this);
  }
  )
}

在此说明.

这篇关于Google 地图 api 中的 infoWindow 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆