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

Commit b746ca3

Browse files
authored
fix(FitBounds): remove old location on loc change (#1710)
when a FitBoundsAccessor emits new bounds, remove the old ones from the map's bounds array fixes: #1512
1 parent b70cbbc commit b746ca3

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

packages/core/directives/fit-bounds.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Subscription, Subject } from 'rxjs';
33
import { distinctUntilChanged, takeUntil } from 'rxjs/operators';
44

55
import { FitBoundsService, FitBoundsAccessor, FitBoundsDetails } from '../services/fit-bounds';
6-
import { LatLng, LatLngLiteral } from '../services/google-maps-types';
76

87
/**
98
* Adds the given directive to the auto fit bounds feature when the value is true.
@@ -53,17 +52,29 @@ export class AgmFitBounds implements OnInit, OnDestroy, OnChanges {
5352
.subscribe(details => this._updateBounds(details));
5453
}
5554

55+
/*
56+
Either the location changed, or visible status changed.
57+
Possible state changes are
58+
invisible -> visible
59+
visible -> invisible
60+
visible -> visible (new location)
61+
*/
5662
private _updateBounds(newFitBoundsDetails?: FitBoundsDetails) {
63+
// either visibility will change, or location, so remove the old one anyway
64+
if (this._latestFitBoundsDetails) {
65+
this._fitBoundsService.removeFromBounds(this._latestFitBoundsDetails.latLng);
66+
// don't set latestFitBoundsDetails to null, because we can toggle visibility from
67+
// true -> false -> true, in which case we still need old value cached here
68+
}
69+
5770
if (newFitBoundsDetails) {
5871
this._latestFitBoundsDetails = newFitBoundsDetails;
5972
}
6073
if (!this._latestFitBoundsDetails) {
6174
return;
6275
}
63-
if (this.agmFitBounds) {
76+
if (this.agmFitBounds === true) {
6477
this._fitBoundsService.addToBounds(this._latestFitBoundsDetails.latLng);
65-
} else {
66-
this._fitBoundsService.removeFromBounds(this._latestFitBoundsDetails.latLng);
6778
}
6879
}
6980

0 commit comments

Comments
 (0)