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. * @param {ol.Coordinate} pixel Pixel.
* @return {ol.Coordinate} Coordinate. * @return {ol.Coordinate|undefined} Coordinate.
*/ */
ol.Map.prototype.getCoordinateFromPixel = function(pixel) { ol.Map.prototype.getCoordinateFromPixel = function(pixel) {
var center = this.getCenter(); if (this.isDef()) {
goog.asserts.assert(goog.isDef(center)); var center = this.getCenter();
var resolution = this.getResolution(); var resolution = this.getResolution();
goog.asserts.assert(goog.isDef(resolution)); var size = this.getSize();
var size = this.getSize(); var x = center.x + resolution * (pixel.x - size.width / 2);
goog.asserts.assert(goog.isDef(size)); var y = center.y - resolution * (pixel.y - size.height / 2);
var x = center.x + resolution * (pixel.x - size.width / 2); return new ol.Coordinate(x, y);
var y = center.y - resolution * (pixel.y - size.height / 2); } else {
return new ol.Coordinate(x, y); return null;
}
}; };
@@ -326,17 +327,17 @@ ol.Map.prototype.getCoordinateFromPixel = function(pixel) {
* @return {ol.Extent|undefined} Extent. * @return {ol.Extent|undefined} Extent.
*/ */
ol.Map.prototype.getExtent = function() { ol.Map.prototype.getExtent = function() {
var size = this.getSize(); if (this.isDef()) {
var center = this.getCenter(); var size = this.getSize();
var resolution = this.getResolution(); var center = this.getCenter();
if (!goog.isDef(size) || !goog.isDef(center) || !goog.isDef(resolution)) { var resolution = this.getResolution();
return undefined;
} else {
var minX = center.x - resolution * size.width / 2; var minX = center.x - resolution * size.width / 2;
var minY = center.y - resolution * size.height / 2; var minY = center.y - resolution * size.height / 2;
var maxX = center.x + resolution * size.width / 2; var maxX = center.x + resolution * size.width / 2;
var maxY = center.y + resolution * size.height / 2; var maxY = center.y + resolution * size.height / 2;
return new ol.Extent(minX, minY, maxX, maxY); 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. * @param {ol.Coordinate} coordinate Coordinate.
* @return {ol.Coordinate} Pixel. * @return {ol.Coordinate|undefined} Pixel.
*/ */
ol.Map.prototype.getPixelFromCoordinate = function(coordinate) { ol.Map.prototype.getPixelFromCoordinate = function(coordinate) {
var center = this.getCenter(); if (this.isDef()) {
goog.asserts.assert(goog.isDef(center)); var center = /** @type {ol.Coordinate} */ this.getCenter();
var resolution = this.getResolution(); var resolution = /** @type {number} */ this.getResolution();
goog.asserts.assert(goog.isDef(resolution)); var size = /** @type {ol.Size} */ this.getSize();
var size = this.getSize(); var x = (coordinate.x - center.x) / resolution + size.width / 2;
goog.asserts.assert(goog.isDef(size)); var y = (center.y - coordinate.y) / resolution + size.height / 2;
var x = (coordinate.x - center.x) / resolution + size.width / 2; return new ol.Coordinate(x, y);
var y = (center.y - coordinate.y) / resolution + size.height / 2; } else {
return new ol.Coordinate(x, y); return undefined;
}
}; };
@@ -397,14 +399,17 @@ ol.Map.prototype.getResolution = function() {
/** /**
* @param {ol.Extent} extent Extent. * @param {ol.Extent} extent Extent.
* @return {number} Resolution. * @return {number|undefined} Resolution.
*/ */
ol.Map.prototype.getResolutionForExtent = function(extent) { ol.Map.prototype.getResolutionForExtent = function(extent) {
var size = this.getSize(); var size = this.getSize();
goog.asserts.assert(goog.isDef(size)); if (goog.isDef(size)) {
var xResolution = (extent.maxX - extent.minX) / size.width; var xResolution = (extent.maxX - extent.minX) / size.width;
var yResolution = (extent.maxY - extent.minY) / size.height; var yResolution = (extent.maxY - extent.minY) / size.height;
return Math.max(xResolution, yResolution); 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 * @private
*/ */

View File

@@ -28,13 +28,13 @@ goog.inherits(ol.MapBrowserEvent, ol.MapEvent);
/** /**
* @private * @private
* @type {ol.Coordinate} * @type {ol.Coordinate|undefined}
*/ */
ol.MapBrowserEvent.prototype.coordinate_; ol.MapBrowserEvent.prototype.coordinate_;
/** /**
* @return {ol.Coordinate} Coordinate. * @return {ol.Coordinate|undefined} Coordinate.
*/ */
ol.MapBrowserEvent.prototype.getCoordinate = function() { ol.MapBrowserEvent.prototype.getCoordinate = function() {
if (goog.isDef(this.coordinate_)) { 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) { ol.control.Drag.prototype.handleMapBrowserEvent = function(mapBrowserEvent) {
var map = mapBrowserEvent.map; var map = mapBrowserEvent.map;
var center = map.getCenter(); if (!map.isDef()) {
var resolution = map.getResolution();
if (!goog.isDef(center) || !goog.isDef(resolution)) {
return; return;
} }
var browserEvent = mapBrowserEvent.browserEvent; var browserEvent = mapBrowserEvent.browserEvent;
@@ -110,8 +108,9 @@ ol.control.Drag.prototype.handleMapBrowserEvent = function(mapBrowserEvent) {
this.startY = browserEvent.clientY; this.startY = browserEvent.clientY;
this.deltaX = 0; this.deltaX = 0;
this.deltaY = 0; this.deltaY = 0;
this.startCenter = center; this.startCenter = /** @type {ol.Coordinate} */ map.getCenter();
this.startCoordinate = mapBrowserEvent.getCoordinate(); this.startCoordinate = /** @type {ol.Coordinate} */
mapBrowserEvent.getCoordinate();
if (this.handleDragStart(mapBrowserEvent)) { if (this.handleDragStart(mapBrowserEvent)) {
this.dragging_ = true; this.dragging_ = true;
mapBrowserEvent.preventDefault(); mapBrowserEvent.preventDefault();

View File

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

View File

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