Add ol.Map.isDef

This commit is contained in:
Tom Payne
2012-07-24 20:47:34 +02:00
parent 042434bc54
commit 228547d0fe
5 changed files with 58 additions and 45 deletions

View File

@@ -307,18 +307,19 @@ ol.Map.prototype.getControls = function() {
/**
* @param {ol.Coordinate} pixel Pixel.
* @return {ol.Coordinate} Coordinate.
* @return {ol.Coordinate|undefined} Coordinate.
*/
ol.Map.prototype.getCoordinateFromPixel = function(pixel) {
var center = this.getCenter();
goog.asserts.assert(goog.isDef(center));
var resolution = this.getResolution();
goog.asserts.assert(goog.isDef(resolution));
var size = this.getSize();
goog.asserts.assert(goog.isDef(size));
var x = center.x + resolution * (pixel.x - size.width / 2);
var y = center.y - resolution * (pixel.y - size.height / 2);
return new ol.Coordinate(x, y);
if (this.isDef()) {
var center = this.getCenter();
var resolution = this.getResolution();
var size = this.getSize();
var x = center.x + resolution * (pixel.x - size.width / 2);
var y = center.y - resolution * (pixel.y - size.height / 2);
return new ol.Coordinate(x, y);
} else {
return null;
}
};
@@ -326,17 +327,17 @@ ol.Map.prototype.getCoordinateFromPixel = function(pixel) {
* @return {ol.Extent|undefined} Extent.
*/
ol.Map.prototype.getExtent = function() {
var size = this.getSize();
var center = this.getCenter();
var resolution = this.getResolution();
if (!goog.isDef(size) || !goog.isDef(center) || !goog.isDef(resolution)) {
return undefined;
} else {
if (this.isDef()) {
var size = this.getSize();
var center = this.getCenter();
var resolution = this.getResolution();
var minX = center.x - resolution * size.width / 2;
var minY = center.y - resolution * size.height / 2;
var maxX = center.x + resolution * size.width / 2;
var maxY = center.y + resolution * size.height / 2;
return new ol.Extent(minX, minY, maxX, maxY);
} else {
return undefined;
}
};
@@ -364,18 +365,19 @@ ol.Map.prototype.getLayers = function() {
/**
* @param {ol.Coordinate} coordinate Coordinate.
* @return {ol.Coordinate} Pixel.
* @return {ol.Coordinate|undefined} Pixel.
*/
ol.Map.prototype.getPixelFromCoordinate = function(coordinate) {
var center = this.getCenter();
goog.asserts.assert(goog.isDef(center));
var resolution = this.getResolution();
goog.asserts.assert(goog.isDef(resolution));
var size = this.getSize();
goog.asserts.assert(goog.isDef(size));
var x = (coordinate.x - center.x) / resolution + size.width / 2;
var y = (center.y - coordinate.y) / resolution + size.height / 2;
return new ol.Coordinate(x, y);
if (this.isDef()) {
var center = /** @type {ol.Coordinate} */ this.getCenter();
var resolution = /** @type {number} */ this.getResolution();
var size = /** @type {ol.Size} */ this.getSize();
var x = (coordinate.x - center.x) / resolution + size.width / 2;
var y = (center.y - coordinate.y) / resolution + size.height / 2;
return new ol.Coordinate(x, y);
} else {
return undefined;
}
};
@@ -397,14 +399,17 @@ ol.Map.prototype.getResolution = function() {
/**
* @param {ol.Extent} extent Extent.
* @return {number} Resolution.
* @return {number|undefined} Resolution.
*/
ol.Map.prototype.getResolutionForExtent = function(extent) {
var size = this.getSize();
goog.asserts.assert(goog.isDef(size));
var xResolution = (extent.maxX - extent.minX) / size.width;
var yResolution = (extent.maxY - extent.minY) / size.height;
return Math.max(xResolution, yResolution);
if (goog.isDef(size)) {
var xResolution = (extent.maxX - extent.minX) / size.width;
var yResolution = (extent.maxY - extent.minY) / size.height;
return Math.max(xResolution, yResolution);
} else {
return undefined;
}
};
@@ -619,6 +624,15 @@ ol.Map.prototype.handleViewportResize = function() {
};
/**
* @return {boolean} Is defined.
*/
ol.Map.prototype.isDef = function() {
return goog.isDef(this.getCenter()) && goog.isDef(this.getResolution()) &&
goog.isDef(this.getSize());
};
/**
* @private
*/

View File

@@ -28,13 +28,13 @@ goog.inherits(ol.MapBrowserEvent, ol.MapEvent);
/**
* @private
* @type {ol.Coordinate}
* @type {ol.Coordinate|undefined}
*/
ol.MapBrowserEvent.prototype.coordinate_;
/**
* @return {ol.Coordinate} Coordinate.
* @return {ol.Coordinate|undefined} Coordinate.
*/
ol.MapBrowserEvent.prototype.getCoordinate = function() {
if (goog.isDef(this.coordinate_)) {

View File

@@ -85,9 +85,7 @@ ol.control.Drag.prototype.handleDragEnd = goog.nullFunction;
*/
ol.control.Drag.prototype.handleMapBrowserEvent = function(mapBrowserEvent) {
var map = mapBrowserEvent.map;
var center = map.getCenter();
var resolution = map.getResolution();
if (!goog.isDef(center) || !goog.isDef(resolution)) {
if (!map.isDef()) {
return;
}
var browserEvent = mapBrowserEvent.browserEvent;
@@ -110,8 +108,9 @@ ol.control.Drag.prototype.handleMapBrowserEvent = function(mapBrowserEvent) {
this.startY = browserEvent.clientY;
this.deltaX = 0;
this.deltaY = 0;
this.startCenter = center;
this.startCoordinate = mapBrowserEvent.getCoordinate();
this.startCenter = /** @type {ol.Coordinate} */ map.getCenter();
this.startCoordinate = /** @type {ol.Coordinate} */
mapBrowserEvent.getCoordinate();
if (this.handleDragStart(mapBrowserEvent)) {
this.dragging_ = true;
mapBrowserEvent.preventDefault();

View File

@@ -38,12 +38,11 @@ goog.inherits(ol.dom.TileLayerRenderer, ol.dom.LayerRenderer);
ol.dom.TileLayerRenderer.prototype.render = function() {
var map = this.getMap();
var center = map.getCenter();
var resolution = map.getResolution();
if (!goog.isDef(center) || !goog.isDef(resolution)) {
if (!map.isDef()) {
return;
}
var center = /** @type {ol.Coordinate} */ map.getCenter();
var resolution = /** @type {number} */ map.getResolution();
var tileStore = this.getTileStore_();
var tileGrid = tileStore.getTileGrid();

View File

@@ -479,11 +479,12 @@ ol.webgl.Map.prototype.handleWebGLContextRestored = function() {
*/
ol.webgl.Map.prototype.renderInternal = function() {
var center = this.getCenter();
var resolution = this.getResolution();
if (!goog.isDef(center) || !goog.isDef(resolution)) {
if (!this.isDef()) {
return false;
}
var center = this.getCenter();
var resolution = this.getResolution();
var size = this.getSize();
var animate = goog.base(this, 'renderInternal');