Mapbox-gl-js: How to add (and then fire) a custom event on mapboxgl.Map ?

Created on 10 Mar 2015  路  6Comments  路  Source: mapbox/mapbox-gl-js

There are already some events defined such as 'render', 'load', 'move'.

Can you give me an example of how I can add a custom event to the Map object and then fire it?

Thanks

Most helpful comment

@misterfresh You can register an event handler with on and then fire it when you want the function to be called. i.e.

var map = new mapboxgl.Map({container:document.createElement('div')});
map.on('foo', function() { alert('bar'); })
map.fire('foo');

All 6 comments

@misterfresh You can register an event handler with on and then fire it when you want the function to be called. i.e.

var map = new mapboxgl.Map({container:document.createElement('div')});
map.on('foo', function() { alert('bar'); })
map.fire('foo');

Thanks, this works!

@tristen Is the fire method still available? Otherwise its seems undocumented :/

@Fractaliste this is no longer public as of 0.45.0. You'll need to bring your own event emitter package.

@tristen Can you give us an example of a package that we'd use in the browser for this?

@chriszrc here's a few! http://microjs.com/#event%20emitter

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iamdenny picture iamdenny  路  3Comments

PBrockmann picture PBrockmann  路  3Comments

aaronlidman picture aaronlidman  路  3Comments

aendrew picture aendrew  路  3Comments

bgentry picture bgentry  路  3Comments