Skip to content

Commit

Permalink
feat(AgmMap): recentering for triggerResize
Browse files Browse the repository at this point in the history
When you call the triggerResize method of AgmMap, the map gets recentered
automaticaly.

Closes sebholstein#789
Closes sebholstein#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).
  • Loading branch information
sebholstein authored and franknwankwo committed Jun 7, 2017
1 parent 4b37831 commit 87fdf22
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/core/directives/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,22 @@ export class AgmMap implements OnChanges, OnInit, OnDestroy {

/**
* Triggers a resize event on the google map instance.
* When recenter is true, the of the google map gets called with the current lat/lng values or fitBounds value to recenter the map.
* Returns a promise that gets resolved after the event was triggered.
*/
triggerResize(): Promise<void> {
triggerResize(recenter: boolean = true): Promise<void> {
// Note: When we would trigger the resize event and show the map in the same turn (which is a
// common case for triggering a resize event), then the resize event would not
// work (to show the map), so we trigger the event in a timeout.
return new Promise<void>((resolve) => {
setTimeout(
() => { return this._mapsWrapper.triggerMapEvent('resize').then(() => resolve()); });
setTimeout(() => {
return this._mapsWrapper.triggerMapEvent('resize').then(() => {
if (recenter) {
this.fitBounds != null ? this._fitBounds() : this._setCenter();
}
resolve();
});
});
});
}

Expand All @@ -423,6 +430,10 @@ export class AgmMap implements OnChanges, OnInit, OnDestroy {
if (typeof this.latitude !== 'number' || typeof this.longitude !== 'number') {
return;
}
this._setCenter();
}

private _setCenter() {
let newCenter = {
lat: this.latitude,
lng: this.longitude,
Expand Down

0 comments on commit 87fdf22

Please sign in to comment.