Skip to content

Commit 7c87bb2

Browse files
feat(layer type): added property type to layer class that returns the GeoJSON layer type
In order to get more information about the type of a layer (e.g. obtained by calling `getLayers`), the GeoJSON layer type was added as property to the layer class. This is for example helpful to find the first symbol layer to add a polyline layer under all map annotations.
1 parent e6c89b9 commit 7c87bb2

File tree

4 files changed

+85
-7
lines changed

4 files changed

+85
-7
lines changed

src/ui-mapbox/common.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,19 @@ export interface AddGeoJsonClusteredOptions {
263263
clusters?: MapboxCluster[];
264264
}
265265

266+
// ------------------------------------------------------------
267+
268+
export type LayerType = "fill" | "line" | "symbol" | "circle" | "heatmap" | "fill-extrusion" | "raster" | "hillshade" | "background" | "sky"
269+
270+
export type SupportedLayerType = LayerType & ("line" | "circle" | "fill" | "symbol" | "raster")
271+
272+
// ------------------------------------------------------------
273+
266274
export interface AddLayerOptions {
267275
id: string;
268276
source: string;
269277
sourceLayer: string;
270-
type: string;
278+
type: SupportedLayerType;
271279

272280
/**
273281
* 'circle' paint properties
@@ -553,6 +561,7 @@ export interface LayerCommon {
553561
getNativeInstance(): any;
554562
setFilter(filter: any[]): void;
555563
getFilter(): any[];
564+
type(): LayerType;
556565
}
557566

558567
// ------------------------------------------------------------

src/ui-mapbox/layers/layer-factory.android.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LayerCommon } from '../common';
1+
import { LayerCommon, LayerType } from "../common"
22
import { ExpressionParser } from '../expression/expression-parser';
33
import { PropertyParser } from './parser/property-parser';
44

@@ -43,6 +43,40 @@ export class Layer implements LayerCommon {
4343
public getProperty(name: string): any {
4444
return PropertyParser.propertyValueFromLayer(this.instance, name);
4545
}
46+
47+
public type(): LayerType {
48+
if (this.instance instanceof com.mapbox.mapboxsdk.style.layers.FillLayer) {
49+
return "fill"
50+
}
51+
if (this.instance instanceof com.mapbox.mapboxsdk.style.layers.LineLayer) {
52+
return "line"
53+
}
54+
if (this.instance instanceof com.mapbox.mapboxsdk.style.layers.SymbolLayer) {
55+
return "symbol"
56+
}
57+
if (this.instance instanceof com.mapbox.mapboxsdk.style.layers.CircleLayer) {
58+
return "circle"
59+
}
60+
if (this.instance instanceof com.mapbox.mapboxsdk.style.layers.HeatmapLayer) {
61+
return "heatmap"
62+
}
63+
if (this.instance instanceof com.mapbox.mapboxsdk.style.layers.FillExtrusionLayer) {
64+
return "fill-extrusion"
65+
}
66+
if (this.instance instanceof com.mapbox.mapboxsdk.style.layers.RasterLayer) {
67+
return "raster"
68+
}
69+
if (this.instance instanceof com.mapbox.mapboxsdk.style.layers.HillshadeLayer) {
70+
return "hillshade"
71+
}
72+
if (this.instance instanceof com.mapbox.mapboxsdk.style.layers.BackgroundLayer) {
73+
return "background"
74+
}
75+
76+
// there is no sky layer in the Android Mapbox SDK
77+
78+
return null;
79+
}
4680
}
4781

4882
export class LayerFactory {

src/ui-mapbox/layers/layer-factory.d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LayerCommon } from '../common';
1+
import { LayerCommon, LayerType } from "../common"
22

33
declare class LayerFactory {
44
static createLayer(style, source): Promise<LayerCommon>;
@@ -16,4 +16,5 @@ export declare class Layer implements LayerCommon {
1616
getFilter(): any[];
1717
setProperty(name: string, value: any): void;
1818
getProperty(name: string): any;
19-
}
19+
type(): LayerType;
20+
}

src/ui-mapbox/layers/layer-factory.ios.ts

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LayerCommon } from '../common';
1+
import { LayerCommon, LayerType } from "../common"
22
import { ExpressionParser } from '../expression/expression-parser';
33
import { PropertyParser } from './parser/property-parser';
44

@@ -53,13 +53,47 @@ export class LayerFactory {
5353

5454
export class Layer implements LayerCommon {
5555
public id: string;
56-
private instance;
56+
private instance: MGLStyleLayer;
5757

58-
constructor(instance) {
58+
constructor(instance: MGLStyleLayer) {
5959
this.instance = instance;
6060
this.id = instance.identifier;
6161
}
6262

63+
type(): LayerType {
64+
if (this.instance instanceof MGLFillStyleLayer) {
65+
return "fill"
66+
}
67+
if (this.instance instanceof MGLLineStyleLayer) {
68+
return "line"
69+
}
70+
if (this.instance instanceof MGLSymbolStyleLayer) {
71+
return "symbol"
72+
}
73+
if (this.instance instanceof MGLCircleStyleLayer) {
74+
return "circle"
75+
}
76+
if (this.instance instanceof MGLHeatmapStyleLayer) {
77+
return "heatmap"
78+
}
79+
if (this.instance instanceof MGLFillExtrusionStyleLayer) {
80+
return "fill-extrusion"
81+
}
82+
if (this.instance instanceof MGLRasterStyleLayer) {
83+
return "raster"
84+
}
85+
if (this.instance instanceof MGLHillshadeStyleLayer) {
86+
return "hillshade"
87+
}
88+
if (this.instance instanceof MGLBackgroundStyleLayer) {
89+
return "background"
90+
}
91+
92+
// there is no sky layer in the Mapbox iOS SDK
93+
94+
return null;
95+
}
96+
6397
visibility(): boolean {
6498
return this.instance.visible;
6599
}

0 commit comments

Comments
 (0)