@@ -3,7 +3,6 @@ import { Subscription, Subject } from 'rxjs';
33import { distinctUntilChanged , takeUntil } from 'rxjs/operators' ;
44
55import { 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