mapbox使用v3版本,v2的样式切换不同时间段

  1. 创建DayAndNight.js

    javascript 复制代码
    /**
     * 使用方式
     * const dayNight = new DayAndNight({ map: map // map 地图对象}) 
     * 修改类型
     * dayNight.setConfigProperty(value)
     */
    class DayAndNight {
      constructor (sdMap) {
        this.map = sdMap.map
    
        this.initStyle()
      }
    
      // 初始化时添加必要样式
      initStyle () {
        const map = this.map
    
        // 添加一个方向灯
        map.setLights([
          {
            "id": "ambient",
            "type": "ambient",
            "properties": {
              "color": [
                "match",
                [
                  "config",
                  "lightPreset"
                ],
                "dawn",
                "hsl(28, 98%, 93%)",
                "day",
                "hsl(0, 0%, 100%)",
                "dusk",
                "hsl(228, 27%, 29%)",
                "night",
                "hsl(217, 100%, 11%)",
                "hsl(0, 0%, 100%)"
              ],
              "intensity": [
                "match",
                [
                  "config",
                  "lightPreset"
                ],
                "dawn",
                0.75,
                "day",
                0.8,
                "dusk",
                0.8,
                "night",
                0.5,
                0.8
              ]
            }
          },
          {
            "id": "directional",
            "type": "directional",
            "properties": {
              "direction": [
                "match",
                [
                  "config",
                  "lightPreset"
                ],
                "dawn",
                [
                  "literal",
                  [
                    120,
                    50
                  ]
                ],
                "day",
                [
                  "literal",
                  [
                    180,
                    20
                  ]
                ],
                "dusk",
                [
                  "literal",
                  [
                    240,
                    80
                  ]
                ],
                "night",
                [
                  "literal",
                  [
                    270,
                    20
                  ]
                ],
                [
                  "literal",
                  [
                    180,
                    20
                  ]
                ]
              ],
              "color": [
                "match",
                [
                  "config",
                  "lightPreset"
                ],
                "dawn",
                "hsl(33, 98%, 77%)",
                "day",
                "hsl(0, 0%, 100%)",
                "dusk",
                "hsl(30, 98%, 76%)",
                "night",
                "hsl(0, 0%, 29%)",
                "hsl(0, 0%, 100%)"
              ],
              "intensity": [
                "interpolate",
                [
                  "linear"
                ],
                [
                  "zoom"
                ],
                12,
                [
                  "match",
                  [
                    "config",
                    "lightPreset"
                  ],
                  "dawn",
                  0.5,
                  "day",
                  0.2,
                  "dusk",
                  0,
                  "night",
                  0,
                  0.2
                ],
                14,
                [
                  "match",
                  [
                    "config",
                    "lightPreset"
                  ],
                  "dawn",
                  0.5,
                  "day",
                  0.2,
                  "dusk",
                  0.2,
                  "night",
                  0.5,
                  0.2
                ]
              ],
              "cast-shadows": true,
              "shadow-intensity": [
                "match",
                [
                  "config",
                  "lightPreset"
                ],
                "night",
                0.5,
                1
              ]
            }
          }
        ]);
        // 添加雾
        map.setFog({
          "vertical-range": [
            30,
            120
          ],
          "range": [
            "interpolate",
            [
              "linear"
            ],
            [
              "zoom"
            ],
            13,
            [
              "literal",
              [
                1,
                10
              ]
            ],
            15,
            [
              "literal",
              [
                1,
                4
              ]
            ]
          ],
          "color": [
            "interpolate",
            [
              "exponential",
              1.2
            ],
            [
              "zoom"
            ],
            5,
            [
              "interpolate",
              [
                "linear"
              ],
              [
                "measure-light",
                "brightness"
              ],
              0.1,
              "hsla(240, 9%, 55%, 1)",
              0.4,
              "hsla(0, 0%, 100%, 1)"
            ],
            7,
            [
              "interpolate",
              [
                "linear"
              ],
              [
                "measure-light",
                "brightness"
              ],
              0.02,
              "hsla(213, 63%, 20%, 0.9)",
              0.03,
              "hsla(30, 65%, 60%, 0.5)",
              0.4,
              "hsla(10, 79%, 88%, 0.95)",
              0.45,
              "hsla(200, 60%, 98%, 0.9)"
            ]
          ],
          "high-color": [
            "interpolate",
            [
              "exponential",
              1.2
            ],
            [
              "zoom"
            ],
            5,
            [
              "interpolate",
              [
                "linear"
              ],
              [
                "measure-light",
                "brightness"
              ],
              0.1,
              "hsla(215, 100%, 20%, 1)",
              0.4,
              "hsla(215, 100%, 51%, 1)"
            ],
            7,
            [
              "interpolate",
              [
                "linear"
              ],
              [
                "measure-light",
                "brightness"
              ],
              0,
              "hsla(228, 38%, 20%, 1)",
              0.05,
              "hsla(360, 100%, 85%, 1)",
              0.2,
              "hsla(205, 88%, 86%, 1)",
              0.4,
              "hsla(270, 65%, 85%, 1)",
              0.45,
              "hsla(0, 0%, 100%, 1)"
            ]
          ],
          "space-color": [
            "interpolate",
            [
              "exponential",
              1.2
            ],
            [
              "zoom"
            ],
            5,
            "hsl(211, 84%, 9%)",
            7,
            [
              "interpolate",
              [
                "linear"
              ],
              [
                "measure-light",
                "brightness"
              ],
              0,
              "hsl(211, 84%, 17%)",
              0.2,
              "hsl(210, 40%, 30%)",
              0.4,
              "hsl(270, 45%, 98%)",
              0.45,
              "hsl(210, 100%, 80%)"
            ]
          ],
          "horizon-blend": [
            "interpolate",
            [
              "exponential",
              1.2
            ],
            [
              "zoom"
            ],
            5,
            0.01,
            7,
            [
              "interpolate",
              [
                "exponential",
                1.2
              ],
              [
                "measure-light",
                "brightness"
              ],
              0.35,
              0.03,
              0.4,
              0.1,
              0.45,
              0.03
            ]
          ],
          "star-intensity": [
            "interpolate",
            [
              "exponential",
              1.2
            ],
            [
              "zoom"
            ],
            5,
            0.4,
            7,
            [
              "interpolate",
              [
                "exponential",
                1.2
              ],
              [
                "measure-light",
                "brightness"
              ],
              0.1,
              0.2,
              0.3,
              0
            ]
          ]
        });
        // 修改相机类型
        map.setCamera({
          "camera-projection": "orthographic", // perspective 默认
        });
        // 默认白天
        map.setConfigProperty('', 'lightPreset', "day")
      }
    
      /**
       * 修改
       * @param {*} value 
       * day    白天
       * night  夜晚
       * dawn   破晓
       * dusk   黄昏
       */
      setConfigProperty(value){
        const map = this.map
    
        map.setConfigProperty('', 'lightPreset', value);
      }
    }
  2. 使用

    javascript 复制代码
    <!DOCTYPE html>
    <html lang="zh-CN">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <script src='https://api.mapbox.com/mapbox-gl-js/v3.0.0/mapbox-gl.js'></script>
      <link href='https://api.mapbox.com/mapbox-gl-js/v3.0.0/mapbox-gl.css' rel='stylesheet' />
      <style>
        body {
          margin: 0;
          padding: 0;
        }
    
        #map {
          position: absolute;
          top: 0;
          bottom: 0;
          width: 100%;
        }
    
        #menu {
          position: absolute;
          background: #efefef;
          padding: 10px;
          font-family: 'Open Sans', sans-serif;
          top: 10px;
          left: 10px;
        }
    
        .map-ctrl-zoom {
          color: #a4a4a4;
          font-weight: bold;
          padding: 4px 8px;
          user-select: none;
          background: rgb(28, 31, 36);
          margin: 10px 10px 0 0;
        }
    
        .mapboxgl-ctrl-logo {
          display: none !important;
        }
        #custom-scale {
          position: absolute;
          bottom: 10px;
          left: 10px;
          background-color: rgba(255, 255, 255, 0.8);
          padding: 5px 10px;
          border-radius: 3px;
          font-size: 12px;
          font-family: Arial, sans-serif;
        }
      </style>
    </head>
    
    <body>
      <div id='map'></div>
      <div id="menu">
        <input id="day" type="radio" name="rtoggle" value="day" checked="checked">
        <label for="day">白天</label>
        <input id="night" type="radio" name="rtoggle" value="night">
        <label for="night">夜晚</label>
        <input id="dawn" type="radio" name="rtoggle" value="dawn">
        <label for="dawn">破晓</label>
        <input id="dusk" type="radio" name="rtoggle" value="dusk">
        <label for="dusk">黄昏</label>
      </div>
      <div id='custom-scale'></div>
    </body>
    
    
    <script src="./js/DayAndNight.js"></script>
    <script>
      mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
      const map = new mapboxgl.Map({
        container: 'map', // container ID
        center: [106.1813231568759, 29.556810823148066],
        zoom: 17,
        pitch: 60,
        attributionControl: false,
        style: `http://`
      });
    
      map.on('style.load', () => {
        const dayNight = new DayAndNight({
          map: map,
        })
    
        const itemList = document.getElementById('menu');
        const inputs = itemList.getElementsByTagName('input');
    
        for (const input of inputs) {
          input.onclick = (layer) => {
            const value = layer.target.value;
            dayNight.setConfigProperty(value)
          };
        }
      });
    </script>
    
    </html>


