Last active
January 18, 2020 03:03
-
-
Save warlyware/da909c63a5630ed16fa6a3200250326c to your computer and use it in GitHub Desktop.
Custom HTML Google Map Marker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line 22 should be
panes.overlayMouseTarget.appendChild(this.div)to capture mouse events. (Taken from https://stackoverflow.com/a/7414594/3728569)