Google Maps JS API v3:需要从事件处理程序中访问标记对象 [英] Google Maps JS API v3: Need to access Marker Object from inside the Event Handler

查看:185
本文介绍了Google Maps JS API v3:需要从事件处理程序中访问标记对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个处理我的Google Mapping功能的MapObject。对象将地图上的所有标记收集到数组中。 (我使用的是Prototype JS Library。)当创建标记时,它们的Click事件被映射到MapObject上的一个函数。一切都按预期工作,除非我处于点击事件中,否则我无法再访问触发事件的标记。如果我将Marker绑定到事件处理程序,那么我无法访问MapObject的方法和属性。以下是一些示例代码,以显示正在发生的情况。

I've created a MapObject that is handling my Google Mapping functionality. The object collects all the Markers on the map into an Array. (I'm using the Prototype JS Library.) As the Markers are created their Click event is mapped to a function on the MapObject. Everything works as expected except once I'm in the Click event I can no longer access the Marker that fired the event. If I bind the Marker to the event handler then I can't access the methods and properties of the MapObject. Here's some sample code to show what is going on.

MappingObject = Class.create({
initialize: function()
{
    try
    {
        this.geocoder = new google.maps.Geocoder();
        this.trucks = $A();
        this.truckPic = "Images/TruckIcon.jpg";
    }
    catch (ex)
    {
        this.sendToConsole("FAIL! " + ex.toString());
    }
},

putTruckOnMapWithLatLong: function(latitude, longitude, title)
{
    var marker = this.getNewTruck(this.getNewLatLong(latitude, longitude));
    try
    {
        marker.setTitle(title);
        google.maps.event.addListener(marker, "click",
                                                  this.truckClickHandler);
        this.trucks.push(marker);
    }
    catch (ex)
    {
        this.sendToConsole(ex.toString());
    }
},

truckClickHandler: function(event)
{
    $("dashboardmessage").update(this.getTitle());
    this.setIcon("Images/small_smiley.jpeg");
}})

getNewTruck()函数只返回一个Google标记对象。问题是,truckClickHandler中的this可以引用MapObject,但是我无法访问标记对象。如果我绑定这样的标记对象...

The getNewTruck() function just returns a Google Marker Object. The problem is that the "this" in the truckClickHandler can refer to the MapObject but then I can't access the marker object. If I bind the marker object like such...

google.maps.event.addListener(marker, "click", this.truckClickHandler.bind(marker));

然后我无法访问MapObject中的函数。我会感谢任何帮助我可以得到。谢谢!

Then I can't access the functions in the MapObject anymore. I'd appreciate any help I could get. Thanks!

推荐答案

好的,对于那些有兴趣的人,我终于明白了。原型有一个.curry()方法。如果我这样声明我的truckClickHandler ...

Okay, for those who are interested I finally figured this out. Prototype has a .curry() method. If I declare my truckClickHandler like this...

truckClickHandler: function(marker, event)

然后将其设置为...

And then set it like this...

google.maps.event.addListener(marker, "click", this.truckClickHandler.curry(marker).bind(this));

然后我可以访问标记对象,这意味着我的MapObject如预期。去原型!

Then I have access to both the marker object and this refers to my MapObject as expected. Go Prototype!

这篇关于Google Maps JS API v3:需要从事件处理程序中访问标记对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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