This commit is contained in:
Petr Pridal
2012-06-20 17:47:28 +02:00
18 changed files with 789 additions and 167 deletions

View File

@@ -33,9 +33,9 @@ ol.Loc = function(x, y, opt_z, opt_projection) {
/**
* @private
* @type {ol.Projection|undefined}
* @type {ol.Projection}
*/
this.projection_ = opt_projection;
this.projection_ = goog.isDef(opt_projection) ? opt_projection : null;
};
@@ -73,7 +73,7 @@ ol.Loc.prototype.getZ = function() {
/**
* @param {ol.Projection|undefined} projection Projection.
* @param {ol.Projection} projection Projection.
*/
ol.Loc.prototype.setProjection = function(projection) {
this.projection_ = projection;
@@ -118,7 +118,7 @@ ol.Loc.prototype.transform = function(proj) {
if (goog.isString(proj)) {
proj = new ol.Projection(proj);
}
return this.transform_(proj);
return this._transform(proj);
};
/**
@@ -127,8 +127,8 @@ ol.Loc.prototype.transform = function(proj) {
* @param {!ol.Projection} proj The destination projection.
* @returns {!ol.Loc}
*/
ol.Loc.prototype.transform_ = function(proj) {
var point = {x: this.x_, y: this.y_};
ol.Loc.prototype._transform = function(proj) {
var point = {'x': this.x_, 'y': this.y_};
var sourceProj = this.projection_;
if (!goog.isDefAndNotNull(sourceProj)) {
throw new Error("Cannot transform a location without a source projection.");

View File

@@ -42,16 +42,28 @@ ol.Map = function() {
/**
* @private
* @type {Array|undefined}
* @type {Array}
*/
this.resolutions_ = null;
/**
* @private
* @type {Array|undefined}
* @type {Array}
*/
this.layers_ = null;
/**
* @private
* @type {ol.UnreferencedBounds}
*/
this.maxExtent_ = null;
/**
* @private
* @type {number|undefined}
*/
this.maxRes_ = undefined;
};
/**
@@ -64,12 +76,23 @@ ol.Map.prototype.DEFAULT_PROJECTION = "EPSG:3857";
@type {string}
*/
ol.Map.prototype.DEFAULT_USER_PROJECTION = "EPSG:4326";
/**
@const
@type {number}
*/
ol.Map.ZOOM_FACTOR = 2;
/**
@const
@type {number}
*/
ol.Map.DEFAULT_TILE_SIZE = 256;
/**
* @return {ol.Loc} Location.
*/
ol.Map.prototype.getCenter = function() {
var proj = this.getUserProjection();
this.center_ = this.center_.transform(proj);
return this.center_;
};
@@ -145,11 +168,47 @@ ol.Map.prototype.getMaxExtent = function() {
};
/**
* @return {number} the max resolution for the map
*/
ol.Map.prototype.getMaxRes = function() {
if (goog.isDefAndNotNull(this.maxRes_)) {
return this.maxRes_;
} else {
var extent = this.getMaxExtent();
var dim = Math.max(
(extent.getMaxX()-extent.getMinX()),
(extent.getMaxY()-extent.getMinY())
);
return dim/ol.Map.DEFAULT_TILE_SIZE;
}
};
/**
* @param {number} zoom the zoom level being requested
* @return {number} the resolution for the map at the given zoom level
*/
ol.Map.prototype.getResolutionForZoom = function(zoom) {
if (goog.isDefAndNotNull(this.resolutions_)) {
return this.resolutions_[zoom];
} else {
var maxRes = this.getMaxRes();
return maxRes/Math.pow(ol.Map.ZOOM_FACTOR, zoom);
}
};
/**
* @param {ol.Loc} center Center.
*/
ol.Map.prototype.setCenter = function(center) {
this.center_ = center;
var proj = center.getProjection();
if (goog.isNull(proj)) {
proj = this.getUserProjection();
center.setProjection(proj);
}
this.center_ = center.transform(this.getProjection());
};
@@ -199,12 +258,19 @@ ol.Map.prototype.setLayers = function(layers) {
};
/**
* @param {ol.Bounds} extent the maxExtent for the map
* @param {ol.UnreferencedBounds} extent the maxExtent for the map
*/
ol.Map.prototype.setMaxExtent = function(extent) {
this.maxExtent_ = extent;
};
/**
* @param {number} res the max resolution for the map
*/
ol.Map.prototype.setMaxRes = function(res) {
this.maxRes_ = res;
};
/**
*/
ol.Map.prototype.destroy = function() {

View File

@@ -22,15 +22,15 @@ ol.Projection = function(code) {
/**
* @private
* @type {!Object|undefined}
* @type {Object}
*/
this.proj_ = undefined;
this.proj_ = null;
/**
* @private
* @type {!ol.UnreferencedBounds|undefined}
* @type {ol.UnreferencedBounds}
*/
this.extent_ = undefined;
this.extent_ = null;
};
@@ -66,9 +66,18 @@ ol.Projection.prototype.setUnits = function(units) {
/**
* Get the validity extent of the coordinate reference system.
*
* @return {!ol.UnreferencedBounds|undefined} The valididty extent.
* @return {ol.UnreferencedBounds} The valididty extent.
*/
ol.Projection.prototype.getExtent = function() {
if (goog.isNull(this.extent_)) {
var defs = ol.Projection['defaults'][this.code_];
if (goog.isDef(defs)) {
var ext = defs['maxExtent'];
if (goog.isDef(ext)) {
this.setExtent(new ol.UnreferencedBounds(ext[0],ext[1],ext[2],ext[3]));
}
}
}
return this.extent_;
};

View File

@@ -11,21 +11,21 @@ goog.require('goog.style');
/**
* Determine whether event was caused by a single touch
*
* @param {Event} evt
* @param {!Event} evt
* @return {boolean}
*/
ol.event.isSingleTouch = function(evt) {
return evt.touches && evt.touches.length == 1;
return !!(evt.touches && evt.touches.length == 1);
};
/**
* Determine whether event was caused by a multi touch
*
* @param {Event} evt
* @param {!Event} evt
* @return {boolean}
*/
ol.event.isMultiTouch = function(evt) {
return evt.touches && evt.touches.length > 1;
return !!(evt.touches && evt.touches.length > 1);
};
@@ -83,7 +83,7 @@ ol.event.Events.prototype.getObject = function() {
* @param {boolean} includeXY
*/
ol.event.Events.prototype.setIncludeXY = function(includeXY) {
this._includeXY = includeXY;
this.includeXY_ = includeXY;
};
/**
@@ -215,7 +215,7 @@ ol.event.Events.prototype.un = function(object) {
};
/**
* Unregister a listener for an egent
* Unregister a listener for an event
*
* @param {string} type Name of the event to unregister
* @param {Function} listener The callback function.
@@ -242,6 +242,9 @@ ol.event.Events.prototype.triggerEvent = function(type, evt) {
var returnValue,
listeners = goog.events.getListeners(this, type, true)
.concat(goog.events.getListeners(this, type, false));
if (arguments.length === 1) {
evt = {type: type};
}
for (var i=0, ii=listeners.length; i<ii; ++i) {
returnValue = listeners[i].handleEvent(evt);
if (returnValue === false) {
@@ -283,7 +286,7 @@ ol.event.Events.prototype.handleBrowserEvent = function(evt) {
evt.clientX = x / num;
evt.clientY = y / num;
}
if (this.includeXY) {
if (this.includeXY_) {
var element = /** @type {!Element} */ this.element_;
evt.xy = goog.style.getRelativePosition(evt, element);
}
@@ -294,6 +297,7 @@ ol.event.Events.prototype.handleBrowserEvent = function(evt) {
* Destroy this Events instance.
*/
ol.event.Events.prototype.destroy = function() {
this.setElement();
for (var p in this) {
delete this[p];
}

View File

@@ -106,3 +106,37 @@ ol.geom.Point.prototype.setZ = function(z) {
this.z_ = z;
};
/**
* Transform this point to another coordinate reference system. This
* requires that this point has a projection set already (if not, an error
* will be thrown). Returns a new point object and does not modify this
* point.
*
* @param {string|!ol.Projection} proj The destination projection. Can be
* supplied as a projection instance of a string identifier.
* @returns {!ol.geom.Point} A new location.
*/
ol.geom.Point.prototype.transform = function(proj) {
if (goog.isString(proj)) {
proj = new ol.Projection(proj);
}
return this.transform_(proj);
};
/**
* Transform this point to a new location given a projection object.
*
* @param {!ol.Projection} proj The destination projection.
* @returns {!ol.geom.Point}
* @private
*/
ol.geom.Point.prototype.transform_ = function(proj) {
var point = {x: this.x_, y: this.y_};
var sourceProj = this.projection_;
if (!goog.isDefAndNotNull(sourceProj)) {
throw new Error("Cannot transform a point without a source projection.");
}
ol.Projection.transform(point, sourceProj, proj);
return new ol.geom.Point(point.x, point.y, this.z_, proj);
};

View File

@@ -8,7 +8,7 @@ goog.require('ol.TileSet');
* Class for XYZ layers.
* @constructor
* @param {string} url URL template. E.g.
* http://a.tile.openstreetmap.org/${z}/${x}/${y}.png.
* http://a.tile.openstreetmap.org/{z}/{x}/{y}.png.
*/
ol.layer.XYZ = function(url) {
@@ -108,9 +108,10 @@ ol.layer.XYZ.prototype.setResolutions = function(resolutions) {
*/
ol.layer.XYZ.prototype.getData = function(bounds, resolution) {
var me = this,
zoom = me.zoomForResolution_(resolution);
zoom = me.zoomForResolution(resolution);
resolution = me.resolutions_[zoom];
// define some values used for the actual tiling
var boundsMinX = bounds.getMinX(),
boundsMaxX = bounds.getMaxX(),
boundsMinY = bounds.getMinY(),
@@ -130,26 +131,27 @@ ol.layer.XYZ.prototype.getData = function(bounds, resolution) {
offsetY = Math.floor(
(tileOriginY - boundsMaxY) / tileHeightGeo),
gridWidth = Math.ceil(
(boundsMaxX - boundsMinX) / tileWidthGeo),
gridHeight = Math.ceil(
(boundsMaxY - boundsMinY) / tileHeightGeo);
gridLeft = tileOriginX + tileWidthGeo * offsetX,
gridTop = tileOriginY - tileHeightGeo * offsetY;
// now tile
var tiles = [],
tile,
url,
i, ii,
j, jj;
for (i=0, ii=gridWidth; i<ii; i++) {
tiles[i] = [];
for (j=0, jj=gridHeight; j<jj; j++) {
url = me.url_.replace('{x}', offsetX + i + '')
.replace('{y}', offsetY + j + '')
x = 0,
y = 0;
while (gridTop - (y * tileHeightGeo) > boundsMinY) {
tiles[y] = [];
while (gridLeft + (x * tileWidthGeo) < boundsMaxX) {
url = me.url_.replace('{x}', offsetX + x + '')
.replace('{y}', offsetY + y + '')
.replace('{z}', zoom);
tile = new ol.Tile(url);
tiles[i][j] = tile;
tiles[y][x] = tile;
x++;
}
y++;
x = 0;
}
return new ol.TileSet(tiles, tileWidth, tileHeight, resolution);
@@ -157,10 +159,9 @@ ol.layer.XYZ.prototype.getData = function(bounds, resolution) {
/**
* Get the zoom level (z) for the given resolution.
* @private
* @param {number} resolution
*/
ol.layer.XYZ.prototype.zoomForResolution_ = function(resolution) {
ol.layer.XYZ.prototype.zoomForResolution = function(resolution) {
var delta = Number.POSITIVE_INFINITY,
currentDelta,
resolutions = this.resolutions_;

View File

@@ -1,31 +0,0 @@
goog.provide('ol.mixins.coordinate');
goog.require('goog.object');
goog.object.extend(ol.mixins.coordinate, {
getX : function() {
return this.x_;
},
getY : function() {
return this.y_;
},
getZ : function() {
return this.z_;
},
setX : function(x) {
this.x_ = x;
return this;
},
setY : function(y) {
this.y_ = y;
return this;
},
setZ: function(z) {
this.z_ = z;
return this;
}
});