-
-
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.
This file contains 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
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