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:
committed by
Bart van den Eijnden
parent
3021d3a6a7
commit
74759142d9
@@ -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 `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.
|
* 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
|
#### `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.
|
* 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.
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ docs: >
|
|||||||
This example demonstrates how a map's view can be
|
This example demonstrates how a map's view can be
|
||||||
adjusted so a geometry or coordinate is positioned at a specific
|
adjusted so a geometry or coordinate is positioned at a specific
|
||||||
pixel location. The map above has top, right, bottom, and left
|
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
|
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)
|
view's <code>centerOn</code> method is used to position a coordinate (Lausanne)
|
||||||
at a specific pixel location (the center of the black box).
|
at a specific pixel location (the center of the black box).
|
||||||
|
|||||||
+4
-4
@@ -65,7 +65,7 @@ zoomtoswitzerlandbest.addEventListener('click', function() {
|
|||||||
var feature = source.getFeatures()[0];
|
var feature = source.getFeatures()[0];
|
||||||
var polygon = /** @type {ol.geom.SimpleGeometry} */ (feature.getGeometry());
|
var polygon = /** @type {ol.geom.SimpleGeometry} */ (feature.getGeometry());
|
||||||
var size = /** @type {ol.Size} */ (map.getSize());
|
var size = /** @type {ol.Size} */ (map.getSize());
|
||||||
view.fitGeometry(
|
view.fit(
|
||||||
polygon,
|
polygon,
|
||||||
size,
|
size,
|
||||||
{
|
{
|
||||||
@@ -81,7 +81,7 @@ zoomtoswitzerlandconstrained.addEventListener('click', function() {
|
|||||||
var feature = source.getFeatures()[0];
|
var feature = source.getFeatures()[0];
|
||||||
var polygon = /** @type {ol.geom.SimpleGeometry} */ (feature.getGeometry());
|
var polygon = /** @type {ol.geom.SimpleGeometry} */ (feature.getGeometry());
|
||||||
var size = /** @type {ol.Size} */ (map.getSize());
|
var size = /** @type {ol.Size} */ (map.getSize());
|
||||||
view.fitGeometry(
|
view.fit(
|
||||||
polygon,
|
polygon,
|
||||||
size,
|
size,
|
||||||
{
|
{
|
||||||
@@ -96,7 +96,7 @@ zoomtoswitzerlandnearest.addEventListener('click', function() {
|
|||||||
var feature = source.getFeatures()[0];
|
var feature = source.getFeatures()[0];
|
||||||
var polygon = /** @type {ol.geom.SimpleGeometry} */ (feature.getGeometry());
|
var polygon = /** @type {ol.geom.SimpleGeometry} */ (feature.getGeometry());
|
||||||
var size = /** @type {ol.Size} */ (map.getSize());
|
var size = /** @type {ol.Size} */ (map.getSize());
|
||||||
view.fitGeometry(
|
view.fit(
|
||||||
polygon,
|
polygon,
|
||||||
size,
|
size,
|
||||||
{
|
{
|
||||||
@@ -111,7 +111,7 @@ zoomtolausanne.addEventListener('click', function() {
|
|||||||
var feature = source.getFeatures()[1];
|
var feature = source.getFeatures()[1];
|
||||||
var point = /** @type {ol.geom.SimpleGeometry} */ (feature.getGeometry());
|
var point = /** @type {ol.geom.SimpleGeometry} */ (feature.getGeometry());
|
||||||
var size = /** @type {ol.Size} */ (map.getSize());
|
var size = /** @type {ol.Size} */ (map.getSize());
|
||||||
view.fitGeometry(
|
view.fit(
|
||||||
point,
|
point,
|
||||||
size,
|
size,
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ dragAndDropInteraction.on('addfeatures', function(event) {
|
|||||||
style: styleFunction
|
style: styleFunction
|
||||||
})
|
})
|
||||||
}));
|
}));
|
||||||
map.getView().fitExtent(
|
map.getView().fit(
|
||||||
vectorSource.getExtent(), /** @type {ol.Size} */ (map.getSize()));
|
vectorSource.getExtent(), /** @type {ol.Size} */ (map.getSize()));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ dragAndDropInteraction.on('addfeatures', function(event) {
|
|||||||
source: vectorSource,
|
source: vectorSource,
|
||||||
style: styleFunction
|
style: styleFunction
|
||||||
}));
|
}));
|
||||||
map.getView().fitExtent(
|
map.getView().fit(
|
||||||
vectorSource.getExtent(), /** @type {ol.Size} */ (map.getSize()));
|
vectorSource.getExtent(), /** @type {ol.Size} */ (map.getSize()));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+7
-6
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Object}
|
* @type {Object}
|
||||||
*/
|
*/
|
||||||
@@ -6406,7 +6407,7 @@ olx.view;
|
|||||||
* minResolution: (number|undefined)}}
|
* minResolution: (number|undefined)}}
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
olx.view.FitGeometryOptions;
|
olx.view.FitOptions;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -6415,7 +6416,7 @@ olx.view.FitGeometryOptions;
|
|||||||
* @type {!Array.<number>}
|
* @type {!Array.<number>}
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
olx.view.FitGeometryOptions.prototype.padding;
|
olx.view.FitOptions.prototype.padding;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -6423,7 +6424,7 @@ olx.view.FitGeometryOptions.prototype.padding;
|
|||||||
* @type {boolean|undefined}
|
* @type {boolean|undefined}
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
olx.view.FitGeometryOptions.prototype.constrainResolution;
|
olx.view.FitOptions.prototype.constrainResolution;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -6431,7 +6432,7 @@ olx.view.FitGeometryOptions.prototype.constrainResolution;
|
|||||||
* @type {boolean|undefined}
|
* @type {boolean|undefined}
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
olx.view.FitGeometryOptions.prototype.nearest;
|
olx.view.FitOptions.prototype.nearest;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -6439,7 +6440,7 @@ olx.view.FitGeometryOptions.prototype.nearest;
|
|||||||
* @type {number|undefined}
|
* @type {number|undefined}
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
olx.view.FitGeometryOptions.prototype.minResolution;
|
olx.view.FitOptions.prototype.minResolution;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -6448,7 +6449,7 @@ olx.view.FitGeometryOptions.prototype.minResolution;
|
|||||||
* @type {number|undefined}
|
* @type {number|undefined}
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
olx.view.FitGeometryOptions.prototype.maxZoom;
|
olx.view.FitOptions.prototype.maxZoom;
|
||||||
|
|
||||||
|
|
||||||
/* typedefs for object literals exposed by the library */
|
/* typedefs for object literals exposed by the library */
|
||||||
|
|||||||
@@ -338,7 +338,7 @@ ol.control.OverviewMap.prototype.resetExtent_ = function() {
|
|||||||
ol.OVERVIEWMAP_MAX_RATIO / ol.OVERVIEWMAP_MIN_RATIO) / Math.LN2;
|
ol.OVERVIEWMAP_MAX_RATIO / ol.OVERVIEWMAP_MIN_RATIO) / Math.LN2;
|
||||||
var ratio = 1 / (Math.pow(2, steps / 2) * ol.OVERVIEWMAP_MIN_RATIO);
|
var ratio = 1 / (Math.pow(2, steps / 2) * ol.OVERVIEWMAP_MIN_RATIO);
|
||||||
ol.extent.scaleFromCenter(extent, ratio);
|
ol.extent.scaleFromCenter(extent, ratio);
|
||||||
ovview.fitExtent(extent, ovmapSize);
|
ovview.fit(extent, ovmapSize);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -77,5 +77,5 @@ ol.control.ZoomToExtent.prototype.handleZoomToExtent_ = function() {
|
|||||||
view.getProjection().getExtent() : this.extent_;
|
view.getProjection().getExtent() : this.extent_;
|
||||||
var size = map.getSize();
|
var size = map.getSize();
|
||||||
goog.asserts.assert(goog.isDef(size), 'size should be defined');
|
goog.asserts.assert(goog.isDef(size), 'size should be defined');
|
||||||
view.fitExtent(extent, size);
|
view.fit(extent, size);
|
||||||
};
|
};
|
||||||
|
|||||||
+10
-24
@@ -14,6 +14,8 @@ goog.require('ol.RotationConstraintType');
|
|||||||
goog.require('ol.Size');
|
goog.require('ol.Size');
|
||||||
goog.require('ol.coordinate');
|
goog.require('ol.coordinate');
|
||||||
goog.require('ol.extent');
|
goog.require('ol.extent');
|
||||||
|
goog.require('ol.geom.Polygon');
|
||||||
|
goog.require('ol.geom.SimpleGeometry');
|
||||||
goog.require('ol.proj');
|
goog.require('ol.proj');
|
||||||
goog.require('ol.proj.METERS_PER_UNIT');
|
goog.require('ol.proj.METERS_PER_UNIT');
|
||||||
goog.require('ol.proj.Projection');
|
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
|
* Fit the given geometry or extent based on the given map size and border.
|
||||||
* of the box to fit the extent into. In most cases you will want to use the map
|
* Take care on the map angle.
|
||||||
* size, that is `map.getSize()`.
|
* @param {ol.geom.SimpleGeometry|ol.Extent} geometry Geometry.
|
||||||
* @param {ol.Extent} extent Extent.
|
|
||||||
* @param {ol.Size} size Box pixel size.
|
* @param {ol.Size} size Box pixel size.
|
||||||
|
* @param {olx.view.FitOptions=} opt_options Options.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
ol.View.prototype.fitExtent = function(extent, size) {
|
ol.View.prototype.fit = function(geometry, size, opt_options) {
|
||||||
if (!ol.extent.isEmpty(extent)) {
|
|
||||||
this.setCenter(ol.extent.getCenter(extent));
|
if (!(geometry instanceof ol.geom.SimpleGeometry)) {
|
||||||
var resolution = this.getResolutionForExtent(extent, size);
|
geometry = ol.geom.Polygon.fromExtent(geometry);
|
||||||
var constrainedResolution = this.constrainResolution(resolution, 0, 0);
|
|
||||||
if (constrainedResolution < resolution) {
|
|
||||||
constrainedResolution =
|
|
||||||
this.constrainResolution(constrainedResolution, -1, 0);
|
|
||||||
}
|
|
||||||
this.setResolution(constrainedResolution);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 options = goog.isDef(opt_options) ? opt_options : {};
|
||||||
|
|
||||||
var padding = goog.isDef(options.padding) ? options.padding : [0, 0, 0, 0];
|
var padding = goog.isDef(options.padding) ? options.padding : [0, 0, 0, 0];
|
||||||
|
|||||||
@@ -364,7 +364,7 @@ describe('ol.View', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('fitGeometry', function() {
|
describe('fit', function() {
|
||||||
var view;
|
var view;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
view = new ol.View({
|
view = new ol.View({
|
||||||
@@ -372,7 +372,7 @@ describe('ol.View', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('fit correctly to the geometry', function() {
|
it('fit correctly to the geometry', function() {
|
||||||
view.fitGeometry(
|
view.fit(
|
||||||
new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]),
|
new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]),
|
||||||
[200, 200],
|
[200, 200],
|
||||||
{
|
{
|
||||||
@@ -384,7 +384,7 @@ describe('ol.View', function() {
|
|||||||
expect(view.getCenter()[0]).to.be(5950);
|
expect(view.getCenter()[0]).to.be(5950);
|
||||||
expect(view.getCenter()[1]).to.be(47100);
|
expect(view.getCenter()[1]).to.be(47100);
|
||||||
|
|
||||||
view.fitGeometry(
|
view.fit(
|
||||||
new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]),
|
new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]),
|
||||||
[200, 200],
|
[200, 200],
|
||||||
{
|
{
|
||||||
@@ -395,7 +395,7 @@ describe('ol.View', function() {
|
|||||||
expect(view.getCenter()[0]).to.be(5500);
|
expect(view.getCenter()[0]).to.be(5500);
|
||||||
expect(view.getCenter()[1]).to.be(47550);
|
expect(view.getCenter()[1]).to.be(47550);
|
||||||
|
|
||||||
view.fitGeometry(
|
view.fit(
|
||||||
new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]),
|
new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]),
|
||||||
[200, 200],
|
[200, 200],
|
||||||
{
|
{
|
||||||
@@ -407,7 +407,7 @@ describe('ol.View', function() {
|
|||||||
expect(view.getCenter()[0]).to.be(6000);
|
expect(view.getCenter()[0]).to.be(6000);
|
||||||
expect(view.getCenter()[1]).to.be(47050);
|
expect(view.getCenter()[1]).to.be(47050);
|
||||||
|
|
||||||
view.fitGeometry(
|
view.fit(
|
||||||
new ol.geom.Point([6000, 46000]),
|
new ol.geom.Point([6000, 46000]),
|
||||||
[200, 200],
|
[200, 200],
|
||||||
{
|
{
|
||||||
@@ -419,7 +419,7 @@ describe('ol.View', function() {
|
|||||||
expect(view.getCenter()[0]).to.be(5900);
|
expect(view.getCenter()[0]).to.be(5900);
|
||||||
expect(view.getCenter()[1]).to.be(46100);
|
expect(view.getCenter()[1]).to.be(46100);
|
||||||
|
|
||||||
view.fitGeometry(
|
view.fit(
|
||||||
new ol.geom.Point([6000, 46000]),
|
new ol.geom.Point([6000, 46000]),
|
||||||
[200, 200],
|
[200, 200],
|
||||||
{
|
{
|
||||||
@@ -433,7 +433,7 @@ describe('ol.View', function() {
|
|||||||
expect(view.getCenter()[1]).to.be(46100);
|
expect(view.getCenter()[1]).to.be(46100);
|
||||||
|
|
||||||
view.setRotation(Math.PI / 4);
|
view.setRotation(Math.PI / 4);
|
||||||
view.fitGeometry(
|
view.fit(
|
||||||
new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]),
|
new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]),
|
||||||
[200, 200],
|
[200, 200],
|
||||||
{
|
{
|
||||||
@@ -445,6 +445,12 @@ describe('ol.View', function() {
|
|||||||
expect(view.getCenter()[0]).to.roughlyEqual(5200, 1e-9);
|
expect(view.getCenter()[0]).to.roughlyEqual(5200, 1e-9);
|
||||||
expect(view.getCenter()[1]).to.roughlyEqual(46300, 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() {
|
describe('centerOn', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user