[英]How to dynamically change html of custom google maps marker?
I have created a custom HTML Map Marker which I can use to add html to a marker. 我创建了一个自定义HTML地图标记,我可以用它来将html添加到标记中。 This works perfectly well. 这非常有效。 I have added the class below. 我在下面添加了这个课程。
Every 10 seconds new data comes in and I would like to change the html of the marker such that the data is shown appropriately. 每隔10秒就有新数据进入,我想更改标记的html,以便正确显示数据。
I have tried simply changing the html attribute of the marker like this: 我试过简单地改变标记的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>`;
Which does indeed change the value of marker.html but the marker is not updated on the map. 这确实改变了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`;
}
}
draw() {
if (!this.div) {
this.createDiv();
this.appendDivToOverlay();
}
this.positionDiv();
}
remove() {
if (this.div) {
this.div.parentNode.removeChild(this.div);
this.div = null;
}
}
getPosition() {
return this.latlng;
}
getDraggable() {
return false;
}
}
return new HTMLMapMarker();
};
As the marker is an element in the DOM, you can update its contents via .innerHTML = ...
: 由于标记是DOM中的元素,您可以通过.innerHTML = ...
更新其内容:
document.getElementById("test").innerHTML = "This is my new content";
In your case I would add a new method update(text)
to your HTMLMapMarker class and call it whenever a marker's contents need to be changed: 在您的情况下,我会向您的HTMLMapMarker类添加一个新的方法update(text)
,并在需要更改标记的内容时调用它:
class HTMLMapMarker extends google.maps.OverlayView {
...
update(newText) {
if (this.div) {
this.div.innerHTML = newText;
} else {
console.warn("createDiv() has not been called on this point", this);
}
}
...
}
Call the update() routine: 调用update()例程:
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>`
})
marker.update("This should be my new content");
marker.html
is just a property of the HTMLMapMarker
class that is used to actually create a HTML object in createDiv
. marker.html
只是HTMLMapMarker
类的一个属性,用于在createDiv
实际创建HTML对象。 Just updating this property doesn't trigger any action, and createDiv
was already called before. 只更新此属性不会触发任何操作,之前已调用createDiv
。 What you need to do is ie add a updateDiv(html)
method such: 你需要做的是添加一个updateDiv(html)
方法,如:
updateDiv(html) {
this.html=html;
if(!this.div){
this.createDiv();
} else {
this.div.innerHTML = this.html;
}
}
Then call this method to update the marker (with HTML). 然后调用此方法更新标记(使用HTML)。 Alternately, you can use the .div
property of the HTMLMapMarker
object to manipulate the marker object directly. 或者,您可以使用HTMLMapMarker
对象的.div
属性直接操作标记对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.