Skip to content

Instantly share code, notes, and snippets.

@humaan
Last active July 2, 2019 09:20
Show Gist options
  • Save humaan/a0fcde2813d4722cd7f0 to your computer and use it in GitHub Desktop.
Save humaan/a0fcde2813d4722cd7f0 to your computer and use it in GitHub Desktop.
Extending the Google Maps Javascript API's OverlayView class to create a custom HTML map marker.
function CustomMarker(latlng, map, args) {
this.latlng = latlng;
this.args = args;
this.setMap(map);
}
CustomMarker.prototype = new google.maps.OverlayView();
CustomMarker.prototype.draw = function() {
var self = this;
var div = this.div;
if (!div) {
div = this.div = document.createElement('div');
div.className = 'marker';
div.style.position = 'absolute';
div.style.cursor = 'pointer';
div.style.width = '20px';
div.style.height = '20px';
div.style.background = 'blue';
if (typeof(self.args.marker_id) !== 'undefined') {
div.dataset.marker_id = self.args.marker_id;
}
google.maps.event.addDomListener(div, "click", function(event) {
google.maps.event.trigger(self, "click");
});
var panes = this.getPanes();
panes.overlayImage.appendChild(div);
}
var point = this.getProjection().fromLatLngToDivPixel(this.latlng);
if (point) {
div.style.left = point.x + 'px';
div.style.top = point.y + 'px';
}
};
CustomMarker.prototype.remove = function() {
if (this.div) {
this.div.parentNode.removeChild(this.div);
this.div = null;
}
};
CustomMarker.prototype.getPosition = function() {
return this.latlng;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment