map get/set methods

This commit is contained in:
Mike Adair
2012-06-19 08:06:08 -04:00
3 changed files with 100 additions and 14 deletions

View File

@@ -4,6 +4,7 @@ goog.require('ol.Loc');
goog.require('ol.Map');
goog.require('ol.Projection');
goog.require('ol.loc');
goog.require('ol.projection');
/**
@@ -24,6 +25,8 @@ ol.map = function(opt_arg){
var zoom;
/** @type {number|undefined} */
var numZoomLevels;
/** @type {ol.Projection|undefined} */
var projection;
var target;
var map = new ol.Map();
@@ -47,6 +50,10 @@ ol.map = function(opt_arg){
numZoomLevels = config.numZoomLevels;
map.setNumZoomLevels(numZoomLevels);
}
if (goog.isDef(config.projection)) {
projection = config.projection;
map.setProjection(projection);
}
if (goog.isDef(config.target)) {
target = config.target;
}
@@ -72,23 +79,35 @@ ol.Map.prototype.center = function(opt_arg) {
}
};
/**
* @param {number|undefined} opt_arg Get or set the current zoom level.
* @returns {ol.Map|number|undefined} current zoom level on get or the map.
*/
ol.Map.prototype.zoom = function(opt_arg) {
if (arguments.length == 1 && goog.isDef(opt_arg)) {
return this.setZoom(opt_arg);
} else {
return this.getZoom();
}
};
/**
* @param {ol.Projection|string|undefined} opt_arg Get or set the map projection.
* @returns {ol.Map|number|undefined} the current zoom level, or the map on set.
*/
ol.Map.prototype.projection = function(opt_arg) {
if (arguments.length == 1 && goog.isDef(opt_arg)) {
return this.setProjection(ol.projection(opt_arg));
} else {
return this.getProjection();
}
};
/**
* @param {ol.ProjectionLike=} opt_arg
* @returns {ol.Map|ol.Loc|undefined}
*/
ol.Map.prototype.userProjection = function(opt_arg) {
if (arguments.length == 1 && goog.isDef(opt_arg)) {
return this.setUserProjection(ol.projection(opt_arg));
} else {
return this.getUserProjection();
}
};
/**
* @param {number|undefined} opt_arg Get or set the current zoom level.
* @returns {ol.Map|number|undefined} current zoom level on get or the map.
*/
ol.Map.prototype.zoom = function(opt_arg) {
if (arguments.length == 1 && goog.isDef(opt_arg)) {
return this.setZoom(opt_arg);
} else {

View File

@@ -14,7 +14,13 @@ ol.Map = function() {
* @private
* @type {ol.Projection}
*/
this.projection_ = new ol.Projection();
this.projection_ ;
/**
* @private
* @type {ol.Projection}
*/
this.userProjection_;
/**
* @private
@@ -36,6 +42,13 @@ ol.Map = function() {
};
ol.Map.prototype.defaults = {};
/**
@type {string}
*/
ol.Map.prototype.defaults.projection = "EPSG:3857";
ol.Map.prototype.defaults.userProjection = "EPSG:4326";
/**
* @return {ol.Loc} Location.
@@ -49,10 +62,24 @@ ol.Map.prototype.getCenter = function() {
* @return {ol.Projection} Projection.
*/
ol.Map.prototype.getProjection = function() {
if (!goog.isDef(this.projection_)) {
this.projection_ = new ol.Projection(this.defaults.projection);
}
return this.projection_;
};
/**
* @return {ol.Projection} User projection.
*/
ol.Map.prototype.getUserProjection = function() {
if (!goog.isDef(this.userProjection_)) {
this.userProjection_ = new ol.Projection(this.defaults.userProjection_);
}
return this.userProjection_;
};
/**
* @return {number} Zoom.
*/
@@ -89,6 +116,16 @@ ol.Map.prototype.setProjection = function(projection) {
};
/**
* @param {ol.Projection} projection set the user projection.
* @return {ol.Map} This.
*/
ol.Map.prototype.setUserProjection = function(projection) {
this.userProjection_ = projection;
return this;
};
/**
* @param {number} zoom Zoom.
* @return {ol.Map} This.
@@ -107,3 +144,9 @@ ol.Map.prototype.setNumZoomLevels = function(nZoom) {
this.numZoomLevels_ = nZoom;
return this;
};
/**
*/
ol.Map.prototype.destroy = function() {
//remove layers, etc.
};

View File

@@ -57,7 +57,7 @@ describe("ol.Map", function() {
// all at once
map = ol.map({
center: [1, 2],
center: [4, 5],
zoom: 6
});
@@ -98,6 +98,30 @@ describe("ol.Map", function() {
});
it("has a default user projection in 4326", function() {
var map = ol.map();
var userproj = map.userProjection();
expect(userproj instanceof ol.Projection).toBe(true);
expect(userproj.code()).toBe("EPSG:4326");
});
it("allows number of zoom levels to be set", function() {
var map = ol.map();
var nzoom = map.numZoomLevels();
expect(nzoom).toBe(22);
map.numZoomLevels(15);
nzoom = map.numZoomLevels();
expect(nzoom).toBe(15);
});
it("allows a user projection to be set", function() {
var proj;