• 【MAPBOX基础功能】18、mapbox添加pbf矢量切片图层


    前言

    官网指引,生成accesstoken,下载相关依赖请翻阅[https://blog.csdn.net/weixin_44402694/article/details/125414381?spm=1001.2014.3001.5501](https://blog.csdn.net/weixin_44402694/article/details/125414381?spm=1001.2014.3001.5501)
    本文使用官网accesstoken,请自行生成私人token
    mapbox添加pbf矢量切片图层
    
    • 1
    • 2
    • 3

    效果

    在这里插入图片描述

    代码实现

    <!DOCTYPE html>
    <html lang="en">
      <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>18、添加pbf矢量切片图层</title>
        <link
          href="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.css"
          rel="stylesheet"
        />
        <script src="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.js"></script>
        <style>
          * {
            padding: 0;
            margin: 0;
            list-style: none;
            text-decoration: none;
          }
          html,
          body {
            width: 100%;
            height: 100%;
          }
          #map {
            width: 100%;
            height: 100%;
          }
          .btn-list {
            position: fixed;
            top: 100px;
            left: 100px;
          }
        </style>
      </head>
      <body>
        <div id="map"></div>
        <ul class="btn-list">
          <li>
            <button onclick="add()">添加</button>
          </li>
          <li>
            <button onclick="remove()">删除</button>
          </li>
        </ul>
        <script>
          mapboxgl.accessToken =
            'pk.eyJ1Ijoid2FuZ3Rvbmd4dWUiLCJhIjoiY2pzY3E2M2k0MDk3NzN5dDA0Nmtia2h0cCJ9.oP9fEJxOgVzm0dWGvL6tGg'
          const map = new mapboxgl.Map({
            container: 'map', // 容器 id
            style: 'mapbox://styles/mapbox/satellite-v9', // mapbox底图
            center: [108, 35], // 初始化中心点
            zoom: 4, // 初始化层级
          })
          // 添加
          function add() {
            if (map.getSource('railway_source')) return
            map.addSource('railway_source', {
              type: 'vector',
              tiles: [
                // 切片服务地址tagola切片
                `http://xxxx/tagola/maps/railway-maps/railway/{z}/{x}/{y}.pbf`,
              ],
              minzoom: 3,
              maxzoom: 16,
            })
            map.addLayer({
              id: 'railway_bg',
              type: 'line',
              source: 'railway_source',
              'source-layer': 'railway',
              layout: {
                'line-cap': 'round',
                'line-join': 'round',
              },
              paint: {
                'line-color': '#94928C',
                'line-width': {
                  stops: [
                    [3, 1],
                    [11, 3],
                    [22, 4],
                  ],
                },
              },
            })
            map.addLayer({
              id: 'railway',
              type: 'line',
              source: 'railway_source',
              'source-layer': 'railway',
              layout: {
                'line-cap': 'round',
                'line-join': 'round',
              },
              paint: {
                'line-color': '#fff',
                'line-width': {
                  stops: [
                    [3, 0.5],
                    [11, 2],
                    [22, 3],
                  ],
                },
                'line-dasharray': [2, 7],
              },
            })
          }
          // 删除
          function remove() {
            if (!map.getSource('railway_source')) {
              return alert('请先添加')
            }
            map.removeLayer('railway_bg')
            map.removeLayer('railway')
            map.removeSource('railway_source')
          }
        </script>
      </body>
    </html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
  • 相关阅读:
    电脑硬盘就一个c盘怎么分区,新电脑买回来只有一个c盘怎么分区
    uniapp app 导出excel 表格
    javafx+JDBC实现新生报到管理系统
    赞奇科技出席江苏828 B2B企业服务峰会,助力企业数字化转型
    微服务(服务治理)
    Docker命令
    以太坊智能合约方法初探
    Linear、Logistic回归
    分类树,我从2s优化到0.1s
    22.11.18打卡 [传智杯 #3 初赛] 部分题
  • 原文地址:https://blog.csdn.net/weixin_44402694/article/details/125421042