HomerPlata
HomerPlata

Reputation: 1797

Programmatically change OFFSET of MapBox Marker via JavaScript

Following on from this question I asked yesterday...

I am adding MapBox Markers to an array like so:

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 now need to be able to change the offset of deviceMarker programmatically in javascript, to ensure that the image is still centred as the div resizes with zoom.

Can it be done, and if so, how?

Upvotes: 0

Views: 469

Answers (1)

mollymerp
mollymerp

Reputation: 1602

There is no way to do this with the current API. I would recommend you just make a new marker with the new offset and the existing element (i.e. new mapboxgl.Marker(oldMarker.getElement(), ...) and then remove the old marker)

Upvotes: 1

Related Questions