SO 21321157: Google Maps Custom Events

How can I create custom events for google maps api v3 objects?

by Steve Jansen

HTML

<script src="https://maps.googleapis.com/maps/api/js?v=3&amp;sensor=false&amp;libraries=geometry"></script>

JavaScript

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