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

Commit 29e4ebb

Browse files
chrenedoom777
authored andcommitted
feat(AgmMap): add optional fit bounds padding
add optional `fitBoundsPadding` property that when defined will be forwarded to the native maps `fitBounds` method. fixes: #1660 #1471
1 parent 74ceb2f commit 29e4ebb

4 files changed

Lines changed: 25 additions & 13 deletions

File tree

packages/core/directives/map.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { FitBoundsService } from '../services/fit-bounds';
66
import { GoogleMapsAPIWrapper } from '../services/google-maps-api-wrapper';
77
import {
88
FullscreenControlOptions, LatLng, LatLngBounds, LatLngBoundsLiteral, LatLngLiteral,
9-
MapRestriction, MapTypeControlOptions, MapTypeId, MapTypeStyle, PanControlOptions,
9+
MapRestriction, MapTypeControlOptions, MapTypeId, MapTypeStyle, Padding, PanControlOptions,
1010
RotateControlOptions, ScaleControlOptions, StreetViewControlOptions, ZoomControlOptions,
1111
} from '../services/google-maps-types';
1212
import { CircleManager } from '../services/managers/circle-manager';
@@ -204,6 +204,11 @@ export class AgmMap implements OnChanges, OnInit, OnDestroy {
204204
*/
205205
@Input() fitBounds: LatLngBoundsLiteral | LatLngBounds | boolean = false;
206206

207+
/**
208+
* Padding amount for the bounds.
209+
*/
210+
@Input() fitBoundsPadding: number | Padding;
211+
207212
/**
208213
* The initial enabled/disabled state of the Scale control. This is disabled by default.
209214
*/
@@ -521,19 +526,19 @@ export class AgmMap implements OnChanges, OnInit, OnDestroy {
521526
}
522527
break;
523528
default:
524-
this._updateBounds(this.fitBounds);
529+
this._updateBounds(this.fitBounds, this.fitBoundsPadding);
525530
}
526531
}
527532

528533
private _subscribeToFitBoundsUpdates() {
529534
this._zone.runOutsideAngular(() => {
530535
this._fitBoundsSubscription = this._fitBoundsService.getBounds$().subscribe(b => {
531-
this._zone.run(() => this._updateBounds(b));
536+
this._zone.run(() => this._updateBounds(b, this.fitBoundsPadding));
532537
});
533538
});
534539
}
535540

536-
protected _updateBounds(bounds: LatLngBounds | LatLngBoundsLiteral) {
541+
protected _updateBounds(bounds: LatLngBounds | LatLngBoundsLiteral, padding?: number | Padding) {
537542
if (!bounds) {
538543
return;
539544
}
@@ -543,10 +548,10 @@ export class AgmMap implements OnChanges, OnInit, OnDestroy {
543548
bounds = newBounds;
544549
}
545550
if (this.usePanning) {
546-
this._mapsWrapper.panToBounds(bounds);
551+
this._mapsWrapper.panToBounds(bounds, padding);
547552
return;
548553
}
549-
this._mapsWrapper.fitBounds(bounds);
554+
this._mapsWrapper.fitBounds(bounds, padding);
550555
}
551556

552557
private _isLatLngBoundsLiteral(bounds: LatLngBounds | LatLngBoundsLiteral): bounds is LatLngBoundsLiteral {

packages/core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export * from './services';
44
export * from './map-types';
55

66
// Google Maps types
7-
export { LatLngBounds, LatLng, LatLngLiteral, MapTypeStyle, PolyMouseEvent } from './services/google-maps-types';
7+
export { LatLngBounds, LatLng, LatLngLiteral, MapTypeStyle, Padding, PolyMouseEvent } from './services/google-maps-types';
88

99
// core module
1010
// we explicitly export the module here to prevent this Ionic 2 bug:

packages/core/services/google-maps-api-wrapper.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,12 @@ export class GoogleMapsAPIWrapper {
180180
return this._map.then((map) => map.panBy(x, y));
181181
}
182182

183-
fitBounds(latLng: mapTypes.LatLngBounds | mapTypes.LatLngBoundsLiteral): Promise<void> {
184-
return this._map.then((map) => map.fitBounds(latLng));
183+
fitBounds(latLng: mapTypes.LatLngBounds | mapTypes.LatLngBoundsLiteral, padding?: number | mapTypes.Padding): Promise<void> {
184+
return this._map.then((map) => map.fitBounds(latLng, padding));
185185
}
186186

187-
panToBounds(latLng: mapTypes.LatLngBounds | mapTypes.LatLngBoundsLiteral): Promise<void> {
188-
return this._map.then((map) => map.panToBounds(latLng));
187+
panToBounds(latLng: mapTypes.LatLngBounds | mapTypes.LatLngBoundsLiteral, padding?: number | mapTypes.Padding): Promise<void> {
188+
return this._map.then((map) => map.panToBounds(latLng, padding));
189189
}
190190

191191
/**

packages/core/services/google-maps-types.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export interface GoogleMap extends MVCObject {
3434
getMapTypeId(): MapTypeId;
3535
getZoom(): number;
3636
setOptions(options: MapOptions): void;
37-
panToBounds(latLngBounds: LatLngBounds | LatLngBoundsLiteral): void;
38-
fitBounds(bounds: LatLngBounds | LatLngBoundsLiteral): void;
37+
panToBounds(latLngBounds: LatLngBounds | LatLngBoundsLiteral, padding?: number | Padding): void;
38+
fitBounds(bounds: LatLngBounds | LatLngBoundsLiteral, padding?: number | Padding): void;
3939
}
4040

4141
export interface LatLng {
@@ -162,6 +162,13 @@ export interface LatLngBounds {
162162
union(other: LatLngBounds | LatLngBoundsLiteral): LatLngBounds;
163163
}
164164

165+
export interface Padding {
166+
top: number;
167+
left: number;
168+
right: number;
169+
bottom: number;
170+
}
171+
165172
export interface LatLngBoundsLiteral {
166173
east: number;
167174
north: number;

0 commit comments

Comments
 (0)