相关推荐
PYB32 小时前
【Web·JS·基础】函数的使用和展运算符...objs
前端·javascript
默_笙3 小时前
🚥 给 RAG 装一台心电监护仪:LangSmith 全链路观测(上)
前端·javascript
变与不变8065 小时前
js同步和异步难点重点详解
开发语言·javascript·ecmascript
Dovis(誓平步青云)5 小时前
多个链接不等于多份证据,新闻核验看板怎样合并来源
java·服务器·前端·javascript·人工智能·pdf·电脑
Dovis(誓平步青云)6 小时前
几个方案来回选不定?做一个随时切换的候选推荐页
android·java·服务器·开发语言·javascript·数据库·智能化
可乐鸡翅yeah_8 小时前
fmp4 碎片 MP4 格式 HLS 流,和传统 TS 分片有什么不一样
开发语言·javascript·ios·音视频·safari·m3u8·m3u8在线播放
ss2738 小时前
AI全栈实战 | 2.3-01 组件库实战:Element Plus 怎么搭出专业界面,过度封装为什么成维护负担
前端·javascript·elementui·vue
福兮说9 小时前
URL 编码的七个坑:c++ 传到后端变空格、%25 套娃、截断 emoji 直接报错
开发语言·前端·javascript·node.js·url
小的时候可菜了9 小时前
JS 异步的本质:从单线程到 thenable(一条进化链)
开发语言·前端·javascript
可乐鸡翅yeah_9 小时前
hls.js 配置 loadStartPosition,点播起始播放位置踩坑
开发语言·javascript·网络协议·ecmascript·m3u8在线