Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

612 - GMF draw - Zoom out when required #4292

Merged
merged 1 commit into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contribs/gmf/src/drawing/drawFeatureComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ exports.Controller_ = function($scope, $timeout, gettextCatalog,
this.selectedFeatures.push(newFeature);
this.registerInteractions_();
if (this.listSelectionInProgress_) {
this.featureHelper_.panMapToFeature(newFeature, this.map);
this.featureHelper_.fitMapToFeature(newFeature, this.map);
this.listSelectionInProgress_ = false;
}
} else {
Expand Down
87 changes: 57 additions & 30 deletions src/misc/FeatureHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1206,45 +1206,71 @@ exports.prototype.getType = function(feature) {


/**
* This method first checks if a feature's extent intersects with the map view
* extent. If it doesn't, then the view gets recentered with an animation to
* the center of the feature.
* This methods will try to fit a feature into a map view.
*
* If the feature is already visible, then the map will be zoomed out
* if the feature is too big for the current view.
*
* If the feature is not visible but would fit in the map view, the
* map is panned to the center of the feature.
*
* If the feature is not visible and would not fit in the map view,
* the map is fix to the feature's extent.
*
* @param {!ol.Feature} feature Feature.
* @param {!ol.Map} map Map.
* @param {boolean=} opt_zoomOut Whether the map should also be zoomed
* out if the feature doesn't fit inside the current map view
* extent. Defaults to `true`.
* @param {number=} opt_panDuration Pan animation duration. Defaults to `250`.
* @param {number=} opt_duration Aimation duration. Defaults to `250`.
* @export
*/
exports.prototype.panMapToFeature = function(feature, map, opt_zoomOut,
opt_panDuration) {
exports.prototype.fitMapToFeature = function(feature, map, opt_duration) {

const panDuration = opt_panDuration !== undefined ? opt_panDuration : 250;
const zoomOut = opt_zoomOut !== false;
const duration = opt_duration !== undefined ? opt_duration : 250;
const size = map.getSize();
googAsserts.assertArray(size);
const view = map.getView();
const viewExtent = view.calculateExtent(size);
const geometry = feature.getGeometry();

if (!geometry.intersectsExtent(viewExtent)) {
const mapCenter = view.getCenter();
googAsserts.assertArray(mapCenter);
const geomIsVisible = geometry.intersectsExtent(viewExtent);

const mapCenter = view.getCenter();
googAsserts.assertArray(mapCenter);

const featureExtent = geometry.getExtent();

if (geomIsVisible) {

if (!(geometry instanceof olGeomPoint)) {
// == Action: Zoom out ==
// if the geometry is visible
const featureResolution = view.getResolutionForExtent(featureExtent);
const featureZoom = Math.floor(
view.getZoomForResolution(featureResolution));
const zoom = view.getZoom();
if (featureZoom < zoom) {
view.animate({
center: mapCenter,
duration: duration
}, {
center: mapCenter,
duration: duration,
zoom: featureZoom
});
}
}

const featureExtent = geometry.getExtent();
let shouldPan = true;
} else {

if (zoomOut && !(geometry instanceof olGeomPoint)) {
const featureExtentHeight = olExtent.getHeight(featureExtent);
const featureExtentWidth = olExtent.getWidth(featureExtent);
const viewExtentHeight = olExtent.getHeight(viewExtent);
const viewExtentWidth = olExtent.getWidth(viewExtent);
shouldPan = viewExtentHeight >= featureExtentHeight &&
viewExtentWidth >= featureExtentWidth;
}
const featureExtentHeight = olExtent.getHeight(featureExtent);
const featureExtentWidth = olExtent.getWidth(featureExtent);
const viewExtentHeight = olExtent.getHeight(viewExtent);
const viewExtentWidth = olExtent.getWidth(viewExtent);
const geomFitsInExtent = viewExtentHeight >= featureExtentHeight &&
viewExtentWidth >= featureExtentWidth;

if (shouldPan) {
if (geomFitsInExtent) {
// == Action: Pan ==
// if geometry is not visible but fits in current map extent
let featureCenter;
if (geometry instanceof olGeomLineString) {
featureCenter = geometry.getCoordinateAt(0.5);
Expand All @@ -1258,16 +1284,17 @@ exports.prototype.panMapToFeature = function(feature, map, opt_zoomOut,

view.animate({
center: mapCenter,
duration: panDuration
duration: duration
}, {
center: featureCenter,
duration: panDuration
duration: duration
});
} else {
// i.e. "should zoom out to the feature" instead
// == Action: Fit ==
// if geometry is not visible and doesn't fit in current map extent
view.fit(featureExtent, {
duration: panDuration,
size: size
duration,
size
});
}
}
Expand Down