Skip to content

Commit a6dd1c2

Browse files
author
Chris Vietor
committed
fix: addSource/addLayer on android
1 parent 4b72713 commit a6dd1c2

File tree

3 files changed

+75
-80
lines changed

3 files changed

+75
-80
lines changed

demo/app/main-view-model.ts

+56-58
Original file line numberDiff line numberDiff line change
@@ -418,65 +418,63 @@ export class HelloWorldModel extends Observable {
418418
);
419419
}
420420

421-
// ===================================================================================
422-
/**
423-
*
424-
* @todo INTEGRATE THIS
425-
*
426-
public doAddLayerAndSource(): void {
427-
this.mapbox.addSource(
428-
{
429-
id: "terrain-source",
430-
type: "vector",
431-
url: "mapbox://mapbox.mapbox-terrain-v2"
432-
}
433-
).then(
434-
() => {
435-
this.mapbox.addLayer(
436-
{
437-
id: "terrain-data",
438-
source: "terrain-source",
439-
sourceLayer: "contour",
440-
type: "line",
441-
lineJoin: "round",
442-
lineCap: "round",
443-
lineColor: "#ff69b4",
444-
lineWidth: 1,
445-
}
446-
).then(
447-
() => {
448-
console.log("Mapbox doAddLayerAndSource done");
449-
},
450-
(error: string) => {
451-
console.log("mapbox doAddLayerAndSource error: " + error);
452-
}
453-
);
454-
},
455-
(error: string) => {
456-
console.log("mapbox doAddLayerAndSource error: " + error);
457-
}
458-
);
459-
}
460-
461-
public doRemoveLayerAndSource(): void {
462-
this.mapbox.removeLayer("terrain-data").then(
463-
() => {
464-
this.mapbox.removeSource("terrain-source").then(
421+
public doAddLayerAndSource(): void {
422+
this.mapbox
423+
.addLayer({
424+
id: 'custom-layer-1',
425+
type: 'line',
426+
source: {
427+
type: 'geojson',
428+
data: {
429+
type: 'Feature',
430+
properties: {},
431+
geometry: {
432+
type: 'LineString',
433+
coordinates: [
434+
[4.744720458984375, 52.47357958606801],
435+
[5.108642578125, 52.24882376803033],
436+
],
437+
},
438+
},
439+
url: null,
440+
},
441+
layout: {
442+
'line-cap': 'round',
443+
'line-join': 'round',
444+
},
445+
paint: {
446+
'line-color': '#053481',
447+
'line-width': 5,
448+
'line-opacity': 0.8,
449+
'line-dash-array': [1, 1, 1, 1],
450+
},
451+
})
452+
.catch(console.error);
453+
}
454+
455+
public doRemoveLayerAndSource(): void {
456+
this.mapbox.removeLayer('custom-layer-1').then(
465457
() => {
466-
console.log("Mapbox doRemoveLayerAndSource done");
458+
/*
459+
layer 'custom-layer-1' implicitly added a source under the hood,
460+
so the source id was generate as <layer-id>_source, i.e.: 'custom-layer-1_source'
461+
*/
462+
const sourceId = 'custom-layer-1_source';
463+
this.mapbox.removeSource(sourceId).then(
464+
() => {
465+
console.log('Mapbox doRemoveLayerAndSource done');
466+
},
467+
(error: string) => {
468+
console.log('mapbox doAddSource error: ' + error);
469+
}
470+
);
467471
},
468472
(error: string) => {
469-
console.log("mapbox doAddSource error: " + error);
473+
console.log('mapbox doAddSource error: ' + error);
470474
}
471-
);
472-
},
473-
(error: string) => {
474-
console.log("mapbox doAddSource error: " + error);
475-
}
476-
);
477-
}
478-
479-
*/
475+
);
476+
}
477+
480478
// ===============================================================
481479

482480
// -------------------------------------------------------------------------------
@@ -827,11 +825,11 @@ export class HelloWorldModel extends Observable {
827825

828826
public doGetLayers(): void {
829827
this.mapbox.getLayers().then((layers) => {
830-
layers.map(l => console.log(l.id));
828+
layers.map((l) => console.log(l.id));
831829

832830
const alertOptions: AlertOptions = {
833831
title: 'All map style layers',
834-
message: JSON.stringify(layers.map(l => l.id)),
832+
message: JSON.stringify(layers.map((l) => l.id)),
835833
okButtonText: 'OK',
836834
};
837835
alert(alertOptions);
@@ -841,7 +839,7 @@ export class HelloWorldModel extends Observable {
841839
if (!!waterwayLayer) {
842840
console.log(`getLayer("${waterwayLayer.id}") visible?: ${waterwayLayer.visibility()}`);
843841
}
844-
})
842+
});
845843
}
846844

