Skip to content

Commit e593480

Browse files
committed
feat: updateSource (for now only to update geojson data)
1 parent d3e5e42 commit e593480

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/mapbox.android.ts

+36
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
SetZoomLevelOptions,
4141
ShowOptions,
4242
TrackUserOptions,
43+
UpdateSourceOptions,
4344
UserLocation,
4445
UserLocationCameraMode,
4546
Viewport,
@@ -2264,6 +2265,41 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
22642265
});
22652266
}
22662267

2268+
/**
2269+
* update a geojson source
2270+
*
2271+
*/
2272+
updateSource(id: string, options: UpdateSourceOptions, nativeMap?) {
2273+
return new Promise((resolve, reject) => {
2274+
try {
2275+
const theMap: com.mapbox.mapboxsdk.maps.MapboxMap = nativeMap || this._mapboxMapInstance;
2276+
if (!theMap) {
2277+
reject('No map has been loaded');
2278+
return;
2279+
}
2280+
2281+
const source = theMap.getStyle().getSource(id);
2282+
if (!source) {
2283+
reject('Source does not exists: ' + id);
2284+
return;
2285+
}
2286+
switch (options.type) {
2287+
case 'geojson':
2288+
const geoJsonString = JSON.stringify(options.data);
2289+
(source as com.mapbox.mapboxsdk.style.sources.GeoJsonSource).setGeoJson(geoJsonString);
2290+
break;
2291+
default:
2292+
reject('Invalid source type: ' + options['type']);
2293+
return;
2294+
}
2295+
} catch (ex) {
2296+
if (Trace.isEnabled()) {
2297+
CLog(CLogTypes.info, 'Error in mapbox.addSource: ' + ex);
2298+
}
2299+
reject(ex);
2300+
}
2301+
});
2302+
}
22672303
/**
22682304
* add a geojson or vector source
22692305
*

src/mapbox.common.ts

+5
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ export type UserTrackingMode = 'NONE' | 'FOLLOW' | 'FOLLOW_WITH_HEADING' | 'FOLL
293293
// -------------------------------------------------------------
294294

295295
export type AddSourceOptions = VectorSource | GeoJSONSource | RasterSource;
296+
export type UpdateSourceOptions = VectorSource | GeoJSONSource | RasterSource;
296297

297298
// -------------------------------------------------------------
298299

@@ -605,6 +606,7 @@ export interface MapboxApi {
605606
trackUser(options: TrackUserOptions, nativeMap?: any): Promise<void>;
606607

607608
addSource(id: string, options: AddSourceOptions, nativeMapView?: any): Promise<any>;
609+
updateSource(id: string, options: UpdateSourceOptions, nativeMapView?: any): Promise<any>;
608610

609611
removeSource(id: string, nativeMap?: any): Promise<any>;
610612

@@ -953,6 +955,9 @@ export abstract class MapboxViewCommonBase extends ContentView implements Mapbox
953955
addSource(id: string, options: AddSourceOptions): Promise<any> {
954956
return this.mapbox.addSource(id, options, this.getNativeMapView());
955957
}
958+
updateSource(id: string, options: UpdateSourceOptions): Promise<any> {
959+
return this.mapbox.updateSource(id, options, this.getNativeMapView());
960+
}
956961
removeSource(id: string): Promise<any> {
957962
return this.mapbox.removeSource(id, this.getNativeMapView());
958963
}

src/mapbox.ios.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
SetZoomLevelOptions,
3131
ShowOptions,
3232
TrackUserOptions,
33+
UpdateSourceOptions,
3334
UserLocation,
3435
UserLocationCameraMode,
3536
Viewport,
@@ -2068,6 +2069,44 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
20682069
});
20692070
}
20702071

2072+
/**
2073+
* update a geojson source
2074+
*
2075+
*/
2076+
updateSource(id: string, options: UpdateSourceOptions, nativeMap?) {
2077+
return new Promise((resolve, reject) => {
2078+
try {
2079+
const theMap: MGLMapView = nativeMap || this._mapboxViewInstance;
2080+
if (!theMap) {
2081+
reject('No map has been loaded');
2082+
return;
2083+
}
2084+
2085+
const source = theMap.style.sourceWithIdentifier(id);
2086+
if (!source) {
2087+
reject('Source does not exists: ' + id);
2088+
return;
2089+
}
2090+
switch (options.type) {
2091+
case 'geojson':
2092+
const content: NSString = NSString.stringWithString(JSON.stringify(options.data));
2093+
const nsData: NSData = content.dataUsingEncoding(NSUTF8StringEncoding);
2094+
const geoJsonShape = MGLShape.shapeWithDataEncodingError(nsData, NSUTF8StringEncoding);
2095+
(source as MGLShapeSource).shape = geoJsonShape;
2096+
break;
2097+
default:
2098+
reject('Invalid source type: ' + options['type']);
2099+
return;
2100+
}
2101+
} catch (ex) {
2102+
if (Trace.isEnabled()) {
2103+
CLog(CLogTypes.info, 'Error in mapbox.addSource: ' + ex);
2104+
}
2105+
reject(ex);
2106+
}
2107+
});
2108+
}
2109+
20712110
/**
20722111
* add a vector or geojson source
20732112
*
@@ -2076,7 +2115,6 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
20762115
*
20772116
* @link https://docs.mapbox.com/mapbox-gl-js/api/#map#addsource
20782117
*/
2079-
20802118
addSource(id: string, options: AddSourceOptions, nativeMap?): Promise<void> {
20812119
return new Promise((resolve, reject) => {
20822120
try {

0 commit comments

Comments
 (0)