Skip to content

Commit

Permalink
fix(FitBounds): remove old location on loc change (#1710)
Browse files Browse the repository at this point in the history
when a FitBoundsAccessor emits new bounds,  remove the old ones from
the map's bounds array

fixes: #1512
  • Loading branch information
doom777 committed Aug 26, 2019
1 parent b70cbbc commit b746ca3
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/core/directives/fit-bounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit b746ca3

Please sign in to comment.