npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mapbox-helper

v1.1.0

Published

Some utilities to simplify repeated mapbox methods and patterns and to Promisify some actions like adding sources and adding layers

Downloads

170

Readme

Mapbox helper

This module will supply some helper functions to help developers working with Mapbox GL JS.

It's very much a work in progress and currently exports one method, addSourceAndLayers, which allows you to combine mapbox's native addSource and addLayer methods into one function, with the ability to add one layer or more at a time (with the same source).

It also Promisifies the adding of the source and the adding of the layer(s). A Promise is resolved within the local scope of the function when the source is successfully added to the map, and the layers are added only after that resolution. The function returns a Promise to the outer scope (your code) which resolves true when all the layers added are ready for further interaction. Visible layers resolve only after queryRenderedFeatures() returns truthy for the layer. You can chain your next actions onto the resolution of that Promise.

Important note: the module uses ES6 syntax and APIs. It's configured in its package.json and .babelrc file to be transpiled to work in browsers ( "last 2 versions", "> 1%" ) by babelify via Browserify when / if that is part of the buld process of the project that requires it. If that's not part of your process, the module, as far as I undestand, will be imported into your code without being transpiled first and so may not work in all browsers / runtime environments without futher doing on your end.

Why?

With mapbox, adding a source sometimes doesn't take effect quickly enough to immediately support adding a map layer based on it. Similarly, adding a layer sometimes doesn't take effect quickly enough for you to immediately interact with it. You can work around this by listening for the maps render event and checking on render if the source or layer exists before continuing.

That's tedious and repetitive. This method makes it less so.

How to use

Install it

npm install mapbox-helper --save-dev

Import it

// your code
var mbHelper = require('mapbox-helper');
// your code

Use it

Call the addSourceAndLayers method with the map as the context and two parameters, the source config object and an array of layer config objects (or just one):

mbHelper.addSourceAndLayers.call(<map reference>, <source config object>, <array of layer config objects>);

Example:

var addLayers = mbHelper.addSourceAndLayers.call(map,
    { // source
        "type": "vector",
        "url": "mapbox://mapbox.us_census_states_2015",
        "name": "states"
    }, [ // layers
        { // layer one
            "id": "states-join",
            "type": "fill",
            "source-layer": 'states',
            "paint": {
              "fill-color": 'transparent'
            },
            "beforeLayer": "water" // <== this is different from mapbox native specs
        },
        { // layer two
            "id": "states-join-hover",
            "type": "line",
            "source-layer": 'states',
            "paint": {
                "line-color": '#4D90FE',
                "line-width": 4,
                "line-blur": 2
            },
            "filter": ["==", "name", ""]
        }
    ]);

The config object for the source is the same as when using the native mapbox methods; the config objects for the layers are the same, except for the way you specify which map layer the new layer should be placed before (under). Natively, that is specified by passing in the name as an argument after the config object. Here, it is included as a property of the config object. See the note above in the code.

addSourceAndLayers returns a Promise that resolves when the layers are ready to be intereactive with, so you can chain .then() onto it to handle subsequent actions:

addLayers.then(() => {
  // do some stuff
});

Or chain it directly onto the function call:

mbHelper.addSourceAndLayers.call(map,
    { // source
        "type": "vector",
        "url": "mapbox://mapbox.us_census_states_2015",
        "name": "states"
    }, [ // layers
        { // layer one
            "id": "states-join",
            "type": "fill",
            "source-layer": 'states',
            "paint": {
              "fill-color": 'transparent'
            },
            "beforeLayer": "water" // <== this is different from mapbox native specs
        },
        { // layer two
            "id": "states-join-hover",
            "type": "line",
            "source-layer": 'states',
            "paint": {
                "line-color": '#4D90FE',
                "line-width": 4,
                "line-blur": 2
            },
            "filter": ["==", "name", ""]
        }
    ]).then(() => {
        // do some stuff
        });

Thanks. Feedback welcome.