Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit faea24d

Browse files
committed
feat(AgmMap): recentering for triggerResize
When you call the triggerResize method of AgmMap, the map gets recentered automaticaly. Closes #789 Closes #976 BREAKING CHANGES Recentering of the map after a triggerResize call is now the default behavoir. You can create the previous behavoir with triggerResize(false).
1 parent 0696e5f commit faea24d

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/core/directives/map.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,22 @@ export class AgmMap implements OnChanges, OnInit, OnDestroy {
395395

396396
/**
397397
* Triggers a resize event on the google map instance.
398+
* When recenter is true, the of the google map gets called with the current lat/lng values or fitBounds value to recenter the map.
398399
* Returns a promise that gets resolved after the event was triggered.
399400
*/
400-
triggerResize(): Promise<void> {
401+
triggerResize(recenter: boolean = true): Promise<void> {
401402
// Note: When we would trigger the resize event and show the map in the same turn (which is a
402403
// common case for triggering a resize event), then the resize event would not
403404
// work (to show the map), so we trigger the event in a timeout.
404405
return new Promise<void>((resolve) => {
405-
setTimeout(
406-
() => { return this._mapsWrapper.triggerMapEvent('resize').then(() => resolve()); });
406+
setTimeout(() => {
407+
return this._mapsWrapper.triggerMapEvent('resize').then(() => {
408+
if (recenter) {
409+
this.fitBounds != null ? this._fitBounds() : this._setCenter();
410+
}
411+
resolve();
412+
});
413+
});
407414
});
408415
}
409416

@@ -423,6 +430,10 @@ export class AgmMap implements OnChanges, OnInit, OnDestroy {
423430
if (typeof this.latitude !== 'number' || typeof this.longitude !== 'number') {
424431
return;
425432
}
433+
this._setCenter();
434+
}
435+
436+
private _setCenter() {
426437
let newCenter = {
427438
lat: this.latitude,
428439
lng: this.longitude,

0 commit comments

Comments
 (0)