Programmatically change OFFSET of MapBox Marker via JavaScript

≯℡__Kan透↙ 提交于 2019-12-12 04:05:36

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/46072617/programmatically-change-offset-of-mapbox-marker-via-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!