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

@wabson/mapbox-gl-feature-info

v0.0.8

Published

Mapbox GL JS custom controls to display and allow editing of information about selected features in Mapbox Draw

Downloads

146

Readme

Mapbox Draw GL JS Feature Information

This project provides a set of custom Mapbox GL JS controls, to display and allow editing of information about selected features, for use with the Mapbox Draw GL JS editor.

  • See line lengths updated as you draw out the line or select single or multiple features

  • Assign feature names and other metadata, which will be stored as properties in the output GeoJSON

  • Additional tools to join together, extend or cut lines into multiple parts

View demo page

Usage

Include the library in your project

npm install @wabson/mapbox-gl-feature-info

Using <script> tags

<script src="node_modules/@wabson/mapbox-gl-feature-info/dist/lib.bundle.js" type="application/javascript"></script>

Or ES6 import

import { LineStringInfoControl, PointInfoControl, MultiLineInfoControl, DrawNamedLineMode } from '@wabson/mapbox-gl-feature-info';

Add a basic LineString distance indicator to your map

var map = new mapboxgl.Map({
    container: 'map',
    ...
});

var draw = new MapboxDraw({
    displayControlsDefault: false,
    controls: {
        point: true,
        line_string: true,
        trash: true
    },
    ...
});

var LineStringInfoControl = mapboxglFeatureInfo.LineStringInfoControl; // only for <script> tag method
map.on('load', () => {
    map.addControl(draw);
    map.addControl(new LineStringInfoControl({
        distanceUnits: 'kilometers',
        drawControl: draw
    }));

Valid distance units are miles, kilometers and none (if you don't want distances to be calculated). Internally Turf.js is used to calculate lengths.

You must pass in the Draw instance to LineStringInfoControl and all other custom controls via the drawControl property in the contructor.

Other controls

The other controls provided are PointInfoControl for showing and editing properties of a Point and MultiLineInfoControl for multiple selected LineStrings.

Displaying distances while drawing lines

A custom Draw mode is provided to fire events whilst drawing lines, in order to allow the distance to be displayed in real time. To enable this, you must override Draw's modes property:

var draw = new MapboxDraw({
    displayControlsDefault: false,
    controls: {
        point: true,
        line_string: true,
        trash: true
    },
    modes: Object.assign({}, MapboxDraw.modes, {'draw_line_string': DrawNamedLineMode}),
    ...
});

Viewing and editing feature properties

The controls support setting and showing property values of the drawn features, which Draw lacks direct support for.

To allow editing of properties, configure the editProperties property of the controls, for example

map.addControl(new PointInfoControl({
    drawControl: draw,
    editProperties: [
        {
            name: 'name',
            label: 'Name'
        }
    ],
    defaultTitle: 'Untitled'
}));

Any valid GeoJSON property names and form labels are supported, however the name property if present will be displayed as the feature title bar of the custom controls.

The defaultTitle property allows customising the placeholder that will be used if the selected feature has no name, the default is based on the type of feature, e.g. LineStrings will show as 'Line'.

Prompting for a name before drawing

In some circumstances you may want to make it a requirement for the user to enter a name before they start drawing a feature, or at least to allow them the option of entering a name before they start drawing.

To set this up, set either of the following properties on the imported DrawNamedLineMode object, prior to passing it in the map modes as above, i.e.

DrawNamedLineMode.isNameRequired = true;

or

DrawNamedLineMode.showNamePrompt = true;