Skip to content

Instantly share code, notes, and snippets.

@warlyware
Last active January 18, 2020 03:03
Show Gist options
  • Select an option

  • Save warlyware/da909c63a5630ed16fa6a3200250326c to your computer and use it in GitHub Desktop.

Select an option

Save warlyware/da909c63a5630ed16fa6a3200250326c to your computer and use it in GitHub Desktop.
Custom HTML Google Map Marker
class HTMLMapMarker extends 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.overlayLayer.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;
}
}
@skiqh
Copy link
Copy Markdown

skiqh commented Jan 18, 2020

line 22 should be panes.overlayMouseTarget.appendChild(this.div) to capture mouse events. (Taken from https://stackoverflow.com/a/7414594/3728569)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment