Giter Club home page Giter Club logo

ol-mapbox-style's Introduction

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:

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

var key = 'Your Mapbox Access Token here';
apply('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.

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

  • layer ol.layer.VectorTile OpenLayers layer.
  • glStyle (string | Object) Mapbox Style object.
  • source string source key or an array of layer ids from the Mapbox Style object. When a source key is provided, all layers for the specified source will be included in the style function. When layer ids are provided, they must be from layers that use the same source.
  • path string Path of the style file. Only required when a relative path is used with the "sprite" property of the style. (optional, default undefined)

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

  • map ol.Map OpenLayers Map.
  • glStyle Object Mapbox Style object.

apply

Loads and applies a Mapbox Style object to an OpenLayers Map.

Parameters

  • map (ol.Map | HTMLElement | string) Either an existing OpenLayers Map instance, or a HTML element, or the id of a HTML element that will be the target of a new OpenLayers Map.
  • style (string | Object) JSON style object or style url pointing to a Mapbox Style object. When using Mapbox APIs, the url must contain an access token and look like https://api.mapbox.com/styles/v1/mapbox/bright-v9?access_token=[your_access_token_here].

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

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.