-
Notifications
You must be signed in to change notification settings - Fork 817
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc042ae
commit a6efee2
Showing
9 changed files
with
214 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Directive, OnInit, Self, OnDestroy, Input, OnChanges, SimpleChanges } from '@angular/core'; | ||
import { FitBoundsService, FitBoundsAccessor, FitBoundsDetails } from '../services/fit-bounds'; | ||
import { Subscription } from 'rxjs/Subscription'; | ||
import { distinctUntilChanged } from 'rxjs/operators'; | ||
import { LatLng, LatLngLiteral } from '@agm/core'; | ||
|
||
/** | ||
* TODO: docs | ||
*/ | ||
@Directive({ | ||
selector: '[agmFitBounds]' | ||
}) | ||
export class AgmFitBounds implements OnInit, OnDestroy, OnChanges { | ||
/** | ||
* If the value is true, the element gets added to the bounds of the map. | ||
* Default: true. | ||
*/ | ||
@Input() agmFitBounds: boolean = true; | ||
|
||
private _subscription: Subscription; | ||
private _latestFitBoundsDetails: FitBoundsDetails | null = null; | ||
|
||
constructor( | ||
@Self() private readonly _fitBoundsAccessor: FitBoundsAccessor, | ||
private readonly _fitBoundsService: FitBoundsService | ||
) {} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
ngOnChanges(changes: SimpleChanges) { | ||
this._updateBounds(); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
ngOnInit() { | ||
this._subscription = this._fitBoundsAccessor | ||
.getFitBoundsDetails$() | ||
.pipe( | ||
distinctUntilChanged( | ||
(x: FitBoundsDetails, y: FitBoundsDetails) => | ||
x.latLng.lat === y.latLng.lng | ||
) | ||
) | ||
.subscribe(details => this._updateBounds(details)); | ||
} | ||
|
||
private _updateBounds(newFitBoundsDetails?: FitBoundsDetails) { | ||
if (newFitBoundsDetails) { | ||
this._latestFitBoundsDetails = newFitBoundsDetails; | ||
} | ||
if (!this._latestFitBoundsDetails) { | ||
return; | ||
} | ||
if (this.agmFitBounds) { | ||
this._fitBoundsService.addToBounds(this._latestFitBoundsDetails.latLng); | ||
} else { | ||
this._fitBoundsService.removeFromBounds(this._latestFitBoundsDetails.latLng); | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
ngOnDestroy() { | ||
if (this._subscription) { | ||
this._subscription.unsubscribe(); | ||
} | ||
if (this._latestFitBoundsDetails !== null) { | ||
this._fitBoundsService.removeFromBounds(this._latestFitBoundsDetails.latLng); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { BehaviorSubject, Observable, from } from 'rxjs'; | ||
import { flatMap, map, skipWhile } from 'rxjs/operators'; | ||
import { LatLng, LatLngBounds, LatLngLiteral } from './google-maps-types'; | ||
import { MapsAPILoader } from './maps-api-loader/maps-api-loader'; | ||
|
||
declare var google: any; | ||
|
||
export interface FitBoundsDetails { | ||
latLng: LatLng|LatLngLiteral; | ||
} | ||
|
||
export abstract class FitBoundsAccessor { | ||
abstract getFitBoundsDetails$(): Observable<FitBoundsDetails>; | ||
} | ||
|
||
@Injectable() | ||
export class FitBoundsService { | ||
readonly bounds$: Observable<LatLngBounds>; | ||
protected readonly _boundsChangeDebounceTime$: BehaviorSubject<number> = new BehaviorSubject<number>(200); | ||
protected readonly _includeInBounds$: BehaviorSubject<Map<string, LatLng | LatLngLiteral>> = new BehaviorSubject<Map<string, LatLng | LatLngLiteral>>(new Map<string, LatLng | LatLngLiteral>()); | ||
protected _emitPaused: boolean = false; | ||
|
||
constructor(loader: MapsAPILoader) { | ||
this.bounds$ = from(loader.load()).pipe( | ||
flatMap(() => this._includeInBounds$), | ||
skipWhile(() => this._emitPaused), | ||
// debounce(() => this._boundsChangeDebounceTime$), | ||
map((includeInBounds: Map<string, LatLng | LatLngLiteral>) => { | ||
const bounds = new google.maps.LatLngBounds() as LatLngBounds; | ||
includeInBounds.forEach(b => bounds.extend(b)); | ||
return bounds; | ||
}) | ||
); | ||
} | ||
|
||
addToBounds(latLng: LatLng | LatLngLiteral) { | ||
const id = this._createIdentifier(latLng); | ||
if (this._includeInBounds$.value.has(id)) { | ||
return; | ||
} | ||
const map = this._includeInBounds$.value; | ||
map.set(id, latLng); | ||
this._includeInBounds$.next(map); | ||
} | ||
|
||
removeFromBounds(latLng: LatLng|LatLngLiteral) { | ||
const map = this._includeInBounds$.value; | ||
map.delete(this._createIdentifier(latLng)); | ||
this._includeInBounds$.next(map); | ||
} | ||
|
||
changeFitBoundsDebounceTime(timeMs: number) { | ||
this._boundsChangeDebounceTime$.next(timeMs); | ||
} | ||
|
||
protected _createIdentifier(latLng: LatLng|LatLngLiteral): string { | ||
return `${latLng.lat}+${latLng.lng}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters