Skip to content

Commit 0b09d5a

Browse files
committed
feat: Add a zoomAndCenter method
This is more efficient than making two calls, because is prevents triggering multiple times. This also adds noTrigger options to zoom, pan, and center calls.
1 parent 9174d2a commit 0b09d5a

File tree

1 file changed

+49
-12
lines changed

1 file changed

+49
-12
lines changed

src/map.js

Lines changed: 49 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,48 @@ 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+
};
488+
458489
/**
459490
* Pan the map by a number of display pixels.
460491
*
@@ -468,10 +499,11 @@ var map = function (arg) {
468499
* view. When `'limited'`, the `clampBoundsX` and `clampBoundsY` options
469500
* are selectively enforced so that the map will not end up more out of
470501
* bounds than its current state.
502+
* @param {boolean} [noTrigger] If truthy, do not trigger a pan event.
471503
* @returns {this}
472504
* @fires geo.event.pan
473505
*/
474-
this.pan = function (delta, ignoreDiscreteZoom, ignoreClampBounds) {
506+
this.pan = function (delta, ignoreDiscreteZoom, ignoreClampBounds, noTrigger) {
475507
var evt = {
476508
screenDelta: delta
477509
};
@@ -510,7 +542,9 @@ var map = function (arg) {
510542
y: m_height / 2
511543
});
512544

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

515549
m_this.modified();
516550
return m_this;
@@ -575,7 +609,7 @@ var map = function (arg) {
575609
/**
576610
* Get or set the center of the map in the given geographic coordinates.
577611
*
578-
* @param {geo.geoPosition} coordinates If specified, the new center of the
612+
* @param {geo.geoPosition} [coordinates] If specified, the new center of the
579613
* map.
580614
* @param {string|geo.transform|null} [gcs] `undefined` to use the interface
581615
* gcs, `null` to use the map gcs, or any other transform. If setting the
@@ -588,10 +622,11 @@ var map = function (arg) {
588622
* view. When `'limited'`, the `clampBoundsX` and `clampBoundsY` options
589623
* are selectively enforced so that the map will not end up more out of
590624
* bounds than its current state.
625+
* @param {boolean} [noTrigger] If truthy, do not trigger a pan event.
591626
* @returns {geo.geoPosition|this}
592627
* @fires geo.event.pan
593628
*/
594-
this.center = function (coordinates, gcs, ignoreDiscreteZoom, ignoreClampBounds) {
629+
this.center = function (coordinates, gcs, ignoreDiscreteZoom, ignoreClampBounds, noTrigger) {
595630
var center;
596631
if (coordinates === undefined) {
597632
center = Object.assign({}, m_this.worldToGcs(m_center, gcs));
@@ -606,9 +641,11 @@ var map = function (arg) {
606641
ignoreClampBounds), m_rotation);
607642
m_this.modified();
608643
// trigger a pan event
609-
m_this.geoTrigger(geo_event.pan, {
610-
screenDelta: {x: 0, y: 0}
611-
});
644+
if (!noTrigger) {
645+
m_this.geoTrigger(geo_event.pan, {
646+
screenDelta: {x: 0, y: 0}
647+
});
648+
}
612649
return m_this;
613650
};
614651

@@ -1383,7 +1420,7 @@ var map = function (arg) {
13831420

13841421
// This might have consequences in terms of bounds/zoom clamping.
13851422
// What behavior do we expect from this method in that case?
1386-
m_this.zoom(nav.zoom);
1423+
m_this.zoom(nav.zoom, undefined, undefined, undefined, 'pan');
13871424
m_this.center(nav.center, null);
13881425
}
13891426

0 commit comments

Comments
 (0)