Commit acf98a9a authored by Piotr Gawron's avatar Piotr Gawron
Browse files

fitBounds method for OpenLayers implemented

parent 3b6dec49
......@@ -50,4 +50,17 @@ Bounds.prototype.getRightBottom = function () {
return this._rightBottom;
};
/**
*
* @param {Point} point
* @returns {boolean}
*/
Bounds.prototype.contains = function (point) {
return this._topLeft.x <= point.x && //
this._topLeft.y <= point.y && //
this._rightBottom.x >= point.x && //
this._rightBottom.y >= point.y;
};
module.exports = Bounds;
......@@ -50,6 +50,7 @@ OpenLayerCanvas.prototype.createLayers = function (options) {
source: new ol.source.XYZ({
minZoom: options.minZoom,
maxZoom: options.maxZoom,
wrapX: false,
tileLoadFunction: function (imageTile, src) {
if (src !== null) {
imageTile.getImage().src = src;
......@@ -138,7 +139,10 @@ OpenLayerCanvas.prototype.addRightBottomControl = function (element) {
};
OpenLayerCanvas.prototype.fitBounds = function (bounds) {
logger.fatal("Not implemented");
var self = this;
var projectionBounds = [self.fromPointToProjection(bounds.getRightBottom()), self.fromPointToProjection(bounds.getTopLeft())];
var extent = ol.extent.boundingExtent(projectionBounds);
self.getOpenLayersMap().getView().fit(extent);
};
OpenLayerCanvas.prototype.setCenter = function (center) {
......@@ -188,13 +192,11 @@ OpenLayerCanvas.prototype.getBackgroundId = function () {
*/
OpenLayerCanvas.prototype.setBackgroundId = function (backgroundId) {
backgroundId = parseInt(backgroundId);
console.log("background: ", backgroundId);
var layers = this._layers;
for (var id in layers) {
if (layers.hasOwnProperty(id)) {
var layer = layers[id];
var visible = (parseInt(id) === backgroundId);
console.log(id, "show", visible);
layer.setVisible(visible);
}
}
......@@ -207,15 +209,12 @@ OpenLayerCanvas.prototype.getBounds = function () {
var self = this;
var map = self.getOpenLayersMap();
// console.log(map.getSize());
var extent = map.getView().calculateExtent(map.getSize());
var projection1 = [extent[0], extent[1]];
var p1 = self.fromProjectionToPoint(projection1);
var projection2 = [extent[2], extent[3]];
var p2 = self.fromProjectionToPoint(projection2);
// console.log(extent);
// console.log(p1, p2);
var maxWidth = 360 * self.pixelsPerLonDegree_ * self.zoomFactor;
if (p1.x > maxWidth / 2) {
p1.x -= maxWidth;
......
"use strict";
require("../../mocha-config");
// noinspection JSUnusedLocalSymbols
var logger = require('../../logger');
var Bounds = require('../../../../main/js/map/canvas/Bounds');
......@@ -41,4 +42,33 @@ describe('Bounds', function () {
assert.ok(bounds.getRightBottom().y === p1.y);
});
describe("contains", function () {
it("point inside", function () {
var p1 = new Point(0, 0);
var p2 = new Point(10, 10);
var bounds = new Bounds(p1, p2);
var inside = new Point(5, 8);
assert.ok(bounds.contains(inside));
});
it("point on border", function () {
var p1 = new Point(0, 0);
var p2 = new Point(10, 10);
var bounds = new Bounds(p1, p2);
assert.ok(bounds.contains(p1));
assert.ok(bounds.contains(p2));
});
it("point on outside", function () {
var p1 = new Point(0, 0);
var p2 = new Point(10, 10);
var bounds = new Bounds(p1, p2);
assert.notOk(bounds.contains(new Point(-1, 5)));
assert.notOk(bounds.contains(new Point(11, 5)));
assert.notOk(bounds.contains(new Point(5, -1)));
assert.notOk(bounds.contains(new Point(5, 11)));
});
});
});
......@@ -78,11 +78,50 @@ describe('OpenLayerCanvas', function () {
it("getBounds", function () {
var canvas = new OpenLayerCanvas(testDiv, testOptions);
//set size of the map in DOM
canvas.getOpenLayersMap().setSize([800,600]);
canvas.getOpenLayersMap().setSize([800, 600]);
var bounds = canvas.getBounds(5);
assert.ok(bounds instanceof Bounds);
});
describe("fitBounds", function () {
it("part of map", function () {
var canvas = new OpenLayerCanvas(testDiv, testOptions);
//set size of the map in DOM
canvas.getOpenLayersMap().setSize([800, 600]);
var p1 = new Point(10, 10);
var p2 = new Point(20, 10);
var bounds = new Bounds(p1, p2);
canvas.fitBounds(bounds);
var mapBounds = canvas.getBounds();
console.log(mapBounds);
assert.ok(mapBounds.contains(p1));
assert.ok(mapBounds.contains(p2));
assert.notOk(mapBounds.contains(new Point(0, 0)) && mapBounds.contains(new Point(canvas.getWidth(), canvas.getHeight())));
});
it("full map", function () {
var canvas = new OpenLayerCanvas(testDiv, testOptions);
//set size of the map in DOM
canvas.getOpenLayersMap().setSize([800, 600]);
var p1 = new Point(0, 0);
var p2 = new Point(canvas.getWidth(), canvas.getHeight());
var bounds = new Bounds(p1, p2);
canvas.fitBounds(bounds);
var mapBounds = canvas.getBounds();
assert.ok(mapBounds.contains(p1));
assert.ok(mapBounds.contains(p2));
});
});
});
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment