[GIS] MapBox get div element of Marker

javascriptmapboxmapbox-gl-js

Apologies for cross-posting – I have also asked this on StackOverflow…

I am initially pushing MapBox Marker objects to an array via:

var el = document.createElement('div' + index);
el.className = 'marker';
deviceMarkers.push(new mapboxgl.Marker(el, { offset: [-50 / 2, -50 / 2] }).setLngLat([device.lat, device.lon]).addTo(map));

Elsewhere in the code, I extract the marker via:

var deviceMarker = deviceMarkers[index];

I would like to be able to get 'el' from deviceMarker object, in order to alter the icon size, orientation etc. dynamically. How can I extract 'el' as the original div?

Best Answer

You're saving the mapboxgl.Marker object so you can latter access this via the getElement method, eg:

deviceMarker.getElement()

See https://www.mapbox.com/mapbox-gl-js/api/#marker#getelement