Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ The map will continuously move along with the last known location.

https://docs.mapbox.com/mapbox-gl-js/api/#map#addsource

Supported source types:
- Vector
- GeoJson
- Raster

Adds a vector to GeoJSON source to the map.

```js
Expand Down Expand Up @@ -614,6 +619,7 @@ Supported layer types:
- Circle
- Fill
- Symbol
- Raster

To add a line:

Expand Down
2 changes: 2 additions & 0 deletions demo/app/main-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@

<Button row="12" col="0" text="get layers" tap="{{ doGetLayers }}" class="button" />
<Button row="12" col="1" text="toggle all layers" tap="{{ doToggleLayers }}" class="button" />
<Button row="12" col="2" text="+ raster" tap="{{ doAddRasterLayer }}" class="button" />
<Button row="12" col="3" text="- raster" tap="{{ doRemoveRasterLayer }}" class="button" />

<StackLayout row="13" col="0" colSpan="4">
<ContentView with="100%" height="100%" id="mapContainer" />
Expand Down
23 changes: 23 additions & 0 deletions demo/app/main-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1005,4 +1005,27 @@ export class HelloWorldModel extends Observable {
});
});
}

public doAddRasterLayer(): void {
this.mapbox
.addLayer({
id: 'raster-layer',
type: 'raster',
source: {
type: 'raster',
tiles: ['https://stamen-tiles.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.jpg'],
tileSize: 256
}
})
.then(() => {
console.log('raster layer added');
});
}

public doRemoveRasterLayer(): void {
this.mapbox.removeLayer('raster-layer').then(() => {
this.mapbox.removeSource('raster-layer_source');
console.log('layer removed');
});
}
}
90 changes: 78 additions & 12 deletions src/layers/layer-factory.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { LayerCommon } from '../mapbox.common';

export class LayerFactory {
static async createLayer(style, source): Promise<LayerCommon> {
const layerProperties = this.parseProperties(style.type, Object.assign(style.paint, style.layout)); // TODO: handle defaults
const layerProperties = this.parseProperties(style.type, Object.assign(style.paint || {}, style.layout || {})); // TODO: handle defaults

const sourceId = source.getId();
let nativeLayer: com.mapbox.mapboxsdk.style.layers.Layer;
Expand All @@ -21,11 +21,14 @@ export class LayerFactory {
case 'symbol':
nativeLayer = new com.mapbox.mapboxsdk.style.layers.SymbolLayer(style.id, sourceId).withProperties(layerProperties);
break;
case 'raster':
nativeLayer = new com.mapbox.mapboxsdk.style.layers.RasterLayer(style.id, sourceId).withProperties(layerProperties);
break;
default:
throw new Error(`Unknown layer type: ${style.type}`);
}

var layer = new Layer(nativeLayer);
const layer = new Layer(nativeLayer);

return layer;
}
Expand All @@ -40,6 +43,8 @@ export class LayerFactory {
return this.parsePropertiesForFillLayer(propertiesObject);
case 'symbol':
return this.parsePropertiesForSymbolLayer(propertiesObject);
case 'raster':
return this.parsePropertiesForRasterLayer(propertiesObject);
default:
throw new Error(`Unknown layer type: ${layerType}`);
}
Expand Down Expand Up @@ -246,15 +251,7 @@ export class LayerFactory {
base = propertiesObject['circle-radius'].stops.base;
}

circleProperties.push(
PropertyFactory.circleRadius(
Expression.interpolate(
Expression.exponential(new java.lang.Float(base)),
Expression.zoom(),
stopArgs
)
)
);
circleProperties.push(PropertyFactory.circleRadius(Expression.interpolate(Expression.exponential(new java.lang.Float(base)), Expression.zoom(), stopArgs)));
}
}

Expand Down Expand Up @@ -443,7 +440,76 @@ export class LayerFactory {
if (propertiesObject['visibility']) {
symbolProperties.push(PropertyFactory.visibility(propertiesObject['visibility']));
}

return symbolProperties;
}

private static parsePropertiesForRasterLayer(propertiesObject) {
const rasterProperties = [];

if (!propertiesObject) {
return rasterProperties;
}

/*
raster-brightness-max ✓
raster-brightness-min ✓
raster-contrast ✓
raster-fade-duration ✓
raster-hue-rotate ✓
raster-opacity ✓
raster-resampling ✓
raster-saturation ✓
visibility ✓
*/

const PropertyFactory = com.mapbox.mapboxsdk.style.layers.PropertyFactory;

if (propertiesObject['raster-brightness-max']) {
rasterProperties.push(PropertyFactory.rasterBrightnessMax(new java.lang.Float(propertiesObject['raster-brightness-max'])));
}

if (propertiesObject['raster-brightness-min']) {
rasterProperties.push(PropertyFactory.rasterBrightnessMin(new java.lang.Float(propertiesObject['raster-brightness-min'])));
}

if (propertiesObject['raster-contrast']) {
rasterProperties.push(PropertyFactory.rasterContrast(new java.lang.Float(propertiesObject['raster-contrast'])));
}

if (propertiesObject['raster-fade-duration']) {
rasterProperties.push(PropertyFactory.rasterFadeDuration(new java.lang.Float(propertiesObject['raster-fade-duration'])));
}

if (propertiesObject['raster-hue-rotate']) {
rasterProperties.push(PropertyFactory.rasterHueRotate(new java.lang.Float(propertiesObject['raster-hue-rotate'])));
}

if (propertiesObject['raster-opacity']) {
rasterProperties.push(PropertyFactory.rasterOpacity(new java.lang.Float(propertiesObject['raster-opacity'])));
}

if (propertiesObject['raster-resampling']) {
switch (propertiesObject['raster-resampling']) {
case 'linear':
rasterProperties.push(com.mapbox.mapboxsdk.style.layers.Property.RASTER_RESAMPLING_LINEAR);
break;
case 'nearest':
rasterProperties.push(com.mapbox.mapboxsdk.style.layers.Property.RASTER_RESAMPLING_NEAREST);
break;
default:
throw new Error('Unknown raster resampling value.');
}
}

if (propertiesObject['raster-saturation']) {
rasterProperties.push(PropertyFactory.rasterSaturation(new java.lang.Float(propertiesObject['raster-saturation'])));
}

if (propertiesObject['visibility']) {
rasterProperties.push(PropertyFactory.visibility(propertiesObject['visibility']));
}

return rasterProperties;
}
}
78 changes: 75 additions & 3 deletions src/layers/layer-factory.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,27 @@ export class LayerFactory {
case 'symbol':
nativeLayer = MGLSymbolStyleLayer.alloc().initWithIdentifierSource(style.id, source);
break;
case 'raster':
nativeLayer = MGLRasterStyleLayer.alloc().initWithIdentifierSource(style.id, source);
break;
default:
throw new Error(`Unknown layer type: ${style.type}`);
}

const layerProperties = this.parseProperties(style.type, Object.assign(style.paint, style.layout)); // TODO: handle defaults
const layerProperties = this.parseProperties(style.type, Object.assign(style.paint || {}, style.layout || {})); // TODO: handle defaults

for (const propKey in layerProperties) {
if (Object.prototype.hasOwnProperty.call(layerProperties, propKey)) {
nativeLayer[propKey] = layerProperties[propKey];
}
}

var layer = new Layer(nativeLayer);
const layer = new Layer(nativeLayer);

return layer;
}

private static parseProperties(layerType, propertiesObject) {
private static parseProperties(layerType, propertiesObject): any {
switch (layerType) {
case 'line':
return this.parsePropertiesForLineLayer(propertiesObject);
Expand All @@ -46,6 +49,8 @@ export class LayerFactory {
return this.parsePropertiesForFillLayer(propertiesObject);
case 'symbol':
return this.parsePropertiesForSymbolLayer(propertiesObject);
case 'raster':
return this.parsePropertiesForRasterLayer(propertiesObject);
default:
throw new Error(`Unknown layer type: ${layerType}`);
}
Expand Down Expand Up @@ -360,4 +365,71 @@ export class LayerFactory {

return symbolProperties;
}

private static parsePropertiesForRasterLayer(propertiesObject) {
const rasterProperties = {};

if (!propertiesObject) {
return rasterProperties;
}

/*
raster-brightness-max ✓
raster-brightness-min ✓
raster-contrast ✓
raster-fade-duration ✓
raster-hue-rotate ✓
raster-opacity ✓
raster-resampling ✓
raster-saturation ✓
visibility ✓
*/

if (propertiesObject['raster-brightness-max']) {
rasterProperties['maximumRasterBrightness'] = NSExpression.expressionForConstantValue(propertiesObject['raster-brightness-max']);
}

if (propertiesObject['raster-brightness-min']) {
rasterProperties['minimumRasterBrightness'] = NSExpression.expressionForConstantValue(propertiesObject['raster-brightness-min']);
}

if (propertiesObject['raster-contrast']) {
rasterProperties['rasterContrast'] = NSExpression.expressionForConstantValue(propertiesObject['raster-contrast']);
}

if (propertiesObject['raster-fade-duration']) {
rasterProperties['rasterFadeDuration'] = NSExpression.expressionForConstantValue(propertiesObject['raster-fade-duration']);
}

if (propertiesObject['raster-hue-rotate']) {
rasterProperties['rasterHueRotation'] = NSExpression.expressionForConstantValue(propertiesObject['raster-hue-rotate']);
}

if (propertiesObject['raster-opacity']) {
rasterProperties['rasterOpacity'] = NSExpression.expressionForConstantValue(propertiesObject['raster-opacity']);
}

if (propertiesObject['raster-resampling']) {
switch (propertiesObject['raster-resampling']) {
case 'linear':
rasterProperties['rasterResamplingMode'] = MGLRasterResamplingMode.Linear;
break;
case 'nearest':
rasterProperties['rasterResamplingMode'] = MGLRasterResamplingMode.Nearest;
break;
default:
throw new Error('Unknown raster resampling value.');
}
}

if (propertiesObject['raster-saturation']) {
rasterProperties['rasterSaturation'] = NSExpression.expressionForConstantValue(propertiesObject['raster-saturation']);
}

if (propertiesObject['visibility']) {
rasterProperties['visibility'] = NSExpression.expressionForConstantValue(propertiesObject['visibility']);
}

return rasterProperties;
}
}
41 changes: 31 additions & 10 deletions src/mapbox.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2627,7 +2627,6 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
addSource(id: string, options: AddSourceOptions, nativeMap?): Promise<void> {
return new Promise((resolve, reject) => {
try {
const { url, type } = options;
const theMap = nativeMap || this._mapboxMapInstance;
let source;

Expand All @@ -2641,9 +2640,9 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
return;
}

switch (type) {
switch (options.type) {
case 'vector':
source = new com.mapbox.mapboxsdk.style.sources.VectorSource(id, url);
source = new com.mapbox.mapboxsdk.style.sources.VectorSource(id, options.url);
break;

case 'geojson':
Expand All @@ -2659,8 +2658,33 @@ export class Mapbox extends MapboxCommon implements MapboxApi {

break;

case 'raster':
// use Array.create because a marshal error throws on TileSet if options.tiles directly passed.
const tiles = Array.create(java.lang.String, options.tiles.length);
options.tiles.forEach((val, i) => tiles[i] = val);

const tileSet = new com.mapbox.mapboxsdk.style.sources.TileSet('tileset', tiles);

if (options.minzoom) {
tileSet.setMinZoom(options.minzoom);
}

if (options.maxzoom) {
tileSet.setMaxZoom(options.maxzoom);
}

if (options.scheme) {
tileSet.setScheme(options.scheme);
}

if (options.bounds) {
tileSet.setBounds(options.bounds.map((val) => new java.lang.Float(val)));
}

source = new com.mapbox.mapboxsdk.style.sources.RasterSource(id, tileSet, options.tileSize);
break;
default:
reject('Invalid source type: ' + type);
reject('Invalid source type: ' + options['type']);
return;
}

Expand Down Expand Up @@ -2739,17 +2763,14 @@ export class Mapbox extends MapboxCommon implements MapboxApi {

let source = null;
if (typeof style.source != 'string') {
this.addSource(style.id + '_source', style.source);
await this.addSource(style.id + '_source', style.source);
source = theMap.getStyle().getSource(style.id + '_source');
} else {
source = style.source;
source = theMap.getStyle().getSource(style.source);
}

const layer = await LayerFactory.createLayer(style, source);

const layer = await LayerFactory.createLayer(style, source);
this._mapboxMapInstance.getStyle().addLayer(layer.getNativeInstance());

return Promise.resolve();
}

/**
Expand Down
Loading