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

leaflet-declarative-layers

v0.5.0

Published

A Leaflet plugin to make adding map layers easier. Users just need to describe their data with an array of JSON 'metadata' objects rather than adding each layer with redundant imperative code. Access to map layer references are available, so a user may st

Downloads

1

Readme

A Leaflet plugin to make adding map layers easier. Users just need to describe their data with an array of JSON 'metadata' objects rather than adding each layer with redundant imperative code. Access to map layer references are available, so a user may still access full Leaflet layer methods and features. This project is definitely a work-in-progress and open to contributions.

This documentation is also a work-in-progress. The intention is to keep it current, but its likely already out of date. Please submit an issue or pull request if you notice any needed improvements! 

</div>

Project setup

npm install leaflet
npm install

Lint, test, bundle (minification is a to do item still)

npm run build

Bundle only

npm run bundle

Run unit tests

npm run test

Lints and fixes files

npm run lint

Usage

Add the script to your file:

<script src="../dist/leafletDeclarativeLayers.js"></script>

(For now, first, you may need to run npm run build to compile the leafletDeclarativeLayers.js script. Eventually we will make the library available by other conventional means.)

Describe your data with a 'metadata' JSON object. Available properties described below in the Properties section.

// data to be added, in this case, geoJSON
const geojsonFeatureCollection = {
    type: 'FeatureCollection',
    features: [
        {
            type: 'Feature',
            properties: {
                popupContent: '18th & California Light Rail Stop'
            },
            geometry: {
                type: 'Point',
                coordinates: [-115, 49.5]
            },
        },
    ],
};
// the 'metadata'. An array of JSON objects that describes the data to be added to the map.
const layerMetadata = [
    {
        id: 'tileLayer',
        label: 'Tile Layer Example',
        url: 'https://stamen-tiles.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}.png',
        visibleInitially: true,
    }, 
    {
        id: 'geoJsonLayer',
        label: 'GeoJSON Layer Example',
        data: geojsonFeatureCollection,
        visibleInitially: true,
        generateFeaturePopupContent: (feature) => {
        return feature.properties.popupContent;
        },

    }
];

Initialize a Leaflet map, and then a new DeclarativeLayers instance

const map = new L.Map('map', { zoom: 8, center: new L.LatLng(49.5, -115)});
const basemap = new L.TileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {minZoom: 8, maxZoom: 12});		
map.addLayer(basemap); 

// initialize leaflet-declarative-layers with the map, and the metadata array
const declaredLayers = new L.DeclarativeLayers(map, layerMetadata); 

One can access any of the layer references by the id used in the metadata

declaredLayers.getLayerReferences().tileLayer This means you can use it like you would any other Leaflet layer. For example: declaredLayers.getLayerReferences().tileLayer.redraw()

Methods

|Method|Returns|Description| |---|---|---| |getLayerReferences()| Object of references to Leaflet layers|Returns an Object containing references to all of the Leaflet layers added via the DeclarativeLayers class. Individual layers will be accessible by their id as specified in the metadata. |addLayer({metadata})| Leaflet layer reference for added layer|Adds a single layer to the map. A single metadata object parameter is required.| |removeLayer(Leaflet layer reference) |void| Removes a layer from the map. The required Leaflet layer reference parameter can be obtained from the getLayerReference method or via other Leaflet methods.

Currently Available Metadata Properties

Tile Layers (documentation to come)

Image Layers (documentation to come)

GeoJson Layers

|Property|Required/Optional|Description| |---|---|---| |data|Required| A GeoJson object| |options| Optional|A Leaflet GeoJsonOptions object. See Leaflet documentation.| |generateFeaturePopupContent(function(feature){})|Optional| A function to generate a Leaflet pop up window for each GeoJson feature in a layer when it is clicked. The single required parameter is a function that returns either a string or HTML element. E.g.: generateFeaturePopupContent: (feature) => { return feature.properties.popupContent;}, or generateFeaturePopupContent: () => { return 'I'm a string'},