-
Notifications
You must be signed in to change notification settings - Fork 92
/
ac-map-layer-provider.component.ts
123 lines (109 loc) · 3.56 KB
/
ac-map-layer-provider.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { Component, OnInit, Input, OnDestroy, SimpleChanges, OnChanges } from '@angular/core';
import { CesiumService } from '../../services/cesium/cesium.service';
import { MapLayerProviderOptions } from '../../models/map-layer-provider-options.enum';
import { Checker } from '../../utils/checker';
/**
* This component is used for adding a map provider service to the map (ac-map).
* The component must be a child of <ac-map>.
*
* __Usage :__
* ```
* <ac-map-layer-provider [options]="optionsObject" [provider]="myProvider">
* </ac-map-layer-provider>
* ```
*/
@Component({
selector: 'ac-map-layer-provider',
template: '',
})
export class AcMapLayerProviderComponent implements OnInit, OnChanges, OnDestroy {
private static createWebMapServiceProvider(options) {
return new Cesium.WebMapServiceImageryProvider(options);
}
private static createWebMapTileServiceProvider(options) {
return new Cesium.WebMapTileServiceImageryProvider(options);
}
private static createArcGisMapServerProvider(options) {
return new Cesium.ArcGisMapServerImageryProvider(options);
}
private static createOfflineMapProvider() {
return Cesium.createTileMapServiceImageryProvider({
url: Cesium.buildModuleUrl('Assets/Textures/NaturalEarthII')
});
}
/**
* refer to cesium docs for details https://cesiumjs.org/Cesium/Build/Documentation/ImageryProvider.html
* @type {{Object}}
*/
@Input()
options: { url?: string } = {};
/**
* the provider
* @type {MapLayerProviderOptions}
*/
@Input()
provider: MapLayerProviderOptions = MapLayerProviderOptions.OFFLINE;
/**
* index (optional) - The index to add the layer at. If omitted, the layer will added on top of all existing layers.
* @type {Number}
*/
@Input()
index: Number;
/**
* show (optional) - Determines if the map layer is shown.
* @type {Boolean}
*/
@Input()
show = true;
private imageryLayer;
private imageryLayers;
private layerProvider;
constructor(private cesiumService: CesiumService) {
this.imageryLayers = this.cesiumService.getScene().imageryLayers;
}
ngOnInit() {
if (!Checker.present(this.options.url) && this.provider !== MapLayerProviderOptions.OFFLINE) {
throw new Error('options must have a url');
}
switch (this.provider) {
case MapLayerProviderOptions.WebMapService:
this.layerProvider = AcMapLayerProviderComponent.createWebMapServiceProvider(this.options);
break;
case MapLayerProviderOptions.WebMapTileService:
this.layerProvider = AcMapLayerProviderComponent.createWebMapTileServiceProvider(this.options);
break;
case MapLayerProviderOptions.ArcGisMapServer:
this.layerProvider = AcMapLayerProviderComponent.createArcGisMapServerProvider(this.options);
break;
case MapLayerProviderOptions.OFFLINE:
break;
default:
this.layerProvider = AcMapLayerProviderComponent.createOfflineMapProvider();
break;
}
if (this.show) {
this.imageryLayer = this.imageryLayers.addImageryProvider(this.layerProvider, this.index);
}
}
ngOnChanges(changes: SimpleChanges): void {
if (changes['show'] && !changes['show'].isFirstChange() ) {
const showValue = changes['show'].currentValue;
if (showValue) {
if (this.imageryLayer) {
this.imageryLayers.add(this.imageryLayer, this.index);
}
else {
this.imageryLayer = this.imageryLayers.addImageryProvider(this.layerProvider, this.index);
}
}
else if (this.imageryLayer) {
this.imageryLayers.remove(this.imageryLayer, false);
}
}
}
ngOnDestroy(): void {
if (this.imageryLayer) {
this.imageryLayers.remove(this.imageryLayer, true);
}
}
}