If image size is not provided, determine resolution on load

This commit is contained in:
Tim Schaub
2014-10-05 08:44:32 -06:00
parent 63f126a528
commit ddc3dbaa87
3 changed files with 56 additions and 18 deletions

View File

@@ -1,9 +1,13 @@
goog.provide('ol.source.ImageStatic');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('ol.Image');
goog.require('ol.ImageState');
goog.require('ol.extent');
goog.require('ol.proj');
goog.require('ol.source.Image');
goog.require('ol.source.State');
@@ -21,27 +25,41 @@ ol.source.ImageStatic = function(options) {
var attributions = goog.isDef(options.attributions) ?
options.attributions : null;
var imageExtent = options.imageExtent;
var state, resolution, resolutions;
if (goog.isDef(options.imageSize)) {
state = ol.source.State.READY;
resolution = ol.extent.getHeight(imageExtent) / options.imageSize[1];
resolutions = [resolution];
} else {
state = ol.source.State.LOADING;
}
var crossOrigin = goog.isDef(options.crossOrigin) ?
options.crossOrigin : null;
var imageExtent = options.imageExtent;
var imageSize = options.imageSize;
var imageResolution = (imageExtent[3] - imageExtent[1]) / imageSize[1];
var imageUrl = options.url;
var projection = ol.proj.get(options.projection);
goog.base(this, {
attributions: attributions,
logo: options.logo,
projection: projection,
resolutions: [imageResolution]
projection: ol.proj.get(options.projection),
resolutions: resolutions,
state: state
});
/**
* @private
* @type {ol.Image}
*/
this.image_ = new ol.Image(imageExtent, imageResolution, 1, attributions,
imageUrl, crossOrigin);
this.image_ = new ol.Image(imageExtent, resolution, 1, attributions,
options.url, crossOrigin);
if (state !== ol.source.State.READY) {
goog.events.listen(this.image_, goog.events.EventType.CHANGE,
this.handleImageChange_, false, this);
this.image_.load();
}
};
goog.inherits(ol.source.ImageStatic, ol.source.Image);
@@ -57,3 +75,17 @@ ol.source.ImageStatic.prototype.getImage =
}
return null;
};
/**
* Handle image change events.
* @private
*/
ol.source.ImageStatic.prototype.handleImageChange_ = function() {
var imageState = this.image_.getState();
if (imageState === ol.ImageState.LOADED) {
this.setState(ol.source.State.READY);
} else if (imageState === ol.ImageState.ERROR) {
this.setState(ol.source.State.ERROR);
}
};