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

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

问题描述

我找错了,我找不到。

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

以下是一个coffeescript代码

Here is a coffeescript code

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;
});

那么错误在哪里?

推荐答案

您需要获取关于变量公司的闭包。您可以通过在单独的函数中创建标记来实现。例如:

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地图中的一个infoWindow数组api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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