Skip to content

Commit a75fc27

Browse files
committed
feat: added more source options
1 parent 2015bfb commit a75fc27

File tree

3 files changed

+107
-26
lines changed

3 files changed

+107
-26
lines changed

src/mapbox.android.ts

+38-8
Original file line numberDiff line numberDiff line change
@@ -2364,31 +2364,61 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
23642364
reject('Source exists: ' + id);
23652365
return;
23662366
}
2367-
23682367
switch (options.type) {
2369-
case 'vector':
2370-
source = new com.mapbox.mapboxsdk.style.sources.VectorSource(id, options.url);
2368+
case 'vector': {
2369+
if (options.url) {
2370+
source = new com.mapbox.mapboxsdk.style.sources.VectorSource(id, options.url);
2371+
} else {
2372+
const tiles = Array.create(java.lang.String, options.tiles.length);
2373+
options.tiles.forEach((val, i) => (tiles[i] = val));
2374+
const tileSet = new com.mapbox.mapboxsdk.style.sources.TileSet('2.0.0', tiles);
2375+
if (options.minzoom) {
2376+
tileSet.setMinZoom(options.minzoom);
2377+
}
2378+
2379+
if (options.maxzoom) {
2380+
tileSet.setMaxZoom(options.maxzoom);
2381+
}
2382+
2383+
if (options.scheme) {
2384+
tileSet.setScheme(options.scheme);
2385+
}
2386+
2387+
if (options.bounds) {
2388+
tileSet.setBounds(options.bounds.map((val) => new java.lang.Float(val)));
2389+
}
2390+
source = new com.mapbox.mapboxsdk.style.sources.VectorSource(id, tileSet);
2391+
}
23712392
break;
2393+
}
23722394

2373-
case 'geojson':
2395+
case 'geojson': {
23742396
if (Trace.isEnabled()) {
23752397
CLog(CLogTypes.info, 'Mapbox:addSource(): before addSource with geojson');
23762398
CLog(CLogTypes.info, 'Mapbox:addSource(): before adding geoJSON to GeoJsonSource');
23772399
}
2400+
const geojsonOptions = new com.mapbox.mapboxsdk.style.sources.GeoJsonOptions();
2401+
if (options.minzoom) {
2402+
geojsonOptions.withMinZoom(options.minzoom);
2403+
}
23782404

2379-
const geoJsonSource = new com.mapbox.mapboxsdk.style.sources.GeoJsonSource(id);
2405+
if (options.maxzoom) {
2406+
geojsonOptions.withMaxZoom(options.maxzoom);
2407+
}
2408+
2409+
const geoJsonSource = new com.mapbox.mapboxsdk.style.sources.GeoJsonSource(id, geojsonOptions);
23802410
const geoJsonString = JSON.stringify(options.data);
23812411
geoJsonSource.setGeoJson(geoJsonString);
23822412
source = geoJsonSource;
23832413

23842414
break;
2415+
}
23852416

23862417
case 'raster':
23872418
// use Array.create because a marshal error throws on TileSet if options.tiles directly passed.
23882419
const tiles = Array.create(java.lang.String, options.tiles.length);
2389-
options.tiles.forEach((val, i) => tiles[i] = val);
2390-
2391-
const tileSet = new com.mapbox.mapboxsdk.style.sources.TileSet('tileset', tiles);
2420+
options.tiles.forEach((val, i) => (tiles[i] = val));
2421+
const tileSet = new com.mapbox.mapboxsdk.style.sources.TileSet('2.0.0', tiles);
23922422

23932423
if (options.minzoom) {
23942424
tileSet.setMinZoom(options.minzoom);

src/mapbox.common.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ export type UpdateSourceOptions = VectorSource | GeoJSONSource | RasterSource;
299299

300300
export interface Source {
301301
type: 'vector' | 'raster' | 'geojson';
302+
scheme?: 'xyz' | 'tms';
302303
}
303304

304305
// -------------------------------------------------------------
@@ -310,21 +311,27 @@ export interface RasterSource extends Source {
310311
minzoom?: number;
311312
maxzoom?: number;
312313
tileSize?: number;
313-
scheme?: 'xyz' | 'tms';
314314
}
315315

316316
// -------------------------------------------------------------
317317

318318
export interface VectorSource extends Source {
319319
type: 'vector';
320-
url: string;
320+
url?: string;
321+
tiles?: string[];
322+
bounds?: number[];
323+
minzoom?: number;
324+
maxzoom?: number;
325+
tileSize?: number;
321326
}
322327

323328
// -------------------------------------------------------------
324329

325330
export interface GeoJSONSource extends Source {
326331
type: 'geojson';
327332
data?: any;
333+
minzoom?: number;
334+
maxzoom?: number;
328335
}
329336

330337
// ------------------------------------------------------------

src/mapbox.ios.ts

+60-16
Original file line numberDiff line numberDiff line change
@@ -2132,30 +2132,73 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
21322132
}
21332133

21342134
switch (options.type) {
2135-
case 'vector':
2136-
source = MGLVectorTileSource.alloc().initWithIdentifierConfigurationURL(id, NSURL.URLWithString(options.url));
2137-
break;
2135+
case 'vector': {
2136+
if (options.url) {
2137+
source = MGLVectorTileSource.alloc().initWithIdentifierConfigurationURL(id, NSURL.URLWithString(options.url));
2138+
} else {
2139+
const sourceOptions: any = {};
2140+
if (options.minzoom !== undefined) {
2141+
sourceOptions[MGLTileSourceOptionMinimumZoomLevel] = options.minzoom;
2142+
}
2143+
if (options.maxzoom !== undefined) {
2144+
sourceOptions[MGLTileSourceOptionMaximumZoomLevel] = options.maxzoom;
2145+
}
2146+
if (options.scheme) {
2147+
switch (options.scheme) {
2148+
case 'xyz':
2149+
sourceOptions[MGLTileSourceOptionTileCoordinateSystem] = MGLTileCoordinateSystem.XYZ;
2150+
break;
2151+
case 'tms':
2152+
sourceOptions[MGLTileSourceOptionTileCoordinateSystem] = MGLTileCoordinateSystem.TMS;
2153+
break;
2154+
default:
2155+
throw new Error('Unknown raster tile scheme.');
2156+
}
2157+
}
2158+
if (options.bounds) {
2159+
sourceOptions[MGLTileSourceOptionCoordinateBounds] = NSValue.valueWithMGLCoordinateBounds({
2160+
sw: CLLocationCoordinate2DMake(options.bounds[1], options.bounds[0]),
2161+
ne: CLLocationCoordinate2DMake(options.bounds[3], options.bounds[2]),
2162+
});
21382163

2164+
}
2165+
source = MGLVectorTileSource.alloc().initWithIdentifierTileURLTemplatesOptions(id, options.tiles, sourceOptions);
2166+
}
2167+
break;
2168+
}
21392169
case 'geojson':
21402170
if (theMap.style.sourceWithIdentifier(id)) {
21412171
reject("Remove the layer with this id first with 'removeLayer': " + id);
21422172
return;
21432173
}
2174+
let geoJsonShape: MGLShape;
2175+
if (options.data) {
2176+
const content: NSString = NSString.stringWithString(JSON.stringify(options.data));
2177+
const nsData: NSData = content.dataUsingEncoding(NSUTF8StringEncoding);
2178+
geoJsonShape = MGLShape.shapeWithDataEncodingError(nsData, NSUTF8StringEncoding);
2179+
}
2180+
2181+
const sourceOptions: any = {};
2182+
if (options.minzoom !== undefined) {
2183+
sourceOptions[MGLShapeSourceOptionMinimumZoomLevel] = options.minzoom;
2184+
}
2185+
if (options.maxzoom !== undefined) {
2186+
sourceOptions[MGLShapeSourceOptionMaximumZoomLevel] = options.maxzoom;
2187+
}
21442188

2145-
const content: NSString = NSString.stringWithString(JSON.stringify(options.data));
2146-
const nsData: NSData = content.dataUsingEncoding(NSUTF8StringEncoding);
2147-
const geoJsonShape = MGLShape.shapeWithDataEncodingError(nsData, NSUTF8StringEncoding);
2148-
2149-
source = MGLShapeSource.alloc().initWithIdentifierShapeOptions(id, geoJsonShape, null);
2189+
source = MGLShapeSource.alloc().initWithIdentifierShapeOptions(id, geoJsonShape, sourceOptions);
21502190

21512191
break;
2152-
case 'raster':
2192+
case 'raster': {
21532193
const sourceOptions: any = {
2154-
[MGLTileSourceOptionMinimumZoomLevel]: options.minzoom,
2155-
[MGLTileSourceOptionMaximumZoomLevel]: options.maxzoom,
2156-
[MGLTileSourceOptionTileSize]: options.tileSize,
2194+
[MGLTileSourceOptionTileSize]: options.tileSize || 256,
21572195
};
2158-
2196+
if (options.minzoom !== undefined) {
2197+
sourceOptions[MGLTileSourceOptionMinimumZoomLevel] = options.minzoom;
2198+
}
2199+
if (options.maxzoom !== undefined) {
2200+
sourceOptions[MGLTileSourceOptionMaximumZoomLevel] = options.maxzoom;
2201+
}
21592202
if (options.scheme) {
21602203
switch (options.scheme || 'xyz') {
21612204
case 'xyz':
@@ -2170,15 +2213,16 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
21702213
}
21712214

21722215
if (options.bounds) {
2173-
sourceOptions[MGLTileSourceOptionCoordinateBounds] = {
2216+
sourceOptions[MGLTileSourceOptionCoordinateBounds] = NSValue.valueWithMGLCoordinateBounds(({
21742217
sw: CLLocationCoordinate2DMake(options.bounds[1], options.bounds[0]),
21752218
ne: CLLocationCoordinate2DMake(options.bounds[3], options.bounds[2]),
2176-
} as MGLCoordinateBounds;
2219+
}));
2220+
console.log('test', sourceOptions[MGLTileSourceOptionCoordinateBounds])
21772221
}
2178-
21792222
source = MGLRasterTileSource.alloc().initWithIdentifierTileURLTemplatesOptions(id, options.tiles, sourceOptions);
21802223

21812224
break;
2225+
}
21822226
default:
21832227
reject('Invalid source type: ' + options['type']);
21842228
return;

0 commit comments

Comments
 (0)