Replace fitGeometry and fitExtent with fit

Fit accepts either a geometry or an extent.

This combines two previously distinct functions
into one more flexible call.

Also brings the rotations support and options
previously available to fitGeometry to extents
This commit is contained in:
vmalaret
2015-04-03 18:15:45 +02:00
committed by Bart van den Eijnden
parent 3021d3a6a7
commit 74759142d9
10 changed files with 43 additions and 46 deletions

View File

@@ -100,6 +100,10 @@ The replacement of `ol.tilegrid.Zoomify` is a plain `ol.tilegrid.TileGrid`, conf
* The `minPointsPerRing` config option has been renamed to `minPoints`. It is now also available for linestring drawing, not only for polygons.
* The `ol.DrawEvent` and `ol.DrawEventType` types were renamed to `ol.interaction.DrawEvent` and `ol.interaction.DrawEventType`. This has an impact on your code only if your code is compiled together with ol3.
#### Replace `ol.View.fitExtent()` and `ol.View.fitGeometry()` with `ol.View.fit()`
* This combines two previously distinct functions into one more flexible call which takes either a geometry or an extent.
* Rename all calls to `fitExtent` and `fitGeometry` to `fit`
#### `ol.tilegrid` changes
* The `ol.tilegrid.XYZ` constructor has been replaced by a static `ol.tilegrid.createXYZ()` function. The `ol.tilegrid.createXYZ()` function takes the same arguments as the previous `ol.tilegrid.XYZ` constructor, but returns an `ol.tilegrid.TileGrid` instance.

View File

@@ -6,7 +6,7 @@ docs: >
This example demonstrates how a map's view can be
adjusted so a geometry or coordinate is positioned at a specific
pixel location. The map above has top, right, bottom, and left
padding applied inside the viewport. The view's <code>fitGeometry</code> method
padding applied inside the viewport. The view's <code>fit</code> method
is used to fit a geometry in the view with the same padding. The
view's <code>centerOn</code> method is used to position a coordinate (Lausanne)
at a specific pixel location (the center of the black box).

View File

