项目作者: AdriSolid

项目描述 :
Mapbox GL JS Layers Control Plugin
高级语言: TypeScript
项目地址: git://github.com/AdriSolid/mapbox-gl-layers-control.git
创建时间: 2020-09-12T15:59:36Z
项目社区:https://github.com/AdriSolid/mapbox-gl-layers-control

开源协议:

下载


@adrisolid/mapbox-gl-layers-control

Mapbox GL JS Layers Control


I need some caffeine to work :)


Buy Me a Coffee at ko-fi.com


Demo

thumb

Features

  • Create a layers control by wrapping Mapbox GL JS layers id’s.
  • Layers could be grouped. If all layers of a group are visible ‘select all’ checkbox will be checked, otherwise, will be unchecked.
  • Visibility is firstly controled under visibility layout property, if this property does not exists, Mapbox GL JS assumes that is visible.

Getting Started

  1. yarn add @adrisolid/mapbox-gl-layers-control

Usage with Vanilla JS:

  1. import { Map } from "mapbox-gl";
  2. import { MapboxLayersControl } from "@adrisolid/mapbox-gl-layers-control";
  3. import "@adrisolid/mapbox-gl-layers-control/styles.css";
  4. const LAYERS_INFO = [
  5. {
  6. id: "buildings-1",
  7. color: "red",
  8. xtrsnH: ["/", ["get", "height"], 2],
  9. xtrsB: ["get", "min_height"],
  10. },
  11. {
  12. id: "buildings-2",
  13. color: "orange",
  14. xtrsnH: ["get", "height"],
  15. xtrsB: ["/", ["get", "height"], 2],
  16. },
  17. {
  18. id: "buildings-3",
  19. color: "lightblue",
  20. xtrsnH: ["*", ["get", "height"], 1.5],
  21. xtrsB: ["get", "height"],
  22. },
  23. ];
  24. const map = new Map({
  25. style: "mapbox://styles/mapbox/dark-v10",
  26. center: [-74.0066, 40.7135],
  27. zoom: 15.5,
  28. pitch: 45,
  29. bearing: -17.6,
  30. container: "map",
  31. antialias: true,
  32. });
  33. function layerGenerator(layers) {
  34. layers.forEach((layer) => {
  35. map.addLayer({
  36. id: layer.id,
  37. source: "composite",
  38. "source-layer": "building",
  39. filter: ["==", "extrude", "true"],
  40. type: "fill-extrusion",
  41. minzoom: 15,
  42. paint: {
  43. "fill-extrusion-color": layer.color,
  44. "fill-extrusion-height": layer.xtrsnH,
  45. "fill-extrusion-base": layer.xtrsB,
  46. "fill-extrusion-opacity": 0.6,
  47. },
  48. });
  49. });
  50. }
  51. map.on("load", function () {
  52. layerGenerator(LAYERS_INFO);
  53. map.addControl(
  54. new MapboxLayersControl({
  55. title: "Floors",
  56. layersDefinition: [
  57. {
  58. name: "Select all",
  59. group: true,
  60. children: [
  61. {
  62. id: "buildings-1",
  63. name: "First floor",
  64. },
  65. {
  66. id: "buildings-2",
  67. name: "Second floor",
  68. },
  69. {
  70. id: "buildings-3",
  71. name: "Third floor",
  72. },
  73. ],
  74. },
  75. ],
  76. }),
  77. );
  78. });

Usage with React

Check this out :)

Properties:

  • title (Default: ""): string Layers control title
  • layersDefinition (Default: {}): LayersDefinition Layers control definition. Following these types:

    1. type LayersInfo = Array<{ id: string; name: string }>;
    2. type LayersDefinition = Array<{ children: LayersInfo; group?: boolean; name?: string }>;