通过JavaScript以编程方式更改MapBox Marker的OFFSET [英] Programmatically change OFFSET of MapBox Marker via JavaScript

查看:436
本文介绍了通过JavaScript以编程方式更改MapBox Marker的OFFSET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接着是此问题我昨天问的问题...

我正在将MapBox标记添加到数组中,如下所示:

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

在代码的其他地方,我通过以下方式提取标记:

var deviceMarker = deviceMarkers[index];

我现在需要能够在javascript中以编程方式更改 deviceMarker 的偏移量,以确保随着div使用缩放调整大小时图像仍居中.

可以做到吗?如果可以,怎么办?

解决方案

当前的API无法执行此操作.我建议您仅使用新的偏移量和现有元素(即new mapboxgl.Marker(oldMarker.getElement(), ...),然后删除旧的标记)制作一个新的标记

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?

解决方案

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)

这篇关于通过JavaScript以编程方式更改MapBox Marker的OFFSET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