Skip to content

Commit 0234881

Browse files
author
Wykks
committed
fix(map): implement fitBounds
Also add a demo for fitBounds
1 parent 8812e6c commit 0234881

6 files changed

+141
-10
lines changed

e2e/zoomto-linestring.e2e-spec.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { browser, element, by, ExpectedConditions as EC } from 'protractor';
2+
const PixelDiff = require('pixel-diff');
3+
const browserLogs = require('protractor-browser-logs');
4+
5+
describe('Zoomto Linestring', () => {
6+
let logs: any;
7+
8+
beforeEach(() => {
9+
logs = browserLogs(browser);
10+
});
11+
12+
afterEach(() => {
13+
return logs.verify();
14+
});
15+
16+
it('should zoom to linestring', async () => {
17+
await browser.get('/zoomto-linestring');
18+
const elm = element(by.tagName('canvas'));
19+
await browser.wait(EC.presenceOf(elm), 2000);
20+
const buttons = element.all(by.tagName('button'));
21+
await browser.sleep(4000);
22+
const screen1 = await browser.takeScreenshot();
23+
await buttons.get(0).click();
24+
await browser.sleep(4000);
25+
const screen2 = await browser.takeScreenshot();
26+
const result = new PixelDiff({
27+
imageA: new Buffer(screen1, 'base64'),
28+
imageB: new Buffer(screen2, 'base64')
29+
}).runSync();
30+
expect(result.differences).toBeGreaterThan(0);
31+
});
32+
});

src/app/demo/examples/language-switch.component.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,40 @@ import { Map } from 'mapbox-gl';
55
template: `
66
<mgl-map
77
style="mapbox://styles/mapbox/light-v9"
8-
[zoom]="2.9"
8+
[zoom]="[2.9]"
99
[center]="[16.05, 48]"
1010
(load)="map = $event"
1111
>
1212
<mgl-control>
1313
<button
1414
mat-raised-button
1515
(click)="changeLangTo('fr')"
16-
>French</button>
16+
>
17+
French
18+
</button>
1719
<button
1820
mat-raised-button
1921
(click)="changeLangTo('ru')"
20-
>Russian</button>
22+
>
23+
Russian
24+
</button>
2125
<button
2226
mat-raised-button
2327
(click)="changeLangTo('de')"
24-
>German</button>
28+
>
29+
German
30+
</button>
2531
<button
2632
mat-raised-button
2733
(click)="changeLangTo('es')"
28-
>Spanish</button>
34+
>
35+
Spanish
36+
</button>
2937
</mgl-control>
3038
</mgl-map>
3139
`,
32-
styleUrls: ['./examples.css', './toggle-layers.component.css']
40+
styleUrls: ['./examples.css'],
41+
preserveWhitespaces: false
3342
})
3443
export class LanguageSwitchComponent {
3544
map: Map;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { Component } from '@angular/core';
2+
import { LngLatBounds } from 'mapbox-gl';
3+
4+
@Component({
5+
template: `
6+
<mgl-map
7+
style="mapbox://styles/mapbox/light-v9"
8+
[zoom]="[12]"
9+
[center]="[-77.0214, 38.8970]"
10+
[fitBounds]="bounds"
11+
[fitBoundsOptions]="{
12+
padding: 20
13+
}"
14+
>
15+
<mgl-control>
16+
<button
17+
mat-raised-button
18+
(click)="zoomToBounds()"
19+
>
20+
Zoom to bounds
21+
</button>
22+
</mgl-control>
23+
<mgl-layer
24+
id="LineString"
25+
type="line"
26+
[source]="source"
27+
[paint]="{
28+
'line-color': '#BF93E4',
29+
'line-width': 5
30+
}"
31+
[layout]="{
32+
'line-join': 'round',
33+
'line-cap': 'round'
34+
}"
35+
></mgl-layer>
36+
</mgl-map>
37+
`,
38+
styleUrls: ['./examples.css'],
39+
preserveWhitespaces: false
40+
})
41+
export class ZoomtoLinestringComponent {
42+
bounds: LngLatBounds;
43+
44+
source = {
45+
type: 'geojson',
46+
data: {
47+
'type': 'FeatureCollection',
48+
'features': [{
49+
'type': 'Feature',
50+
'geometry': {
51+
'type': 'LineString',
52+
'properties': {},
53+
'coordinates': [
54+
[-77.0366048812866, 38.89873175227713],
55+
[-77.03364372253417, 38.89876515143842],
56+
[-77.03364372253417, 38.89549195896866],
57+
[-77.02982425689697, 38.89549195896866],
58+
[-77.02400922775269, 38.89387200688839],
59+
[-77.01519012451172, 38.891416957534204],
60+
[-77.01521158218382, 38.892068305429156],
61+
[-77.00813055038452, 38.892051604275686],
62+
[-77.00832366943358, 38.89143365883688],
63+
[-77.00818419456482, 38.89082405874451],
64+
[-77.00815200805664, 38.88989712255097]
65+
]
66+
}
67+
}]
68+
}
69+
};
70+
71+
zoomToBounds() {
72+
const coordinates = this.source.data.features[0].geometry.coordinates;
73+
74+
this.bounds = coordinates.reduce((bounds, coord) => {
75+
return bounds.extend(<any>coord);
76+
}, new LngLatBounds(coordinates[0], coordinates[0]));
77+
}
78+
}

src/app/demo/module.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { PopupComponent } from './examples/popup.component';
3333
import { SatelliteMapComponent } from './examples/satellite-map.component';
3434
import { SetStyleComponent } from './examples/set-style.component';
3535
import { ToggleLayersComponent } from './examples/toggle-layers.component';
36+
import { ZoomtoLinestringComponent } from './examples/zoomto-linestring.component';
3637
import { IndexComponent } from './index.component';
3738
import { LayoutComponent } from './layout/layout.component';
3839

@@ -78,7 +79,8 @@ export const demoRoutes: Routes = [
7879
{ path: 'center-on-symbol', component: CenterOnSymbolComponent, data: { label: 'Center the map on a clicked symbol', cat: Category.USER_INTERACTION } },
7980
{ path: 'ngx-drag-a-point', component: NgxDragAPointComponent, data: { label: '[NGX] Create a draggable point', cat: Category.USER_INTERACTION } },
8081
{ path: 'hover-styles', component: HoverStylesComponent, data: { label: 'Create a hover effect', cat: Category.USER_INTERACTION } },
81-
{ path: 'popup-on-click', component: PopupOnClickComponent, data: { label: 'Display a popup on click', cat: Category.CONTROLS_AND_OVERLAYS } }
82+
{ path: 'popup-on-click', component: PopupOnClickComponent, data: { label: 'Display a popup on click', cat: Category.CONTROLS_AND_OVERLAYS } },
83+
{ path: 'zoomto-linestring', component: ZoomtoLinestringComponent, data: { label: 'Fit to the bounds of a LineString', cat: Category.USER_INTERACTION } },
8284
]
8385
}
8486
];
@@ -128,7 +130,8 @@ export const demoRoutes: Routes = [
128130
CenterOnSymbolComponent,
129131
NgxDragAPointComponent,
130132
HoverStylesComponent,
131-
PopupOnClickComponent
133+
PopupOnClickComponent,
134+
ZoomtoLinestringComponent
132135
]
133136
})
134137
export class DemoModule { }

