Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 74ceb2f

Browse files
loremapsdoom777
authored andcommitted
feat(FitBounds): add fitBounds to agm-polyline-point
Allow agm polyline point to allow autofitting around polylines. Closes #1693
1 parent 2bf73de commit 74ceb2f

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

packages/core/directives/polyline-point.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { SimpleChange, SimpleChanges } from '@angular/core';
2+
import { discardPeriodicTasks, fakeAsync, tick } from '@angular/core/testing';
3+
import { FitBoundsDetails } from '../services/fit-bounds';
24
import { AgmPolylinePoint } from './polyline-point';
35

46
describe('AgmPolylinePoint', () => {
@@ -45,5 +47,26 @@ describe('AgmPolylinePoint', () => {
4547
expect(polylinePoint.positionChanged.emit)
4648
.toHaveBeenCalledWith({lat: 'newLat', lng: 'newLng'});
4749
});
50+
it('should emit bounds on latitude and longitude change', fakeAsync(() => {
51+
const polylinePoint = new AgmPolylinePoint();
52+
polylinePoint.latitude = 50;
53+
polylinePoint.longitude = -50;
54+
55+
let value: FitBoundsDetails = null;
56+
polylinePoint.getFitBoundsDetails$().subscribe(details => value = details);
57+
58+
expect(value).toEqual({ latLng: {lat: 50, lng: -50} });
59+
60+
const positionChanges: SimpleChanges = {
61+
'latitude': new SimpleChange(50, 100, false),
62+
'longitude': new SimpleChange(-50, -100, false),
63+
};
64+
polylinePoint.ngOnChanges(positionChanges);
65+
66+
tick(1);
67+
expect(value).toEqual({ latLng: {lat: 100, lng: -100} });
68+
69+
discardPeriodicTasks();
70+
}));
4871
});
4972
});

packages/core/directives/polyline-point.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
import { Directive, EventEmitter, Input, OnChanges, Output, SimpleChanges } from '@angular/core';
1+
import { Directive, EventEmitter, forwardRef, Input, OnChanges, Output, SimpleChanges } from '@angular/core';
2+
import { Observable } from 'rxjs';
3+
import { map, startWith } from 'rxjs/operators';
24
import { LatLngLiteral } from '../../core/services/google-maps-types';
5+
import { FitBoundsAccessor, FitBoundsDetails } from '../services/fit-bounds';
36

47
/**
58
* AgmPolylinePoint represents one element of a polyline within a {@link
69
* AgmPolyline}
710
*/
8-
@Directive({selector: 'agm-polyline-point'})
9-
export class AgmPolylinePoint implements OnChanges {
11+
@Directive({
12+
selector: 'agm-polyline-point',
13+
providers: [
14+
{provide: FitBoundsAccessor, useExisting: forwardRef(() => AgmPolylinePoint)},
15+
],
16+
})
17+
export class AgmPolylinePoint implements OnChanges, FitBoundsAccessor {
1018
/**
1119
* The latitude position of the point.
1220
*/
@@ -33,4 +41,12 @@ export class AgmPolylinePoint implements OnChanges {
3341
this.positionChanged.emit(position);
3442
}
3543
}
44+
45+
/** @internal */
46+
getFitBoundsDetails$(): Observable<FitBoundsDetails> {
47+
return this.positionChanged.pipe(
48+
startWith({lat: this.latitude, lng: this.longitude}),
49+
map(position => ({latLng: position}))
50+
);
51+
}
3652
}

0 commit comments

Comments
 (0)