From 9365c227ab58559388b4af80220eba447a11cc3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Tue, 26 Nov 2013 16:19:33 +0100 Subject: [PATCH] Make icon size optional --- src/ol/icon.js | 17 ++++++++++++++--- src/ol/style/imagestyle.js | 6 ++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/ol/icon.js b/src/ol/icon.js index 0f62119af7..6f2cdd9f93 100644 --- a/src/ol/icon.js +++ b/src/ol/icon.js @@ -5,12 +5,23 @@ goog.require('ol.style.Image'); /** * @param {string} src Image source URI. - * @param {ol.Size} size Image size. + * @param {ol.Size=} opt_size Image size. * @return {ol.style.Image} Image. */ -ol.icon.renderIcon = function(src, size) { +ol.icon.renderIcon = function(src, opt_size) { + + /** + * @type {ol.Size} + */ + var size = goog.isDef(opt_size) ? opt_size : null; + + /** + * @type {ol.Pixel} + */ + var anchor = !goog.isNull(size) ? [size[0] / 2, size[1] / 2] : null; + return new ol.style.Image({ - anchor: [size[0] / 2, size[1] / 2], + anchor: anchor, size: size, src: src, rotation: 0, diff --git a/src/ol/style/imagestyle.js b/src/ol/style/imagestyle.js index 3021c960d4..2320f78ed0 100644 --- a/src/ol/style/imagestyle.js +++ b/src/ol/style/imagestyle.js @@ -114,6 +114,12 @@ ol.style.Image.prototype.handleImageError_ = function() { */ ol.style.Image.prototype.handleImageLoad_ = function() { this.imageState = ol.style.ImageState.LOADED; + if (goog.isNull(this.size)) { + this.size = [this.image.width, this.image.height]; + } + if (goog.isNull(this.anchor)) { + this.anchor = [this.size[0] / 2, this.size[1] / 2]; + } this.unlistenImage_(); this.dispatchChangeEvent_(); };