我创建了一个自定义 HTML 地图标记,可用于将 html 添加到标记中。这非常有效。我已经添加了下面的类。
每 10 秒有新数据进来,我想更改标记的 html,以便正确显示数据。
我曾尝试简单地更改标记的 html 属性,如下所示:
marker.html = `<div id="test" class="progress consumptionProduction">
<div class="progress-bar" role="progressbar" style="width: 50%" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100"></div>
<div class="progress-bar bg-success" role="progressbar" style="width: 50%" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100"></div>
</div>`;
这确实改变了marker.html 的值,但地图上的标记没有更新。
marker = createHTMLMapMarker({
latlng: new google.maps.LatLng((53.233071+0.00008),(6.535706-0.00006)),
map: map,
html: `<div id="test" class="progress consumptionProduction">
<div class="progress-bar" role="progressbar" style="width: 15%" aria-valuenow="15" aria-valuemin="0" aria-valuemax="100"></div>
<div class="progress-bar bg-success" role="progressbar" style="width: 85%" aria-valuenow="85" aria-valuemin="0" aria-valuemax="100"></div>
</div>`
})
export const createHTMLMapMarker = ({ OverlayView =
google.maps.OverlayView, ...args }) => {
class HTMLMapMarker extends google.maps.OverlayView {
constructor() {
super();
this.latlng = args.latlng;
this.html = args.html;
this.setMap(args.map);
}
createDiv() {
this.div = document.createElement('div');
this.div.style.position = 'absolute';
if (this.html) {
this.div.innerHTML = this.html;
}
google.maps.event.addDomListener(this.div, 'click', event => {
google.maps.event.trigger(this, 'click');
});
}
appendDivToOverlay() {
const panes = this.getPanes();
panes.overlayImage.appendChild(this.div);
}
positionDiv() {
const point = this.getProjection().fromLatLngToDivPixel(this.latlng);
if (point) {
this.div.style.left = `${point.x}px`;
this.div.style.top = `${point.y}px`;
}
}
Helenr
沧海一幻觉
相关分类