boundlessgeo/ol-mapbox-style

Name: ol-mapbox-style

Owner: Boundless

Description: Use Mapbox Style objects with OpenLayers

Created: 2016-08-25 10:11:58.0

Updated: 2018-05-22 16:48:36.0

Pushed: 2018-05-22 14:32:47.0

Homepage: https://rawgit.com/boundlessgeo/ol-mapbox-style/v2.11.2/example/index.html

Size: 1010

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

ol-mapbox-style

Create OpenLayers maps from Mapbox Style objects.

Getting started

To use a standalone build of ol-mapbox-style, just include 'dist/olms.js' on your HTML page. Otherwise just import the ol-mapbox-style module, like in the snippet below.

The code below creates an OpenLayers map from Mapbox's Bright v9 style:

rt { apply } from 'ol-mapbox-style';

key = 'Your Mapbox Access Token here';
y('map', 'https://api.mapbox.com/styles/v1/mapbox/bright-v9?access_token=' + key);

Only commonly available system fonts and Google Fonts will automatically be available for text defined in the Mapbox Style object. It is the responsibility of the application to load other fonts.

To apply a subset of the layers defined in the Mapbox Style layer to a custom OpenLayers layer, use the applyStyle() function.

To apply the properties of the Mapbox Style's background layer to the map, use the applyBackground() function.

To create a style function for individual OpenLayers vector or vector tile layers, use the stylefunction module:

rt stylefuction from 'ol-mapbox-style/stylefunction';
penLayers imports from https://npmjs.com/package/ol
rt VectorLayer from 'ol/layer/Vector';
rt VectorSource from 'ol/source/Vector';
rt GeoJSON from 'ol/format/GeoJSON';

layer = new VectorLayer({
urce: new VectorSource({
format: new GeoJSON(),
url: 'data/states.geojson'



h('data/states.json').then(function(response) {
sponse.json().then(function(glStyle) {
stylefunction(layer, glStyle, 'states');
;

API
applyStyle

Applies a style function to an ol.layer.VectorTile or ol.layer.Vector with an ol.source.VectorTile or an ol.source.Vector. The style function will render all layers from the glStyle object that use the specified source, or a subset of layers from the same source. The source needs to be a "type": "vector", "type": "geojson" or "type": "raster" source.

Parameters

Returns Promise Promise which will be resolved when the style can be used for rendering.

applyBackground

Applies properties of the Mapbox Style's first background layer to the map.

Parameters

apply

Loads and applies a Mapbox Style object to an OpenLayers Map. This includes the map background, the layers, the center and the zoom.

The center and zoom will only be set if present in the Mapbox Style document, and if not already set on the OpenLayers map.

Layers will be added to the OpenLayers map, without affecting any layers that might already be set on the map.

Layers added by apply() will have two additional properties:

The map returned by this function will have an additional mapbox-style property which holds the Mapbox Style object.

Parameters

Returns ol.Map The OpenLayers Map instance that will be populated with the contents described in the Mapbox Style object.

getLayer

Get the OpenLayers layer instance that contains the provided Mapbox Style layer. Note that multiple Mapbox Style layers are combined in a single OpenLayers layer instance when they use the same Mapbox Style source.

Parameters

Returns ol.layer.Layer layer OpenLayers layer instance.

getSource

Get the OpenLayers source instance for the provided Mapbox Style source.

Parameters

Returns ol.layer.Layer layer OpenLayers layer instance.

stylefunction

Creates a style function from the glStyle object for all layers that use the specified source, which needs to be a "type": "vector" or "type": "geojson" source and applies it to the specified OpenLayers layer.

Parameters

Returns ol.style.StyleFunction Style function for use in ol.layer.Vector or ol.layer.VectorTile.

Building the library
npm install

The resulting binary (olms.js) will be in the dist/ folder. To see the library in action, navigate to example/index.html.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.