Skip to content

Instantly share code, notes, and snippets.

@glenrobertson
Created August 19, 2012 23:06
Show Gist options
  • Select an option

  • Save glenrobertson/3398417 to your computer and use it in GitHub Desktop.

Select an option

Save glenrobertson/3398417 to your computer and use it in GitHub Desktop.
leaflet icon hover class
L.Icon.Hover = L.Icon.extend({
options: {
iconSize: new L.Point(25, 41),
iconAnchor: new L.Point(13, 41),
popupAnchor: new L.Point(1, -34),
shadowSize: new L.Point(41, 41)
},
initialize: function (options) {
L.Util.setOptions(this, options);
},
_createIcon: function (name) {
var img = L.Icon.prototype._createIcon.call(this, name);
var hoverUrl = this._getIconHoverUrl();
var standardUrl = this._getIconUrl(name);
if (name === 'icon') {
img.onmouseover = function() {
this.src = hoverUrl;
};
img.onmouseout = function() {
this.src = standardUrl;
};
}
return img;
},
_getIconUrl: function (name) {
var key = name + 'Url';
if (this.options[key]) {
return this.options[key];
}
var path = L.Icon.Default.imagePath;
if (!path) {
throw new Error("Couldn't autodetect L.Icon.Default.imagePath, set it manually.");
}
return path + '/marker-' + name + '.png';
},
_getIconHoverUrl: function () {
var key = 'iconHoverUrl';
if (this.options[key]) {
return this.options[key];
}
var path = L.Icon.Default.imagePath;
if (!path) {
throw new Error("Couldn't autodetect L.Icon.Default.imagePath, set it manually.");
}
return path + '/marker-' + name + '-hover.png';
}
});
L.Icon.Default.imagePath = (function () {
var scripts = document.getElementsByTagName('script'),
leafletRe = /\/?leaflet[\-\._]?([\w\-\._]*)\.js\??/;
var i, len, src, matches;
for (i = 0, len = scripts.length; i < len; i++) {
src = scripts[i].src;
matches = src.match(leafletRe);
if (matches) {
return src.split(leafletRe)[0] + '/images';
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment