如何为google maps api v3对象创建自定义事件? [英] How can I create custom events for google maps api v3 objects?

查看:193
本文介绍了如何为google maps api v3对象创建自定义事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个特殊类型的多边形对象,它包裹或继承自 google.maps.Polygon 类。我希望它有一个可以收听的自定义编辑的事件,理想情况下是通过正常的 addListener 接口。可以这样做吗?

I want to make a specialized type of polygon object that wraps around or inherits from the google.maps.Polygon class. I want it to have a custom edited event that can be listened to, ideally via the normal addListener interface. Can this be done?

推荐答案

我喜欢JustinY的答案。

I like JustinY's answer.

另一种方法是手动触发如下事件:

An alternative would be manually triggering an event like this:

 function CustomPolygon(options) {
     var self = this;
     // initialize any options
     console.log('init')
 }

 function edit() {
     // do work!!!
     // now tell people about it!
     google.maps.event.trigger(this, 'edited');
 }

 // extend CustomPolygon from google.maps.Polygon
 CustomPolygon.prototype = new google.maps.Polygon();
 CustomPolygon.prototype.edit = edit;

 // now you can do 
 var p = new CustomPolygon({ /*options*/
 });
 google.maps.event.addListener(p, 'edited', function () {
     document.body.innerHTML = 'edited!';
 });
 p.edit();

请参阅 http ://jsfiddle.net/stevejansen/hQZcT/

资料来源: https://developers.google.com/maps/documentation/javascript/reference#event

这篇关于如何为google maps api v3对象创建自定义事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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