src/app/lib/map/map.component.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ export class MapComponent implements OnChanges, OnDestroy, AfterViewInit, MapEve
9696
linear?: boolean,
9797
easing?: Function,
9898
padding?: number | PaddingOptions,
99-
offset?: PointLike, maxZoom?: number
99+
offset?: PointLike,
100+
maxZoom?: number
100101
};
101102
@Input() flyToOptions?: FlyToOptions;
102103
@Input() centerWithPanTo?: boolean;
@@ -243,6 +244,9 @@ export class MapComponent implements OnChanges, OnDestroy, AfterViewInit, MapEve
243244
if (changes.maxBounds && !changes.maxBounds.isFirstChange()) {
244245
this.MapService.updateMaxBounds(changes.maxBounds.currentValue);
245246
}
247+
if (changes.fitBounds && !changes.fitBounds.isFirstChange()) {
248+
this.MapService.fitBounds(changes.fitBounds.currentValue, this.fitBoundsOptions);
249+
}
246250
if (
247251
this.centerWithPanTo &&
248252
changes.center && !changes.center.isFirstChange() &&
@@ -265,5 +269,4 @@ export class MapComponent implements OnChanges, OnDestroy, AfterViewInit, MapEve
265269
);
266270
}
267271
}
268-
269272
}

src/app/lib/map/map.service.ts

+6
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ export class MapService {
350350
});
351351
}
352352

353+
fitBounds(bounds: MapboxGl.LngLatBoundsLike, options?: any) {
354+
return this.zone.runOutsideAngular(() => {
355+
this.mapInstance.fitBounds(bounds, options);
356+
});
357+
}
358+
353359
private createMap(options: MapboxGl.MapboxOptions) {
354360
Object.keys(options)
355361
.forEach((key: string) => {

0 commit comments

Comments
 (0)