UNPKG

@nativescript-community/ui-mapbox

Version:

Interactive, thoroughly customizable maps powered by vector tiles and OpenGL.

135 lines 5.65 kB
import { ExpressionParser } from '../expression/expression-parser'; import { PropertyParser } from './parser/property-parser'; import { toCamelCase, transformValue } from './parser/property-parser.android'; export class Layer { constructor(instance) { this.instance = instance; this.id = instance.getSourceId(); } visibility() { return this.instance.getVisibility().getValue() === 'visible' ? true : false; } show() { LayerFactory.applyLayerProperties(this.instance, { visibility: 'visible' }); // this.instance.setProperties([new com.mapbox.maps.extension.style.layers.generated.PropertyValue('visibility', 'visible')]); } hide() { LayerFactory.applyLayerProperties(this.instance, { visibility: 'none' }); // this.instance.setProperties([new com.mapbox.maps.extension.style.layers.generated.PropertyValue('visibility', 'none')]); } getNativeInstance() { return this.instance; } setFilter(filter) { if (this.instance['setFilter']) { this.instance['setFilter'](ExpressionParser.parseJson(filter)); } } getFilter() { if (this.instance['getFilter']) { return ExpressionParser.toJson(this.instance['getFilter']()); } return []; } setProperty(name, value) { LayerFactory.applyLayerProperties(this.instance, { [name]: value }); } getProperty(name) { return PropertyParser.propertyValueFromLayer(this.instance, name); } type() { if (this.instance instanceof com.mapbox.maps.extension.style.layers.generated.FillLayer) { return 'fill'; } if (this.instance instanceof com.mapbox.maps.extension.style.layers.generated.LineLayer) { return 'line'; } if (this.instance instanceof com.mapbox.maps.extension.style.layers.generated.SymbolLayer) { return 'symbol'; } if (this.instance instanceof com.mapbox.maps.extension.style.layers.generated.CircleLayer) { return 'circle'; } if (this.instance instanceof com.mapbox.maps.extension.style.layers.generated.HeatmapLayer) { return 'heatmap'; } if (this.instance instanceof com.mapbox.maps.extension.style.layers.generated.FillExtrusionLayer) { return 'fill-extrusion'; } if (this.instance instanceof com.mapbox.maps.extension.style.layers.generated.RasterLayer) { return 'raster'; } if (this.instance instanceof com.mapbox.maps.extension.style.layers.generated.HillshadeLayer) { return 'hillshade'; } // if (this.instance instanceof com.mapbox.maps.extension.style.layers.generated.BackgroundLayer) { // return 'background'; // } // there is no sky layer in the Android Mapbox SDK return null; } } export class LayerFactory { static applyLayerProperties(layer, properties) { for (const key in properties) { if (!properties.hasOwnProperty(key)) continue; const value = properties[key]; const actualKey = toCamelCase(key); const nValue = transformValue(actualKey, value); // Mapbox v11 setters are named after the property, e.g., circleColor(), circleRadius() const setterName = actualKey; // Call the setter dynamically if (typeof layer[setterName] === 'function') { layer[setterName](nValue); } else { console.warn(`Layer has no setter for ${setterName}`); } } } static async createLayer(style, source) { // const layerProperties = this.parseProperties(style.type, Object.assign(style.paint || {}, style.layout || {})); // TODO: handle defaults const layerProperties = Object.assign(style.paint || {}, style.layout || {}); // TODO: handle defaults const sourceId = source.getSourceId(); let nativeLayer; switch (style.type) { case 'line': nativeLayer = new com.mapbox.maps.extension.style.layers.generated.LineLayer(style.id, sourceId); break; case 'circle': nativeLayer = new com.mapbox.maps.extension.style.layers.generated.CircleLayer(style.id, sourceId); break; case 'fill': nativeLayer = new com.mapbox.maps.extension.style.layers.generated.FillLayer(style.id, sourceId); break; case 'symbol': nativeLayer = new com.mapbox.maps.extension.style.layers.generated.SymbolLayer(style.id, sourceId); break; case 'raster': nativeLayer = new com.mapbox.maps.extension.style.layers.generated.RasterLayer(style.id, sourceId); break; default: throw new Error(`Unknown layer type: ${style.type}`); } LayerFactory.applyLayerProperties(nativeLayer, layerProperties); const layer = new Layer(nativeLayer); if (style.minzoom !== undefined) { nativeLayer.minZoom(style.minzoom); } if (style.maxzoom !== undefined) { nativeLayer.maxZoom(style.maxzoom); } // if (style['source-layer'] && (nativeLayer as any).withSourceLayer) { // (nativeLayer as any).withSourceLayer(style['source-layer']); // } return layer; } } //# sourceMappingURL=layer-factory.android.js.map