847845
public doToggleLayers(): void {

src/mapbox.android.ts

+18-21
Original file line numberDiff line numberDiff line change
@@ -2756,15 +2756,15 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
27562756
return new Promise((resolve, reject) => {
27572757
try {
27582758
const { url, type } = options;
2759-
const theMap = nativeMap;
2759+
const theMap = nativeMap || this._mapboxMapInstance;
27602760
let source;
27612761

27622762
if (!theMap) {
27632763
reject('No map has been loaded');
27642764
return;
27652765
}
27662766

2767-
if (theMap.mapboxMap.getSource(id)) {
2767+
if (theMap.getStyle().getSource(id)) {
27682768
reject('Source exists: ' + id);
27692769
return;
27702770
}
@@ -2787,13 +2787,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
27872787
CLog(CLogTypes.info, 'Mapbox:addSource(): adding feature');
27882788
}
27892789

2790-
// com.mapbox.mapboxsdk.maps.Style
2791-
2792-
const geoJsonSource = new com.mapbox.mapboxsdk.style.sources.GeoJsonSource(id, feature);
2793-
2794-
this._mapboxMapInstance.getStyle().addSource(geoJsonSource);
2795-
2796-
// this.gcFix('com.mapbox.mapboxsdk.style.sources.GeoJsonSource', geoJsonSource);
2790+
source = new com.mapbox.mapboxsdk.style.sources.GeoJsonSource(id, feature);
27972791

27982792
// To support handling click events on lines and circles, we keep the underlying
27992793
// feature.
@@ -2833,7 +2827,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
28332827
return;
28342828
}
28352829

2836-
theMap.mapboxMap.addSource(source);
2830+
theMap.getStyle().addSource(source);
28372831
resolve();
28382832
} catch (ex) {
28392833
if (Trace.isEnabled()) {
@@ -2853,15 +2847,13 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
28532847
removeSource(id: string, nativeMap?): Promise<void> {
28542848
return new Promise((resolve, reject) => {
28552849
try {
2856-
const theMap = nativeMap;
2850+
const theMap = nativeMap || this._mapboxMapInstance;
28572851

28582852
if (!theMap) {
28592853
reject('No map has been loaded');
28602854
return;
28612855
}
28622856

2863-
theMap.mapboxMap.removeSource(id);
2864-
28652857
// if we've cached the underlying feature, remove it.
28662858
//
28672859
// since we don't know if it's a line or a circle we have to check both lists.
@@ -2909,16 +2901,20 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
29092901
* @link https://docs.mapbox.com/mapbox-gl-js/style-spec/#layers
29102902
*/
29112903

2912-
public addLayer(style, nativeMapView?): Promise<void> {
2904+
public addLayer(style, nativeMap?): Promise<void> {
29132905
let retval;
2906+
const theMap = nativeMap || this._mapboxMapInstance;
2907+
if (!theMap) {
2908+
return Promise.reject('No map has been loaded');
2909+
}
29142910

29152911
switch (style.type) {
29162912
case 'line':
2917-
retval = this.addLineLayer(style, nativeMapView);
2913+
retval = this.addLineLayer(style, theMap);
29182914
break;
29192915

29202916
case 'circle':
2921-
retval = this.addCircleLayer(style, nativeMapView);
2917+
retval = this.addCircleLayer(style, theMap);
29222918
break;
29232919

29242920
default:
@@ -2930,8 +2926,6 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
29302926
return retval;
29312927
}
29322928

2933-
// -----------------------------------------------------------------------
2934-
29352929
/**
29362930
* remove layer by ID
29372931
*
@@ -2940,12 +2934,15 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
29402934
* @param {string} id
29412935
*/
29422936

2943-
public async removeLayer(id: string, nativeMapViewInstance) {
2944-
this._mapboxMapInstance.getStyle().removeLayer(id);
2937+
public async removeLayer(id: string, nativeMap?) {
2938+
const theMap = nativeMap || this._mapboxMapInstance;
2939+
2940+
theMap.getStyle().removeLayer(id);
2941+
29452942
if (Trace.isEnabled()) {
29462943
CLog(CLogTypes.info, 'Mapbox:removeLayer(): after removing layer');
29472944
}
2948-
} // end of removeLayer()
2945+
}
29492946

29502947
// -------------------------------------------------------------------------------------
29512948

src/mapbox.ios.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2382,7 +2382,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
23822382
* @param {string} id
23832383
*/
23842384

2385-
public async removeLayer(id: string, nativeMapViewInstance) {
2385+
public async removeLayer(id: string, nativeMapViewInstance?) {
23862386
const theMap: MGLMapView = nativeMapViewInstance || this._mapboxViewInstance;
23872387

23882388
if (Trace.isEnabled()) {

0 commit comments

Comments
 (0)