diff --git a/lib/OpenLayers/Icon.js b/lib/OpenLayers/Icon.js index 7cb7b143fa..fe0a559db6 100644 --- a/lib/OpenLayers/Icon.js +++ b/lib/OpenLayers/Icon.js @@ -15,28 +15,30 @@ OpenLayers.Icon.prototype = { * @type OpenLayers.Pixel */ offset: null, + /** Function to calculate the offset (based on the size) + * @type OpenLayers.Pixel */ + calculateOffset: null, + /** @type DOMElement */ - image: null, + imageDiv: null, + + /** @type OpenLayers.Pixel */ + px: null, /** * @constructor * * @param {String} url * @param {OpenLayers.Size} size - * @param {OpenLayers.Pixel} offset + * @param {Function} calculateOffset */ - initialize: function(url, size, offset) { - this.size = size; + initialize: function(url, size, offset, calculateOffset) { this.url = url; - this.offset = (offset) ? offset - : new OpenLayers.Pixel(0,0); + this.size = size; + this.offset = (offset) ? offset : new OpenLayers.Pixel(0,0); + this.calculateOffset = calculateOffset; - this.image = OpenLayers.Util.createAlphaImageDiv(null, - null, - this.size, - this.url, - "absolute" - ); + this.imageDiv = OpenLayers.Util.createAlphaImageDiv(); }, /** @@ -47,6 +49,16 @@ OpenLayers.Icon.prototype = { return new OpenLayers.Icon(this.size, this.url, this.offset); }, + /** + * @param {OpenLayers.Size} size + */ + setSize: function(size) { + if (size != null) { + this.size = size; + } + this.draw(); + }, + /** * @param {OpenLayers.Pixel} px * @@ -54,18 +66,31 @@ OpenLayers.Icon.prototype = { * @type DOMElement */ draw: function(px) { + OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv, + null, + null, + this.size, + this.url, + "absolute"); this.moveTo(px); - return this.image; + return this.imageDiv; }, /** * @param {OpenLayers.Pixel} px */ moveTo: function (px) { - if ((px != null) && (this.image != null)) { - offsetPx = px.offset(this.offset); - this.image.style.left = offsetPx.x + "px"; - this.image.style.top = offsetPx.y + "px" + //if no px passed in, use stored location + if (px != null) { + this.px = px; + } + + if ((this.px != null) && (this.imageDiv != null)) { + if (this.calculateOffset) { + this.offset = this.calculateOffset(this.size); + } + var offsetPx = this.px.offset(this.offset); + OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv, null, offsetPx); } }, diff --git a/lib/OpenLayers/Marker.js b/lib/OpenLayers/Marker.js index cfc0b9237d..509c7a5fff 100644 --- a/lib/OpenLayers/Marker.js +++ b/lib/OpenLayers/Marker.js @@ -27,7 +27,7 @@ OpenLayers.Marker.prototype = { this.lonlat = lonlat; this.icon = (icon) ? icon : OpenLayers.Marker.defaultIcon(); - this.events = new OpenLayers.Events(this, this.icon.image, null); + this.events = new OpenLayers.Events(this, this.icon.imageDiv, null); }, /** @@ -49,7 +49,7 @@ OpenLayers.Marker.prototype = { this.icon.moveTo(px); } }, - + /** @final @type String */ CLASS_NAME: "OpenLayers.Marker" }; @@ -62,7 +62,9 @@ OpenLayers.Marker.prototype = { OpenLayers.Marker.defaultIcon = function() { var url = OpenLayers.Util.getImagesLocation() + "marker.png"; var size = new OpenLayers.Size(21, 25); - var offset = new OpenLayers.Pixel(-(size.w/2), -size.h); + var offset = function(size) { + return OpenLayers.Pixel(-(size.w/2), -size.h); + }; return new OpenLayers.Icon(url, size, offset); };