-
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.
This adds support for auto fitBounds features. Right now, only markers and custom components are supported. We'll add support for other core components later
- Loading branch information
1 parent
89b6e5c
commit 4d3103c
Showing
13 changed files
with
453 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
+++ | ||
date = "2018-09-22T09:31:00-01:00" | ||
draft = false | ||
title = "Enable auto fit bounds" | ||
|
||
+++ | ||
|
||
Angular Google Maps (AGM) has an auto fit bounds feature, that adds all containing components to the bounds of the map. | ||
To enable it, set the `fitBounds` input of `agm-map` to `true` and add the `agmFitBounds` input/directive to `true` for all components | ||
you want to include in the bounds of the map. | ||
|
||
```html | ||
<agm-map [fitBounds]="true"> | ||
<agm-marker [agmFitBounds]="true"></agm-marker> | ||
|
||
<!-- not included --> | ||
<agm-marker [agmFitBounds]="false"></agm-marker> | ||
</agm-map> | ||
``` |
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,54 @@ | ||
+++ | ||
date = "2018-09-22T09:31:00-01:00" | ||
draft = false | ||
title = "Support auto fit bounds for custom components" | ||
|
||
+++ | ||
|
||
Angular Google Maps (AGM) has an auto fit bounds feature, that adds all containing components to the bounds of the map: | ||
|
||
```html | ||
<agm-map [fitBounds]="true"> | ||
<agm-marker [agmFitBounds]="true"></agm-marker> | ||
</agm-map> | ||
``` | ||
|
||
Let`s say we have a custom component, that extends the features of AGM: | ||
|
||
|
||
```html | ||
<agm-map [fitBounds]="true"> | ||
<my-custom-component></my-custom-component> | ||
</agm-map> | ||
``` | ||
|
||
To add support the auto fit bounds feature for `<my-custom-component>`, we have to implement the `FitBoundsAccessor`: | ||
|
||
```typescript | ||
import { FitBoundsAccessor, FitBoundsDetails } from '@agm/core'; | ||
import { forwardRef, Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'my-custom-component', | ||
template: '', | ||
providers: [ | ||
{provide: FitBoundsAccessor, useExisting: forwardRef(() => MyCustomComponent)} | ||
], | ||
}) | ||
export class MyCustomComponent implements FitBoundsAccessor { | ||
** | ||
* This is a method you need to implement with your custom logic. | ||
*/ | ||
getFitBoundsDetails$(): Observable<FitBoundsDetails> { | ||
return ...; | ||
} | ||
} | ||
``` | ||
|
||
The last step is to change your template. Add the `agmFitBounds` input/directive and set the value to true: | ||
|
||
```html | ||
<agm-map [fitBounds]="true"> | ||
<my-custom-component [agmFitBounds]="true"></my-custom-component> | ||
</agm-map> | ||
``` |
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,78 @@ | ||
import { Directive, OnInit, Self, OnDestroy, Input, OnChanges, SimpleChanges } from '@angular/core'; | ||
import { FitBoundsService, FitBoundsAccessor, FitBoundsDetails } from '../services/fit-bounds'; | ||
import { Subscription, Subject } from 'rxjs'; | ||
import { distinctUntilChanged, takeUntil } from 'rxjs/operators'; | ||
import { LatLng, LatLngLiteral } from '@agm/core'; | ||
|
||
/** | ||
* Adds the given directive to the auto fit bounds feature when the value is true. | ||
* To make it work with you custom AGM component, you also have to implement the {@link FitBoundsAccessor} abstract class. | ||
* @example | ||
* <agm-marker [agmFitBounds]="true"></agm-marker> | ||
*/ | ||
@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 _destroyed$: Subject<void> = new Subject<void>(); | ||
private _latestFitBoundsDetails: FitBoundsDetails | null = null; | ||
|
||
constructor( | ||
@Self() private readonly _fitBoundsAccessor: FitBoundsAccessor, | ||
private readonly _fitBoundsService: FitBoundsService | ||
) {} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
ngOnChanges(changes: SimpleChanges) { | ||
this._updateBounds(); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
ngOnInit() { | ||
this._fitBoundsAccessor | ||
.getFitBoundsDetails$() | ||
.pipe( | ||
distinctUntilChanged( | ||
(x: FitBoundsDetails, y: FitBoundsDetails) => | ||
x.latLng.lat === y.latLng.lng | ||
), | ||
takeUntil(this._destroyed$) | ||
) | ||
.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() { | ||
this._destroyed$.next(); | ||
this._destroyed$.complete(); | ||
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
Oops, something went wrong.