Azure Maps Mapbox Style Importer module

September 1, 2021 ยท View on GitHub

An Azure Maps Web SDK module that takes a Mapbox style object and converts it into native Azure Maps classes and adds it to a map instance. Extracts Mapbox light, source, and layer styles, converts them to Azure Maps comparable values, and applies it to the map.

  • Supported Sources: vector, raster, geojson, image
  • Supported Layers: circle, fill, fill-extrusion, line, heatmap, raster, symbol
  • Handles a combinations of Mapbox and Azure Maps style properties to be passed in.

Getting started

Download the project and copy the azure-maps-mapbox-style-importer JavaScript file from the dist folder into your project.

Usage

//Initialize a map instance.
var map = new atlas.Map('myMap', {
    //Add your auth options
});

//Wait until the map resources are ready.
map.events.add('ready', function () {

    //Create an instance of the mapbox style importer.
    var importer = atlas.MapboxStyleImporter(map);   

    //Import a single source.
    //(id: string, data: string | GeoJSON object)
    importer.addSource('my-source', {
        'type': 'geojson',
        'data': {
            'type': 'FeatureCollection',
            'features': [
            {
                'type': 'Feature',
                'properties': {
                    'color': 'red'
                },
                'geometry': {
                    'type': 'Point',
                    'coordinates': [0, 0]
                }
            }
        }
    });

    //Import a single later.
    importer.addLayer({
        'id': 'my-circle-layer',
        'type': 'circle',
        'source': 'my-source',
        'paint': {
            'circle-color': ['get', 'color']
        }
    });

    //Import in a style that includes multiple sources and layers.
    importer.addStyle({
        sources: {    
            'my-source-1': {
                'type': 'geojson',
                'data': {
                    'type': 'FeatureCollection',
                    'features': [
                    {
                        'type': 'Feature',
                        'properties': {},
                        'geometry': {
                            'type': 'Point',
                            'coordinates': [100, 0]
                        }
                    }
                }
            },
            'my-source-2': {
                'type': 'geojson',
                'data': {
                    'type': 'FeatureCollection',
                    'features': [
                    {
                        'type': 'Feature',
                        'properties': {},
                        'geometry': {
                            'type': 'Point',
                            'coordinates': [-100, 0]
                        }
                    }
                }
            }
        },
        layers: [{
            'id': 'my-circle-layer-1',
            'type': 'circle',
            'source': 'my-source-1',
            'paint': {
                'circle-color': 'purple'
            }
        },
        {
            'id': 'my-circle-layer-2',
            'type': 'circle',
            'source': 'my-source-2',
            'paint': {
                'circle-color': 'blue'
            }
        }]
    });

    //Update the style options of a layer.     
    importer.addLayer('my-circle-layer', {
        'paint': {
            'circle-color': 'red'
        }
    });
});

API Reference

MapboxStyleImporter class

Namespace: atlas

A class that converts Mapbox styles into native Azure Maps classes and imports them into an Azure Maps instance.

Contstructor

MapboxStyleImporter(map: atlas.Map)

Methods

NameReturn typeDescription
addLayer(mapboxLayer: mapbox.Layer, beforeLayer?: string)atlas.layer.LayerConverts a Mapbox style layer to an Azure Map layer and adds it to a map.
setLayerOptions(layer: string | atlas.layer.Layer, options: MapboxLayerOptions | atlas.BubbleLayerOptions | atlas.IconOptions | atlas.TextOptions | atlas.LayerOptions | atlas.HeatMapLayerOptions | atlas.LineLayerOptions | atlas.PolygonLayerOptions | atlas.PolygonExtrusionLayerOptions | atlas.SymbolLayerOptions | atlas.ImageLayerOptions | atlas.TileLayerOptions)Updates the style options of a layer. Note that this appends/ overwrites individual style options, and does not replace the whole style option unless all previously specified options are included in the new options.
addSource(id: string, mapboxSource: mapbox.Source)Converts a Mapbox style source to an Azure Map source and adds it to a map.
addStyle(mapboxStyle: mapbox.Style)Extracts Mapbox light, source, and layer styles, converts them to Azure Maps comparable values, and applies it to the map.

MapboxLayerOptions interface

NameTypeDescription
minzoomnumberMin zoom level of layer.
maxzoomnumberMax zoom level of layer.
filterany[]Layer filter.
layoutmapbox.AnyLayoutMapbox layout options.
paintmapbox.AnyPaintMapbox paint options.

Additional Resources

Contributing

We welcome contributions. Feel free to submit code samples, file issues and pull requests on the repo and we'll address them as we can. Learn more about how you can help on our Contribution Rules & Guidelines.

You can reach out to us anytime with questions and suggestions using our communities below:

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

License

MIT

See License for full license text.