Skip to content

Commit da7664a

Browse files
committed
fix(map): support number in addition to [number] for zoom, ptich, bearing (fix #433)
1 parent d88458b commit da7664a

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Display a map:
103103
import { Component } from '@angular/core';
104104

105105
@Component({
106-
template: `<mgl-map [style]="'mapbox://styles/mapbox/streets-v12'" [zoom]="[9]" [center]="[-74.5, 40]" />`,
106+
template: `<mgl-map [style]="'mapbox://styles/mapbox/streets-v12'" [zoom]="9" [center]="[-74.5, 40]" />`,
107107
styles: [
108108
`
109109
mgl-map {

apps/showcase/src/app/demo/examples/simple-map.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { MapComponent } from 'ngx-mapbox-gl';
33

44
@Component({
55
selector: 'showcase-demo',
6-
template: `<mgl-map [zoom]="[9]" [center]="[-74.5, 40]" />`,
6+
template: `<mgl-map [zoom]="9" [center]="[-74.5, 40]" />`,
77
imports: [MapComponent],
88
styleUrls: ['./examples.css'],
99
})

docs/API.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ Dynamic:
5858
- **style**: `Style | string`
5959
- **center**: `LngLatLike`
6060
- **maxBounds**: [`LngLatBoundsLike`](https://docs.mapbox.com/mapbox-gl-js/api/geography/#lnglatboundslike)
61-
- **zoom**: `[number]` The initial zoom level of the map. If zoom is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to 0. [ngx] It's an array in order to be able to force changes (by changing the array ref) because this input can be desync after user manipulation on map.
62-
- **bearing**: `[number]` The initial bearing (rotation) of the map, measured in degrees counter-clockwise from north. If bearing is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to 0. [ngx] It's an array in order to be able to force changes (by changing the array ref) because this input can be desync after user manipulation on map.
63-
- **pitch**: `[number]` The initial pitch (tilt) of the map, measured in degrees away from the plane of the screen (0-60). If pitch is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to 0. [ngx] It's an array in order to be able to force changes (by changing the array ref) because this input can be desync after user manipulation on map.
61+
- **zoom**: `[number] | number` The initial zoom level of the map. If zoom is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to 0. [ngx] It can be an array in order to be able to force changes (by changing the array ref) because this input can be desync after user manipulation on map.
62+
- **bearing**: `[number] | number` The initial bearing (rotation) of the map, measured in degrees counter-clockwise from north. If bearing is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to 0. [ngx] It can be an array in order to be able to force changes (by changing the array ref) because this input can be desync after user manipulation on map.
63+
- **pitch**: `[number] | number` The initial pitch (tilt) of the map, measured in degrees away from the plane of the screen (0-60). If pitch is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to 0. [ngx] It can be an array in order to be able to force changes (by changing the array ref) because this input can be desync after user manipulation on map.
6464
- **fitBoundsOptions** https://docs.mapbox.com/mapbox-gl-js/api/map/#map#fitbounds
6565
- **projection** https://docs.mapbox.com/mapbox-gl-js/api/map/#map#setprojection
6666
- [ngx] **fitBounds**: [`LngLatBoundsLike`](https://docs.mapbox.com/mapbox-gl-js/api/geography/#lnglatboundslike) If set, the map will center on the given coordinates. Dynamic version of **bounds**.

libs/ngx-mapbox-gl/src/lib/map/map.component.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ export class MapComponent implements OnChanges, OnDestroy {
9393
style = input<MapOptions['style']>();
9494
center = input<MapOptions['center']>();
9595
maxBounds = input<MapOptions['maxBounds']>();
96-
zoom = input<[number]>();
97-
bearing = input<[number]>();
98-
pitch = input<[number]>();
96+
zoom = input<[number] | number>();
97+
bearing = input<[number] | number>();
98+
pitch = input<[number] | number>();
9999
// First value goes to options.fitBoundsOptions. Subsequents changes are passed to fitBounds
100100
fitBoundsOptions = input<MapOptions['fitBoundsOptions']>();
101101
renderWorldCopies = input<MapOptions['renderWorldCopies']>();
@@ -319,9 +319,10 @@ export class MapComponent implements OnChanges, OnDestroy {
319319
'[ngx-mapbox-gl] center / zoom / pitch / fitBounds inputs are being overridden by fitScreenCoordinates input',
320320
);
321321
}
322+
const bearing = this.bearing();
322323
this.mapService.fitScreenCoordinates(
323324
changes['fitScreenCoordinates'].currentValue,
324-
this.bearing() ? this.bearing()![0] : 0,
325+
Array.isArray(bearing) ? bearing[0] : bearing || 0,
325326
this.movingOptions(),
326327
);
327328
}
@@ -342,13 +343,29 @@ export class MapComponent implements OnChanges, OnDestroy {
342343
!changes['fitScreenCoordinates']) ||
343344
(changes['pitch'] && !changes['pitch'].isFirstChange())
344345
) {
346+
const zoom = this.zoom();
347+
const center = this.center();
348+
const bearing = this.bearing();
349+
const pitch = this.pitch();
345350
this.mapService.move(
346351
this.movingMethod(),
347352
this.movingOptions(),
348-
changes['zoom'] && this.zoom() ? this.zoom()![0] : undefined,
349-
changes['center'] ? this.center() : undefined,
350-
changes['bearing'] && this.bearing()! ? this.bearing()![0] : undefined,
351-
changes['pitch'] && this.pitch() ? this.pitch()![0] : undefined,
353+
changes['zoom'] && zoom
354+
? Array.isArray(zoom)
355+
? zoom[0]
356+
: zoom
357+
: undefined,
358+
changes['center'] ? center : undefined,
359+
changes['bearing'] && bearing!
360+
? Array.isArray(bearing)
361+
? bearing[0]
362+
: bearing
363+
: undefined,
364+
changes['pitch'] && pitch
365+
? Array.isArray(pitch)
366+
? pitch[0]
367+
: pitch
368+
: undefined,
352369
);
353370
}
354371
}

libs/ngx-mapbox-gl/src/lib/map/map.service.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ export interface SetupMap {
3535
accessToken?: string;
3636
mapOptions: Omit<MapOptions, 'bearing' | 'pitch' | 'zoom'> & {
3737
/**
38-
* NOTE: Thoses are arrays in order to be able to imperatively change them if the map is moved manually
39-
* TODO: Move thoses to model() maybe
38+
* NOTE: Thoses can be arrays in order to be able to imperatively change them if the map is moved manually
4039
*/
41-
bearing?: [number];
42-
pitch?: [number];
43-
zoom?: [number];
40+
bearing?: [number] | number;
41+
pitch?: [number] | number;
42+
zoom?: [number] | number;
4443
};
4544
mapEvents: NgxMapEvent;
4645
}
@@ -110,9 +109,15 @@ export class MapService {
110109
setup(options: SetupMap) {
111110
const mapOptions = {
112111
...options.mapOptions,
113-
bearing: options.mapOptions.bearing?.[0],
114-
zoom: options.mapOptions.zoom?.[0],
115-
pitch: options.mapOptions.pitch?.[0],
112+
bearing: Array.isArray(options.mapOptions.bearing)
113+
? options.mapOptions.bearing[0]
114+
: options.mapOptions.bearing,
115+
zoom: Array.isArray(options.mapOptions.zoom)
116+
? options.mapOptions.zoom[0]
117+
: options.mapOptions.zoom,
118+
pitch: Array.isArray(options.mapOptions.pitch)
119+
? options.mapOptions.pitch[0]
120+
: options.mapOptions.pitch,
116121
accessToken: options.accessToken || this.MAPBOX_API_KEY || '',
117122
};
118123
this.createMap(mapOptions);

0 commit comments

Comments
 (0)