Google Maps API v3 - 为什么没有上下文与事件? [英] Google Maps API v3 - why no context with events?

查看:137
本文介绍了Google Maps API v3 - 为什么没有上下文与事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次使用Google Maps API v3,并且我有一堆标记的地图。我想要这样做,当你点击一个,一个特定的InfoWindow将显示(特定于您点击的标记)。我真的很惊讶,点击事件并没有告诉你点击的实际标记!

Using Google Maps API v3 for the first time and I've got a map with a bunch of markers. I wanted to make it so when you click one, a specific InfoWindow will display (specific to the marker you clicked). I was really surprised that the click event doesn't tell you the actual marker that was clicked!

我知道有一个解决方案使用单独的方法来创建一个关闭,但是这似乎是对我的一个黑客。有更好的方法吗?或者,有没有办法问地图什么标记存在于这个位置,并从事件参数中传递位置?

I know there is a solution using a separate method to create a closure but that seems like a hack to me. Is there a better way to do it? Or, is there a way to ask the map "what markers exist at this position" and pass in the position from the event argument?

我预计事件将像这样工作:

I expected events to work like this:

google.maps.event.addListener(marker, 'click', function(event, obj)
{
    //Now I can work with "obj" - the thing that was clicked.
});


推荐答案

您应该在事件中引用this监听器。

You should just refer to 'this' in the event listener.

google.maps.event.addListener(marker, 'click', function(e) {
  // this == marker;
  // e == MouseEvent
});

这篇关于Google Maps API v3 - 为什么没有上下文与事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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