本文介绍了使用gmaps4rails将事件监听器添加到标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用gmaps4rails(https://github.com/apneadiving/Google-Maps-for-Rails)的应用程序.我想向标记添加事件侦听器,以便它在地图旁边可视化创建(以及在地图上显示信息窗口).是否可以使用gmaps4rails为每个标记添加事件监听器?

I am developing an application that uses gmaps4rails (https://github.com/apneadiving/Google-Maps-for-Rails). I want to add an event listener to the marker so that it creates a visualization next to the map (as well as displaying an info window on the map). Is it possible to add an event listener for each marker using gmaps4rails?

推荐答案

当然可以.

您应该在gmaps4rails_callback javascript函数中编写代码,以确保在设置完所有内容后执行该代码.

You should write your code in the gmaps4rails_callback javascript function to be sure it's executed when everything is setup.

然后循环markers js变量:Gmaps4Rails.markers

And then loop the markers js variable: Gmaps4Rails.markers

此数组中每个标记的属性是:

The attributes of each marker in this array are:

  • 经度

  • longitude

纬度

包含Google标记的google_object

google_object containing the google marker

那说明你可以做任何你想做的事.

That said you can do whatever you want.

作为旁注,Gmaps4Rails.map也可以使用该地图.

As a side note, the map is also available doing Gmaps4Rails.map.

通常,请阅读gmaps4rails.js文件,该文件有据可查(我希望如此).

In general, read the gmaps4rails.js file, it's well documented (I hope!).

您在评论中解释的问题很奇怪,当我使用时,它对我来说非常有效:

The problem you explain in the comments is weird, it works perfectly for me when I use:

google.maps.event.addListener(Gmaps4Rails.markers[0].google_object, 'click', function(object){ alert("hello"); });

我想您应该尝试使用更传统的for循环,例如:

I guess you should try to use a more traditional for loop like:

<script type="text/javascript"> 
function gmaps4rails_callback() { 
  function say_yo(arg) { return function(){alert('yo '+ arg + '!' );};};
    for (var i = 0; i <  Gmaps4Rails.markers.length; ++i) {
      google.maps.event.addListener(Gmaps4Rails.markers[i].google_object, 'click', say_yo(i));
    }
}
</script>

这篇关于使用gmaps4rails将事件监听器添加到标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 00:06