Skip to content

Commit 99a5bb7

Browse files
author
Wykks
committed
fix(mglImage): re-add image when style change
fix #76
1 parent 0ef27a1 commit 99a5bb7

File tree

2 files changed

+53
-37
lines changed

2 files changed

+53
-37
lines changed

projects/ngx-mapbox-gl/src/lib/image/image.component.ts

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
Output,
1010
SimpleChanges
1111
} from '@angular/core';
12+
import { fromEvent, Subscription } from 'rxjs';
13+
import { filter, startWith, switchMap } from 'rxjs/operators';
1214
import { MapService } from '../map/map.service';
1315
import { MapImageData, MapImageOptions } from '../map/map.types';
1416

@@ -28,40 +30,22 @@ export class ImageComponent implements OnInit, OnDestroy, OnChanges {
2830
@Output() error = new EventEmitter<{ status: number }>();
2931
@Output() loaded = new EventEmitter<void>();
3032

31-
private imageAdded = false;
33+
private isAdded = false;
34+
private isAdding = false;
35+
private sub: Subscription;
3236

3337
constructor(
3438
private MapService: MapService,
3539
private zone: NgZone
3640
) { }
3741

3842
ngOnInit() {
39-
this.MapService.mapLoaded$.subscribe(async () => {
40-
if (this.data) {
41-
this.MapService.addImage(
42-
this.id,
43-
this.data,
44-
this.options
45-
);
46-
this.imageAdded = true;
47-
} else if (this.url) {
48-
try {
49-
await this.MapService.loadAndAddImage(
50-
this.id,
51-
this.url,
52-
this.options
53-
);
54-
this.imageAdded = true;
55-
this.zone.run(() => {
56-
this.loaded.emit();
57-
});
58-
} catch (error) {
59-
this.zone.run(() => {
60-
this.error.emit(error);
61-
});
62-
}
63-
}
64-
});
43+
this.sub = this.MapService.mapLoaded$.pipe(
44+
switchMap(() => fromEvent(<any>this.MapService.mapInstance, 'styledata').pipe(
45+
startWith(undefined),
46+
filter(() => !this.isAdding && !this.MapService.mapInstance.hasImage(this.id))
47+
)),
48+
).subscribe(() => this.init());
6549
}
6650

6751
ngOnChanges(changes: SimpleChanges) {
@@ -76,8 +60,41 @@ export class ImageComponent implements OnInit, OnDestroy, OnChanges {
7660
}
7761

7862
ngOnDestroy() {
79-
if (this.imageAdded) {
63+
if (this.isAdded) {
8064
this.MapService.removeImage(this.id);
8165
}
66+
if (this.sub) {
67+
this.sub.unsubscribe();
68+
}
69+
}
70+
71+
private async init() {
72+
this.isAdding = true;
73+
if (this.data) {
74+
this.MapService.addImage(
75+
this.id,
76+
this.data,
77+
this.options
78+
);
79+
this.isAdded = true;
80+
this.isAdding = false;
81+
} else if (this.url) {
82+
try {
83+
await this.MapService.loadAndAddImage(
84+
this.id,
85+
this.url,
86+
this.options
87+
);
88+
this.isAdded = true;
89+
this.isAdding = false;
90+
this.zone.run(() => {
91+
this.loaded.emit();
92+
});
93+
} catch (error) {
94+
this.zone.run(() => {
95+
this.error.emit(error);
96+
});
97+
}
98+
}
8299
}
83100
}

projects/ngx-mapbox-gl/src/lib/layer/layer.component.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
VideoSource
3434
} from 'mapbox-gl';
3535
import { fromEvent, Subscription } from 'rxjs';
36-
import { filter } from 'rxjs/operators';
36+
import { filter, mapTo, switchMap, startWith } from 'rxjs/operators';
3737
import { MapService } from '../map/map.service';
3838

3939
@Component({
@@ -69,14 +69,13 @@ export class LayerComponent implements OnInit, OnDestroy, OnChanges, Layer {
6969
) { }
7070

7171
ngOnInit() {
72-
this.MapService.mapLoaded$.subscribe(() => {
73-
this.init(true);
74-
this.sub = fromEvent(<any>this.MapService.mapInstance, 'styledata').pipe(
75-
filter(() => !this.MapService.mapInstance.getLayer(this.id))
76-
).subscribe(() => {
77-
this.init(false);
78-
});
79-
});
72+
this.sub = this.MapService.mapLoaded$.pipe(
73+
switchMap(() => fromEvent(<any>this.MapService.mapInstance, 'styledata').pipe(
74+
startWith(true),
75+
filter(() => !this.MapService.mapInstance.getLayer(this.id)),
76+
mapTo(false)
77+
)),
78+
).subscribe((bindEvents: boolean) => this.init(bindEvents));
8079
}
8180

8281
ngOnChanges(changes: SimpleChanges) {

0 commit comments

Comments
 (0)