@@ -65,7 +65,7 @@ zoomtoswitzerlandbest.addEventListener('click', function() {
var feature = source.getFeatures()[0];
var polygon = /** @type {ol.geom.SimpleGeometry} */ (feature.getGeometry());
var size = /** @type {ol.Size} */ (map.getSize());
view.fitGeometry(
view.fit(
polygon,
size,
{
@@ -81,7 +81,7 @@ zoomtoswitzerlandconstrained.addEventListener('click', function() {
var feature = source.getFeatures()[0];
var polygon = /** @type {ol.geom.SimpleGeometry} */ (feature.getGeometry());
var size = /** @type {ol.Size} */ (map.getSize());
view.fitGeometry(
view.fit(
polygon,
size,
{
@@ -96,7 +96,7 @@ zoomtoswitzerlandnearest.addEventListener('click', function() {
var feature = source.getFeatures()[0];
var polygon = /** @type {ol.geom.SimpleGeometry} */ (feature.getGeometry());
var size = /** @type {ol.Size} */ (map.getSize());
view.fitGeometry(
view.fit(
polygon,
size,
{
@@ -111,7 +111,7 @@ zoomtolausanne.addEventListener('click', function() {
var feature = source.getFeatures()[1];
var point = /** @type {ol.geom.SimpleGeometry} */ (feature.getGeometry());
var size = /** @type {ol.Size} */ (map.getSize());
view.fitGeometry(
view.fit(
point,
size,
{

View File

@@ -122,7 +122,7 @@ dragAndDropInteraction.on('addfeatures', function(event) {
style: styleFunction
})
}));
map.getView().fitExtent(
map.getView().fit(
vectorSource.getExtent(), /** @type {ol.Size} */ (map.getSize()));
});

View File

@@ -118,7 +118,7 @@ dragAndDropInteraction.on('addfeatures', function(event) {
source: vectorSource,
style: styleFunction
}));
map.getView().fitExtent(
map.getView().fit(
vectorSource.getExtent(), /** @type {ol.Size} */ (map.getSize()));
});

View File

@@ -1,3 +1,4 @@
/**
* @type {Object}
*/
@@ -6406,7 +6407,7 @@ olx.view;
* minResolution: (number|undefined)}}
* @api
*/
olx.view.FitGeometryOptions;
olx.view.FitOptions;
/**
@@ -6415,7 +6416,7 @@ olx.view.FitGeometryOptions;
* @type {!Array.<number>}
* @api
*/
olx.view.FitGeometryOptions.prototype.padding;
olx.view.FitOptions.prototype.padding;
/**
@@ -6423,7 +6424,7 @@ olx.view.FitGeometryOptions.prototype.padding;
* @type {boolean|undefined}
* @api
*/
olx.view.FitGeometryOptions.prototype.constrainResolution;
olx.view.FitOptions.prototype.constrainResolution;
/**
@@ -6431,7 +6432,7 @@ olx.view.FitGeometryOptions.prototype.constrainResolution;
* @type {boolean|undefined}
* @api
*/
olx.view.FitGeometryOptions.prototype.nearest;
olx.view.FitOptions.prototype.nearest;
/**
@@ -6439,7 +6440,7 @@ olx.view.FitGeometryOptions.prototype.nearest;
* @type {number|undefined}
* @api
*/
olx.view.FitGeometryOptions.prototype.minResolution;
olx.view.FitOptions.prototype.minResolution;
/**
@@ -6448,7 +6449,7 @@ olx.view.FitGeometryOptions.prototype.minResolution;
* @type {number|undefined}
* @api
*/
olx.view.FitGeometryOptions.prototype.maxZoom;
olx.view.FitOptions.prototype.maxZoom;
/* typedefs for object literals exposed by the library */

View File

@@ -338,7 +338,7 @@ ol.control.OverviewMap.prototype.resetExtent_ = function() {
ol.OVERVIEWMAP_MAX_RATIO / ol.OVERVIEWMAP_MIN_RATIO) / Math.LN2;
var ratio = 1 / (Math.pow(2, steps / 2) * ol.OVERVIEWMAP_MIN_RATIO);
ol.extent.scaleFromCenter(extent, ratio);
ovview.fitExtent(extent, ovmapSize);
ovview.fit(extent, ovmapSize);
};

View File

@@ -77,5 +77,5 @@ ol.control.ZoomToExtent.prototype.handleZoomToExtent_ = function() {
view.getProjection().getExtent() : this.extent_;
var size = map.getSize();
goog.asserts.assert(goog.isDef(size), 'size should be defined');
view.fitExtent(extent, size);
view.fit(extent, size);
};

View File

@@ -14,6 +14,8 @@ goog.require('ol.RotationConstraintType');
goog.require('ol.Size');
goog.require('ol.coordinate');
goog.require('ol.extent');
goog.require('ol.geom.Polygon');
goog.require('ol.geom.SimpleGeometry');
goog.require('ol.proj');
goog.require('ol.proj.METERS_PER_UNIT');
goog.require('ol.proj.Projection');
@@ -431,35 +433,19 @@ ol.View.prototype.getZoom = function() {
/**
* Fit the map view to the passed extent and size. The size is pixel dimensions
* of the box to fit the extent into. In most cases you will want to use the map
* size, that is `map.getSize()`.
* @param {ol.Extent} extent Extent.
* Fit the given geometry or extent based on the given map size and border.
* Take care on the map angle.
* @param {ol.geom.SimpleGeometry|ol.Extent} geometry Geometry.
* @param {ol.Size} size Box pixel size.
* @param {olx.view.FitOptions=} opt_options Options.
* @api
*/
ol.View.prototype.fitExtent = function(extent, size) {
if (!ol.extent.isEmpty(extent)) {
this.setCenter(ol.extent.getCenter(extent));
var resolution = this.getResolutionForExtent(extent, size);
var constrainedResolution = this.constrainResolution(resolution, 0, 0);
if (constrainedResolution < resolution) {
constrainedResolution =
this.constrainResolution(constrainedResolution, -1, 0);
}
this.setResolution(constrainedResolution);
ol.View.prototype.fit = function(geometry, size, opt_options) {
if (!(geometry instanceof ol.geom.SimpleGeometry)) {
geometry = ol.geom.Polygon.fromExtent(geometry);
}
};
/**
* Fit the given geometry into the view based on the given map size and border.
* @param {ol.geom.SimpleGeometry} geometry Geometry.
* @param {ol.Size} size Box pixel size.
* @param {olx.view.FitGeometryOptions=} opt_options Options.
* @api
*/
ol.View.prototype.fitGeometry = function(geometry, size, opt_options) {
var options = goog.isDef(opt_options) ? opt_options : {};
var padding = goog.isDef(options.padding) ? options.padding : [0, 0, 0, 0];

View File

@@ -364,7 +364,7 @@ describe('ol.View', function() {
});
});
describe('fitGeometry', function() {
describe('fit', function() {
var view;
beforeEach(function() {
view = new ol.View({
@@ -372,7 +372,7 @@ describe('ol.View', function() {
});
});
it('fit correctly to the geometry', function() {
view.fitGeometry(
view.fit(
new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]),
[200, 200],
{
@@ -384,7 +384,7 @@ describe('ol.View', function() {
expect(view.getCenter()[0]).to.be(5950);
expect(view.getCenter()[1]).to.be(47100);
view.fitGeometry(
view.fit(
new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]),
[200, 200],
{
@@ -395,7 +395,7 @@ describe('ol.View', function() {
expect(view.getCenter()[0]).to.be(5500);
expect(view.getCenter()[1]).to.be(47550);
view.fitGeometry(
view.fit(
new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]),
[200, 200],
{
@@ -407,7 +407,7 @@ describe('ol.View', function() {
expect(view.getCenter()[0]).to.be(6000);
expect(view.getCenter()[1]).to.be(47050);
view.fitGeometry(
view.fit(
new ol.geom.Point([6000, 46000]),
[200, 200],
{
@@ -419,7 +419,7 @@ describe('ol.View', function() {
expect(view.getCenter()[0]).to.be(5900);
expect(view.getCenter()[1]).to.be(46100);
view.fitGeometry(
view.fit(
new ol.geom.Point([6000, 46000]),
[200, 200],
{
@@ -433,7 +433,7 @@ describe('ol.View', function() {
expect(view.getCenter()[1]).to.be(46100);
view.setRotation(Math.PI / 4);
view.fitGeometry(
view.fit(
new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]),
[200, 200],
{
@@ -445,6 +445,12 @@ describe('ol.View', function() {
expect(view.getCenter()[0]).to.roughlyEqual(5200, 1e-9);
expect(view.getCenter()[1]).to.roughlyEqual(46300, 1e-9);
});
it('fit correctly to the extent', function() {
view.fit([1000, 1000, 2000, 2000], [200, 200]);
expect(view.getResolution()).to.be(5);
expect(view.getCenter()[0]).to.be(1500);
expect(view.getCenter()[1]).to.be(1500);
});
});
describe('centerOn', function() {