implement getMaxRes, getMaxExtent, getResForZoom
This commit is contained in:
@@ -31,6 +31,8 @@ ol.map = function(opt_arg){
|
|||||||
var userProjection;
|
var userProjection;
|
||||||
/** @type {ol.Bounds|undefined} */
|
/** @type {ol.Bounds|undefined} */
|
||||||
var maxExtent;
|
var maxExtent;
|
||||||
|
/** @type {ol.Bounds|undefined} */
|
||||||
|
var maxRes;
|
||||||
/** @type {Array.<number>|undefined} */
|
/** @type {Array.<number>|undefined} */
|
||||||
var resolutions;
|
var resolutions;
|
||||||
/** @type {Array|undefined} */
|
/** @type {Array|undefined} */
|
||||||
@@ -47,6 +49,7 @@ ol.map = function(opt_arg){
|
|||||||
projection = opt_arg['projection'];
|
projection = opt_arg['projection'];
|
||||||
userProjection = opt_arg['userProjection'];
|
userProjection = opt_arg['userProjection'];
|
||||||
maxExtent = opt_arg['maxExtent'];
|
maxExtent = opt_arg['maxExtent'];
|
||||||
|
maxRes = opt_arg['maxRes'];
|
||||||
resolutions = opt_arg['resolutions'];
|
resolutions = opt_arg['resolutions'];
|
||||||
layers = opt_arg['layers'];
|
layers = opt_arg['layers'];
|
||||||
}
|
}
|
||||||
@@ -74,6 +77,9 @@ ol.map = function(opt_arg){
|
|||||||
if (goog.isDef(maxExtent)) {
|
if (goog.isDef(maxExtent)) {
|
||||||
map.setMaxExtent(ol.bounds(maxExtent));
|
map.setMaxExtent(ol.bounds(maxExtent));
|
||||||
}
|
}
|
||||||
|
if (goog.isDef(maxRes)) {
|
||||||
|
map.setMaxRes(maxRes);
|
||||||
|
}
|
||||||
if (goog.isDef(resolutions)) {
|
if (goog.isDef(resolutions)) {
|
||||||
map.setResolutions(resolutions);
|
map.setResolutions(resolutions);
|
||||||
}
|
}
|
||||||
@@ -187,3 +193,24 @@ ol.Map.prototype.maxExtent = function(opt_arg) {
|
|||||||
return this.getMaxExtent();
|
return this.getMaxExtent();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number=} opt_arg
|
||||||
|
* @returns {ol.Map|number|undefined} Map maximum resolution
|
||||||
|
*/
|
||||||
|
ol.Map.prototype.maxRes = function(opt_arg) {
|
||||||
|
if (arguments.length == 1 && goog.isDef(opt_arg)) {
|
||||||
|
this.setMaxRes(opt_arg);
|
||||||
|
return this;
|
||||||
|
} else {
|
||||||
|
return this.getMaxRes();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} arg
|
||||||
|
* @returns {number} resolution for a given zoom level
|
||||||
|
*/
|
||||||
|
ol.Map.prototype.getResForZoom = function(arg) {
|
||||||
|
return this.getResolutionForZoom(arg);
|
||||||
|
};
|
||||||
|
|||||||
+12
-1
@@ -21,6 +21,9 @@ ol.projection = function(opt_arg){
|
|||||||
/** @type {undefined|number} */
|
/** @type {undefined|number} */
|
||||||
var units;
|
var units;
|
||||||
|
|
||||||
|
/** @type {undefined|Array|ol.UnreferencedBounds} */
|
||||||
|
var extent;
|
||||||
|
|
||||||
if (arguments.length == 1 && goog.isDefAndNotNull(opt_arg)) {
|
if (arguments.length == 1 && goog.isDefAndNotNull(opt_arg)) {
|
||||||
if (opt_arg instanceof ol.Projection) {
|
if (opt_arg instanceof ol.Projection) {
|
||||||
return opt_arg;
|
return opt_arg;
|
||||||
@@ -35,13 +38,21 @@ ol.projection = function(opt_arg){
|
|||||||
throw new Error('Projection requires a string code.');
|
throw new Error('Projection requires a string code.');
|
||||||
}
|
}
|
||||||
units = opt_arg['units'];
|
units = opt_arg['units'];
|
||||||
|
extent = opt_arg['maxExtent'];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new Error('ol.projection');
|
throw new Error('ol.projection');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var proj = new ol.Projection(code);
|
var proj = new ol.Projection(code);
|
||||||
proj.setUnits(units);
|
if (goog.isDef(units)) {
|
||||||
|
proj.setUnits(units);
|
||||||
|
}
|
||||||
|
if (goog.isDef(extent)) {
|
||||||
|
proj.setExtent(
|
||||||
|
new ol.UnreferencedBounds(extent[0],extent[1],extent[2],extent[3])
|
||||||
|
);
|
||||||
|
}
|
||||||
return proj;
|
return proj;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+62
-1
@@ -52,6 +52,18 @@ ol.Map = function() {
|
|||||||
*/
|
*/
|
||||||
this.layers_ = null;
|
this.layers_ = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @type {ol.UnreferencedBounds|undefined}
|
||||||
|
*/
|
||||||
|
this.maxExtent_ = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @type {number|undefined}
|
||||||
|
*/
|
||||||
|
this.maxRes_ = null;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,7 +76,16 @@ ol.Map.prototype.DEFAULT_PROJECTION = "EPSG:3857";
|
|||||||
@type {string}
|
@type {string}
|
||||||
*/
|
*/
|
||||||
ol.Map.prototype.DEFAULT_USER_PROJECTION = "EPSG:4326";
|
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.
|
* @return {ol.Loc} Location.
|
||||||
@@ -145,6 +166,39 @@ 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();
|
||||||
|
if (goog.isDefAndNotNull(extent)) {
|
||||||
|
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.
|
* @param {ol.Loc} center Center.
|
||||||
*/
|
*/
|
||||||
@@ -205,6 +259,13 @@ ol.Map.prototype.setMaxExtent = function(extent) {
|
|||||||
this.maxExtent_ = 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() {
|
ol.Map.prototype.destroy = function() {
|
||||||
|
|||||||
+14
-5
@@ -21,15 +21,15 @@ ol.Projection = function(code) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @type {!Object|undefined}
|
* @type {Object}
|
||||||
*/
|
*/
|
||||||
this.proj_ = undefined;
|
this.proj_ = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @type {!ol.UnreferencedBounds|undefined}
|
* @type {ol.UnreferencedBounds}
|
||||||
*/
|
*/
|
||||||
this.extent_ = undefined;
|
this.extent_ = null;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -65,9 +65,18 @@ ol.Projection.prototype.setUnits = function(units) {
|
|||||||
/**
|
/**
|
||||||
* Get the validity extent of the coordinate reference system.
|
* 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() {
|
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_;
|
return this.extent_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+35
-22
@@ -164,7 +164,7 @@ describe("ol.map", function() {
|
|||||||
|
|
||||||
map.destroy();
|
map.destroy();
|
||||||
|
|
||||||
expect(goog.isDef(map.layers)).toBe(false);
|
expect(goog.isDef(map.getLayers())).toBe(false);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -199,11 +199,11 @@ describe("ol.map", function() {
|
|||||||
var map = ol.map();
|
var map = ol.map();
|
||||||
|
|
||||||
var extent = map.maxExtent();
|
var extent = map.maxExtent();
|
||||||
expect(extent).toBeA(ol.Bounds);
|
expect(extent).toBeA(ol.UnreferencedBounds);
|
||||||
expect(extent.minX()).toBe(-20037508.34);
|
expect(extent.getMinX()).toBe(-20037508.34);
|
||||||
expect(extent.maxX()).toBe(-20037508.34);
|
expect(extent.getMaxX()).toBe(20037508.34);
|
||||||
expect(extent.minY()).toBe(20037508.34);
|
expect(extent.getMinY()).toBe(-20037508.34);
|
||||||
expect(extent.maxY()).toBe(20037508.34);
|
expect(extent.getMaxY()).toBe(20037508.34);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -212,11 +212,11 @@ describe("ol.map", function() {
|
|||||||
map.maxExtent([-5,-4,7,9]);
|
map.maxExtent([-5,-4,7,9]);
|
||||||
|
|
||||||
var extent = map.maxExtent();
|
var extent = map.maxExtent();
|
||||||
expect(extent).toBeA(ol.Bounds);
|
expect(extent).toBeA(ol.UnreferencedBounds);
|
||||||
expect(extent.minX()).toBe(-5);
|
expect(extent.getMinX()).toBe(-5);
|
||||||
expect(extent.maxX()).toBe(-4);
|
expect(extent.getMaxX()).toBe(7);
|
||||||
expect(extent.minY()).toBe(7);
|
expect(extent.getMinY()).toBe(-4);
|
||||||
expect(extent.maxY()).toBe(9);
|
expect(extent.getMaxY()).toBe(9);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -225,11 +225,11 @@ describe("ol.map", function() {
|
|||||||
map.projection("CRS:84");
|
map.projection("CRS:84");
|
||||||
|
|
||||||
var extent = map.maxExtent();
|
var extent = map.maxExtent();
|
||||||
expect(extent).toBeA(ol.Bounds);
|
expect(extent).toBeA(ol.UnreferencedBounds);
|
||||||
expect(extent.minX()).toBe(-180);
|
expect(extent.getMinX()).toBe(-180);
|
||||||
expect(extent.maxX()).toBe(-90);
|
expect(extent.getMaxX()).toBe(180);
|
||||||
expect(extent.minY()).toBe(180);
|
expect(extent.getMinY()).toBe(-90);
|
||||||
expect(extent.maxY()).toBe(90);
|
expect(extent.getMaxY()).toBe(90);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -244,7 +244,7 @@ describe("ol.map", function() {
|
|||||||
var map = ol.map();
|
var map = ol.map();
|
||||||
|
|
||||||
var res = map.maxRes();
|
var res = map.maxRes();
|
||||||
expect(res.toFixed(5)).toBe(1.40625);
|
expect(res.toFixed(5)).toBe("156543.03391");
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -261,22 +261,35 @@ describe("ol.map", function() {
|
|||||||
it("getMaxRes returns correct for custom maxExtent", function() {
|
it("getMaxRes returns correct for custom maxExtent", function() {
|
||||||
var map = ol.map({
|
var map = ol.map({
|
||||||
projection: ol.projection({
|
projection: ol.projection({
|
||||||
|
code: 'foo',
|
||||||
maxExtent: [0,0,90,90]
|
maxExtent: [0,0,90,90]
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
var res = map.maxRes();
|
var res = map.maxRes();
|
||||||
expect(res.toFixed(7)).toBe(0.3515625);
|
expect(res.toFixed(7)).toBe("0.3515625");
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("getResForZoom returns correct defaults", function() {
|
it("returns correct getResForZoom for default map", function() {
|
||||||
var map = ol.map();
|
var map = ol.map();
|
||||||
|
|
||||||
res = map.getResForZoom(0);
|
var res = map.getResForZoom(0);
|
||||||
expect(res.toFixed(5)).toBe(1.40625);
|
expect(res.toFixed(5)).toBe("156543.03391");
|
||||||
res = map.getResForZoom(5);
|
res = map.getResForZoom(5);
|
||||||
expect(res.toFixed(10)).toBe(0.0439453125);
|
expect(res.toFixed(5)).toBe("4891.96981");
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns correct getResForZoom when resolution array is set",function() {
|
||||||
|
var map = ol.map({
|
||||||
|
resolutions: [1,4,7,9,12]
|
||||||
|
});
|
||||||
|
|
||||||
|
var res = map.getResForZoom(0);
|
||||||
|
expect(res).toBe(1);
|
||||||
|
res = map.getResForZoom(3);
|
||||||
|
expect(res).toBe(9);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user