From b746ca3a3424338952dbb3b3e5f3cee0ae7ab019 Mon Sep 17 00:00:00 2001 From: Ephraim Khantsis Date: Mon, 26 Aug 2019 10:39:50 -0400 Subject: [PATCH] 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 --- packages/core/directives/fit-bounds.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/core/directives/fit-bounds.ts b/packages/core/directives/fit-bounds.ts index 074f3e2db..d49033bc4 100644 --- a/packages/core/directives/fit-bounds.ts +++ b/packages/core/directives/fit-bounds.ts @@ -3,7 +3,6 @@ import { Subscription, Subject } from 'rxjs'; import { distinctUntilChanged, takeUntil } from 'rxjs/operators'; import { FitBoundsService, FitBoundsAccessor, FitBoundsDetails } from '../services/fit-bounds'; -import { LatLng, LatLngLiteral } from '../services/google-maps-types'; /** * 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 { .subscribe(details => this._updateBounds(details)); } + /* + Either the location changed, or visible status changed. + Possible state changes are + invisible -> visible + visible -> invisible + visible -> visible (new location) + */ private _updateBounds(newFitBoundsDetails?: FitBoundsDetails) { + // either visibility will change, or location, so remove the old one anyway + if (this._latestFitBoundsDetails) { + this._fitBoundsService.removeFromBounds(this._latestFitBoundsDetails.latLng); + // don't set latestFitBoundsDetails to null, because we can toggle visibility from + // true -> false -> true, in which case we still need old value cached here + } + if (newFitBoundsDetails) { this._latestFitBoundsDetails = newFitBoundsDetails; } if (!this._latestFitBoundsDetails) { return; } - if (this.agmFitBounds) { + if (this.agmFitBounds === true) { this._fitBoundsService.addToBounds(this._latestFitBoundsDetails.latLng); - } else { - this._fitBoundsService.removeFromBounds(this._latestFitBoundsDetails.latLng); } }