Skip to content

Commit 43f6135

Browse files
authored
Merge pull request #1360 from OpenGeoscience/no-trigger
feat: Add a zoomAndCenter method
2 parents 9174d2a + a68ac6a commit 43f6135

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

src/map.js

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,13 @@ var map = function (arg) {
409409
* option when determining the new view.
410410
* @param {boolean} [ignoreClampBounds] If `true`, ignore the clampBounds
411411
* option when determining the new view.
412+
* @param {boolean} [noTrigger] If truthy, do not trigger a pan or zoom
413+
* event. If 'pan', only trigger the zoom event.
412414
* @returns {number|this}
413415
* @fires geo.event.zoom
414416
* @fires geo.event.pan
415417
*/
416-
this.zoom = function (val, origin, ignoreDiscreteZoom, ignoreClampBounds) {
418+
this.zoom = function (val, origin, ignoreDiscreteZoom, ignoreClampBounds, noTrigger) {
417419
if (val === undefined) {
418420
return m_zoom;
419421
}
@@ -442,19 +444,49 @@ var map = function (arg) {
442444
zoomLevel: m_zoom,
443445
screenPosition: origin ? origin.map : undefined
444446
};
445-
m_this.geoTrigger(geo_event.zoom, evt);
447+
if (!noTrigger || noTrigger === 'pan') {
448+
m_this.geoTrigger(geo_event.zoom, evt);
449+
}
446450

447451
if (aroundPoint) {
448452
var shifted = m_this.gcsToDisplay(origin.mapgcs || origin.geo,
449453
origin.mapgcs ? null : undefined);
450454
m_this.pan({x: origin.map.x - shifted.x, y: origin.map.y - shifted.y},
451-
ignoreDiscreteZoom, true);
455+
ignoreDiscreteZoom, true, noTrigger);
452456
} else {
453-
m_this.pan({x: 0, y: 0}, ignoreDiscreteZoom, ignoreClampBounds);
457+
m_this.pan({x: 0, y: 0}, ignoreDiscreteZoom, ignoreClampBounds, noTrigger);
454458
}
455459
return m_this;
456460
};
457461

462+
/**
463+
* Set zoom level and center of the map.
464+
*
465+
* @param {number} zoom The new zoom level to set.
466+
* @param {geo.geoPosition} center The new center of the
467+
* @param {string|geo.transform|null} [gcs] `undefined` to use the interface
468+
* gcs, `null` to use the map gcs, or any other transform. The center is
469+
* converted from this gcs to the map projection.
470+
* @param {geo.geoPosition} origin.geo The gcs coordinates of the zoom
471+
* center.
472+
* @param {geo.screenPosition} origin.map The display coordinates of the zoom
473+
* center.
474+
* @param {boolean} [ignoreDiscreteZoom] If `true`, ignore the discreteZoom
475+
* option when determining the new view.
476+
* @param {boolean} [ignoreClampBounds] If `true`, ignore the clampBounds
477+
* option when determining the new view.
478+
* @param {boolean} [noTrigger] If truthy, do not trigger a pan or zoom
479+
* event.
480+
* @returns {this}
481+
* @fires geo.event.zoom
482+
* @fires geo.event.pan
483+
*/
484+
this.zoomAndCenter = function (zoom, center, gcs, ignoreDiscreteZoom, ignoreClampBounds, noTrigger) {
485+
this.zoom(zoom, undefined, ignoreDiscreteZoom, ignoreClampBounds, noTrigger || 'pan');
486+
this.center(center, gcs, ignoreDiscreteZoom, ignoreClampBounds, noTrigger);
487+
return this;
488+
};
489+
458490
/**
459491
* Pan the map by a number of display pixels.
460492
*
@@ -468,10 +500,11 @@ var map = function (arg) {
468500
* view. When `'limited'`, the `clampBoundsX` and `clampBoundsY` options
469501
* are selectively enforced so that the map will not end up more out of
470502
* bounds than its current state.
503+
* @param {boolean} [noTrigger] If truthy, do not trigger a pan event.
471504
* @returns {this}
472505
* @fires geo.event.pan
473506
*/
474-
this.pan = function (delta, ignoreDiscreteZoom, ignoreClampBounds) {
507+
this.pan = function (delta, ignoreDiscreteZoom, ignoreClampBounds, noTrigger) {
475508
var evt = {
476509
screenDelta: delta
477510
};
@@ -510,7 +543,9 @@ var map = function (arg) {
510543
y: m_height / 2
511544
});
512545

513-
m_this.geoTrigger(geo_event.pan, evt);
546+
if (!noTrigger) {
547+
m_this.geoTrigger(geo_event.pan, evt);
548+
}
514549

515550
m_this.modified();
516551
return m_this;
@@ -575,7 +610,7 @@ var map = function (arg) {
575610
/**
576611
* Get or set the center of the map in the given geographic coordinates.
577612
*
578-
* @param {geo.geoPosition} coordinates If specified, the new center of the
613+
* @param {geo.geoPosition} [coordinates] If specified, the new center of the
579614
* map.
580615
* @param {string|geo.transform|null} [gcs] `undefined` to use the interface
581616
* gcs, `null` to use the map gcs, or any other transform. If setting the
@@ -588,10 +623,11 @@ var map = function (arg) {
588623
* view. When `'limited'`, the `clampBoundsX` and `clampBoundsY` options
589624
* are selectively enforced so that the map will not end up more out of
590625
* bounds than its current state.
626+
* @param {boolean} [noTrigger] If truthy, do not trigger a pan event.
591627
* @returns {geo.geoPosition|this}
592628
* @fires geo.event.pan
593629
*/
594-
this.center = function (coordinates, gcs, ignoreDiscreteZoom, ignoreClampBounds) {
630+
this.center = function (coordinates, gcs, ignoreDiscreteZoom, ignoreClampBounds, noTrigger) {
595631
var center;
596632
if (coordinates === undefined) {
597633
center = Object.assign({}, m_this.worldToGcs(m_center, gcs));
@@ -606,9 +642,11 @@ var map = function (arg) {
606642
ignoreClampBounds), m_rotation);
607643
m_this.modified();
608644
// trigger a pan event
609-
m_this.geoTrigger(geo_event.pan, {
610-
screenDelta: {x: 0, y: 0}
611-
});
645+
if (!noTrigger) {
646+
m_this.geoTrigger(geo_event.pan, {
647+
screenDelta: {x: 0, y: 0}
648+
});
649+
}
612650
return m_this;
613651
};
614652

@@ -1383,7 +1421,7 @@ var map = function (arg) {
13831421

13841422
// This might have consequences in terms of bounds/zoom clamping.
13851423
// What behavior do we expect from this method in that case?
1386-
m_this.zoom(nav.zoom);
1424+
m_this.zoom(nav.zoom, undefined, undefined, undefined, 'pan');
13871425
m_this.center(nav.center, null);
13881426
}
13891427

tests/cases/map.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,16 @@ describe('geo.core.map', function () {
421421
m = createMap({autoshareRenderer: 'more'});
422422
expect(m.autoshareRenderer()).toBe('more');
423423
});
424+
it('zoomAndCenter and center', function () {
425+
var m = createMap(undefined, {width: '500px', height: '500px'});
426+
expect(m.zoom()).toBe(4);
427+
m.zoomAndCenter(3.5, {x: 4, y: 2});
428+
expect(m.zoom()).toBe(3.5);
429+
expect(closeToEqual(m.center(), {x: 4, y: 2, z: 0})).toBe(true);
430+
m.center({x: 0, y: 0});
431+
expect(m.zoom()).toBe(3.5);
432+
expect(closeToEqual(m.center(), {x: 0, y: 0, z: 0})).toBe(true);
433+
});
424434
});
425435

426436
describe('Public utility methods', function () {

0 commit comments

Comments
 (0)