mapbox进阶技巧

1、filter过滤,使用过滤,可以根据不同条件展示我们想要展示的数据

代码示例:核心代码,根据点的size是否满足条件进行展示

filter: ['<=', 'size', 30]
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
    <script src="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js"></script>
    <link
      href="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css"
      rel="stylesheet"
    />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #map {
        width: 100vw;
        height: 100vh;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      mapboxgl.accessToken = '你的token'
      const gaode = {
        version: 8,
        sources: {
          'raster-tiles': {
            type: 'raster',
            tiles: ['http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}'],
            tileSize: 256
          }
        },
        layers: [
          {
            id: 'simple-tiles',
            type: 'raster',
            source: 'raster-tiles',
            minzoom: 0,
            maxzoom: 22
          }
        ]
      }
      const map = new mapboxgl.Map({
        container: 'map',
        style: gaode,
        center: [114.3, 30.5],
        zoom: 10
      })
      let data = {
        type: 'FeatureCollection',
        features: [
          {
            type: 'Feature',
            properties: {
              size: 20,
              name: '武汉'
            },
            geometry: {
              type: 'Point',
              coordinates: [114.30122, 30.598213]
            }
          },
          {
            type: 'Feature',
            properties: {
              size: 30,
              name: '苏州'
            },
            geometry: {
              type: 'Point',
              coordinates: [114.526261, 30.544391]
            }
          },
          {
            type: 'Feature',
            properties: {
              size: 40
            },
            geometry: {
              type: 'Point',
              coordinates: [114.335601, 30.436657]
            }
          }
        ]
      }
      async function renderLayer() {
        await map.once('style.load')
        map.addSource('source', {
          type: 'geojson',
          data: data
        })
        map.addLayer({
          id: 'layer',
          type: 'circle',
          source: 'source',
          paint: {
            'circle-radius': ['get', 'size'],
            'circle-color': '#ff0000'
          },
          filter: ['<=', 'size', 30]
        })
      }
      renderLayer()
    </script>
  </body>
</html>

has的使用,有,加!表示取反

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
    <script src="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js"></script>
    <link
      href="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css"
      rel="stylesheet"
    />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #map {
        width: 100vw;
        height: 100vh;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      mapboxgl.accessToken = '你的token'
      const gaode = {
        version: 8,
        sources: {
          'raster-tiles': {
            type: 'raster',
            tiles: ['http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}'],
            tileSize: 256
          }
        },
        layers: [
          {
            id: 'simple-tiles',
            type: 'raster',
            source: 'raster-tiles',
            minzoom: 0,
            maxzoom: 22
          }
        ]
      }
      const map = new mapboxgl.Map({
        container: 'map',
        style: gaode,
        center: [114.3, 30.5],
        zoom: 10
      })
      let data = {
        type: 'FeatureCollection',
        features: [
          {
            type: 'Feature',
            properties: {
              size: 20,
              name: '武汉'
            },
            geometry: {
              type: 'Point',
              coordinates: [114.30122, 30.598213]
            }
          },
          {
            type: 'Feature',
            properties: {
              size: 30,
              name: '苏州'
            },
            geometry: {
              type: 'Point',
              coordinates: [114.526261, 30.544391]
            }
          },
          {
            type: 'Feature',
            properties: {
              size: 40
            },
            geometry: {
              type: 'Point',
              coordinates: [114.335601, 30.436657]
            }
          }
        ]
      }
      async function renderLayer() {
        await map.once('style.load')
        map.addSource('source', {
          type: 'geojson',
          data: data
        })
        map.addLayer({
          id: 'layer',
          type: 'circle',
          source: 'source',
          paint: {
            'circle-radius': ['get', 'size'],
            'circle-color': '#ff0000'
          },
          // filter: ['has', 'name']
          filter: ['!', ['has', 'name']]
        })
      }
      renderLayer()
    </script>
  </body>
</html>

2、$type的使用,当点线面数据在一起的时候,可以根据不同的类型加载不同的数据

核心代码:

// 选择type为LineString的要素
          filter: ['==', '$type', 'LineString']
 // 选择type为Polygon的要素
          filter: ['==', '$type', 'Polygon']
// 选择type为Point的要素
          // filter: ['==', '$type', 'Point']
          filter: ['in', '$type', 'Point']

代码示例

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
    <script src="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js"></script>
    <link
      href="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css"
      rel="stylesheet"
    />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #map {
        width: 100vw;
        height: 100vh;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      mapboxgl.accessToken = '你的token'
      const gaode = {
        version: 8,
        sources: {
          'raster-tiles': {
            type: 'raster',
            tiles: ['http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}'],
            tileSize: 256
          }
        },
        layers: [
          {
            id: 'simple-tiles',
            type: 'raster',
            source: 'raster-tiles',
            minzoom: 0,
            maxzoom: 22
          }
        ]
      }
      const map = new mapboxgl.Map({
        container: 'map',
        style: gaode,
        center: [114.3, 30.5],
        zoom: 10
      })
      let data = {
        type: 'FeatureCollection',
        features: [
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [114.3289, 30.527034]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [114.328232, 30.523147]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'LineString',
              coordinates: [
                [114.317704, 30.519836],
                [114.335584, 30.517821],
                [114.33525, 30.514366]
              ]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Polygon',
              coordinates: [
                [
                  [114.302497, 30.513502],
                  [114.302497, 30.521564],
                  [114.311186, 30.521564],
                  [114.311186, 30.513502],
                  [114.302497, 30.513502]
                ]
              ]
            }
          }
        ]
      }
      async function renderLayer() {
        await map.once('style.load')
        map.addSource('source', {
          type: 'geojson',
          data: data
        })
        map.addLayer({
          id: 'layer',
          type: 'circle',
          source: 'source',
          paint: {
            'circle-radius': ['get', 'size'],
            'circle-color': '#ff0000'
          },
          // 选择type为Point的要素
          // filter: ['==', '$type', 'Point']
          filter: ['in', '$type', 'Point']
        })
        map.addLayer({
          id: 'layer2',
          type: 'fill',
          source: 'source',
          paint: {
            'fill-color': '#00ff00'
          },
          // 选择type为Polygon的要素
          filter: ['==', '$type', 'Polygon']
        })
        map.addLayer({
          id: 'layer3',
          type: 'line',
          source: 'source',
          paint: {
            'line-color': '#0000ff'
          },
          // 选择type为LineString的要素
          filter: ['==', '$type', 'LineString']
        })
      }
      renderLayer()
    </script>
  </body>
</html>

3、动态属性stops的使用

核心代码:

 paint: {
            'circle-radius': 20,
            'circle-color': {
              property: 'size',
              stops: [
                /* <5  green */
                [5, 'green'],
                /* 5~10  green-yellow之间渐变 */
                [10, 'yellow'],
                /* 10~20  yellow-red渐变 */
                [20, 'red']
                /* >20  就是red */
              ]
            }
          }

示例代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
    <script src="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js"></script>
    <link
      href="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css"
      rel="stylesheet"
    />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #map {
        width: 100vw;
        height: 100vh;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      mapboxgl.accessToken = '你的token'
      const gaode = {
        version: 8,
        sources: {
          'raster-tiles': {
            type: 'raster',
            tiles: ['http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}'],
            tileSize: 256
          }
        },
        layers: [
          {
            id: 'simple-tiles',
            type: 'raster',
            source: 'raster-tiles',
            minzoom: 0,
            maxzoom: 22
          }
        ]
      }
      const map = new mapboxgl.Map({
        container: 'map',
        style: gaode,
        center: [114.3, 30.5],
        zoom: 10
      })
      var data = {
        type: 'FeatureCollection',
        features: [
          {
            type: 'Feature',
            properties: {
              size: 20
            },
            geometry: { type: 'Point', coordinates: [114.307232, 30.602333] }
          },
          {
            type: 'Feature',
            properties: {
              size: 8
            },
            geometry: { type: 'Point', coordinates: [113.421538, 30.716048] }
          },
          {
            type: 'Feature',
            properties: {
              size: 1
            },
            geometry: { type: 'Point', coordinates: [115.734918, 30.477093] }
          }
        ]
      }
      // 根据size属性设置不同的颜色
      async function renderLayer() {
        await map.once('style.load')
        map.addSource('source', {
          type: 'geojson',
          data: data
        })
        map.addLayer({
          id: 'layer',
          type: 'circle',
          source: 'source',
          paint: {
            'circle-radius': 20,
            'circle-color': {
              property: 'size',
              stops: [
                /* <5  green */
                [5, 'green'],
                /* 5~10  green-yellow之间渐变 */
                [10, 'yellow'],
                /* 10~20  yellow-red渐变 */
                [20, 'red']
                /* >20  就是red */
              ]
            }
          }
        })
      }
      renderLayer()
    </script>
  </body>
</html>

动态属性step的使用,作用与stops一致

核心代码:

        paint: {
            'circle-radius': 20,
            'circle-color': [
            'step', 
            ['get', 'size'], 
            '#ff0000', 
            1, 
            '#00ff00',
            8, 
            '#0000ff',
            20, 
            '#ff00ff']
         }

示例代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
    <script src="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js"></script>
    <link
      href="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css"
      rel="stylesheet"
    />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #map {
        width: 100vw;
        height: 100vh;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      mapboxgl.accessToken = '你的token'
      const gaode = {
        version: 8,
        sources: {
          'raster-tiles': {
            type: 'raster',
            tiles: ['http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}'],
            tileSize: 256
          }
        },
        layers: [
          {
            id: 'simple-tiles',
            type: 'raster',
            source: 'raster-tiles',
            minzoom: 0,
            maxzoom: 22
          }
        ]
      }
      const map = new mapboxgl.Map({
        container: 'map',
        style: gaode,
        center: [114.3, 30.5],
        zoom: 10
      })
      var data = {
        type: 'FeatureCollection',
        features: [
          {
            type: 'Feature',
            properties: {
              size: 20
            },
            geometry: { type: 'Point', coordinates: [114.307232, 30.602333] }
          },
          {
            type: 'Feature',
            properties: {
              size: 7
            },
            geometry: { type: 'Point', coordinates: [113.421538, 30.716048] }
          },
          {
            type: 'Feature',
            properties: {
              size: 10
            },
            geometry: { type: 'Point', coordinates: [115.734918, 30.477093] }
          }
        ]
      }
      // 根据size属性设置不同的颜色
      async function renderLayer() {
        await map.once('style.load')
        map.addSource('source', {
          type: 'geojson',
          data: data
        })
        map.addLayer({
          id: 'layer',
          type: 'circle',
          source: 'source',
          paint: {
            'circle-radius': 20,
            'circle-color': ['step', ['get', 'size'], '#ff0000', 1, '#00ff00', 8, '#0000ff', 20, '#ff00ff']
          }
        })
      }
      renderLayer()
    </script>
  </body>
</html>

动态属性linear的使用,作用与上面两个类似

核心代码:

 'circle-color': ['interpolate', ['linear'], ['get', 'size'], 7, 'blue', 10, 'red', 20, 'green']

示例代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
    <script src="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js"></script>
    <link
      href="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css"
      rel="stylesheet"
    />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #map {
        width: 100vw;
        height: 100vh;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      mapboxgl.accessToken = '你的token'
      const gaode = {
        version: 8,
        sources: {
          'raster-tiles': {
            type: 'raster',
            tiles: ['http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}'],
            tileSize: 256
          }
        },
        layers: [
          {
            id: 'simple-tiles',
            type: 'raster',
            source: 'raster-tiles',
            minzoom: 0,
            maxzoom: 22
          }
        ]
      }
      const map = new mapboxgl.Map({
        container: 'map',
        style: gaode,
        center: [114.3, 30.5],
        zoom: 10
      })
      var data = {
        type: 'FeatureCollection',
        features: [
          {
            type: 'Feature',
            properties: {
              size: 20
            },
            geometry: { type: 'Point', coordinates: [114.307232, 30.602333] }
          },
          {
            type: 'Feature',
            properties: {
              size: 7
            },
            geometry: { type: 'Point', coordinates: [113.421538, 30.716048] }
          },
          {
            type: 'Feature',
            properties: {
              size: 10
            },
            geometry: { type: 'Point', coordinates: [115.734918, 30.477093] }
          }
        ]
      }
      // 根据size属性设置不同的颜色
      async function renderLayer() {
        await map.once('style.load')
        map.addSource('source', {
          type: 'geojson',
          data: data
        })
        map.addLayer({
          id: 'layer',
          type: 'circle',
          source: 'source',
          paint: {
            'circle-radius': 20,
            'circle-color': ['interpolate', ['linear'], ['get', 'size'], 7, 'blue', 10, 'red', 20, 'green']
          }
        })
      }
      renderLayer()
    </script>
  </body>
</html>

或者根据地图zoom的变化设置样式

示例代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
    <script src="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js"></script>
    <link
      href="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css"
      rel="stylesheet"
    />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #map {
        width: 100vw;
        height: 100vh;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      mapboxgl.accessToken = '你的token'
      const gaode = {
        version: 8,
        sources: {
          'raster-tiles': {
            type: 'raster',
            tiles: ['http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}'],
            tileSize: 256
          }
        },
        layers: [
          {
            id: 'simple-tiles',
            type: 'raster',
            source: 'raster-tiles',
            minzoom: 0,
            maxzoom: 22
          }
        ]
      }
      const map = new mapboxgl.Map({
        container: 'map',
        style: gaode,
        center: [114.3, 30.5],
        zoom: 10
      })
      var data = {
        type: 'FeatureCollection',
        features: [
          {
            type: 'Feature',
            properties: {
              size: 20
            },
            geometry: { type: 'Point', coordinates: [114.307232, 30.602333] }
          },
          {
            type: 'Feature',
            properties: {
              size: 7
            },
            geometry: { type: 'Point', coordinates: [113.421538, 30.716048] }
          },
          {
            type: 'Feature',
            properties: {
              size: 10
            },
            geometry: { type: 'Point', coordinates: [115.734918, 30.477093] }
          }
        ]
      }
      // 根据size属性设置不同的颜色
      async function renderLayer() {
        await map.once('style.load')
        map.addSource('source', {
          type: 'geojson',
          data: data
        })
        map.addLayer({
          id: 'layer',
          source: 'source',
          type: 'circle',
          paint: {
            'circle-radius': 20,
            'circle-color': ['interpolate', ['linear'], ['zoom'], 4, 'green', 8, 'yellow']
          }
        })
      }
      renderLayer()
    </script>
  </body>
</html>

4、点聚合

核心代码:

 map.addSource('source', {
          type: 'geojson',
          data: data,
          // 开启点聚合
          cluster: true
        })
 paint: {
            'circle-radius': {
              property: 'point_count',
              stops: [
                [5, 10],
                [10, 20],
                [15, 30],
                [20, 40]
              ]
            },
            'circle-color': {
              property: 'point_count',
              stops: [
                [5, '#652e80'],
                [10, 'green'],
                [15, 'yellow'],
                [20, 'red']
              ]
            }
          },
          filter: ['has', 'point_count']

示例代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
    <script src="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js"></script>
    <link
      href="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css"
      rel="stylesheet"
    />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #map {
        width: 100vw;
        height: 100vh;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      mapboxgl.accessToken = '你的token'
      const gaode = {
        version: 8,
        sources: {
          'raster-tiles': {
            type: 'raster',
            tiles: ['http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}'],
            tileSize: 256
          }
        },
        layers: [
          {
            id: 'simple-tiles',
            type: 'raster',
            source: 'raster-tiles',
            minzoom: 0,
            maxzoom: 22
          }
        ]
      }
      const map = new mapboxgl.Map({
        container: 'map',
        style: gaode,
        center: [114.3, 30.5],
        zoom: 10
      })
      let data = {
        type: 'FeatureCollection',
        features: [
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [114.281363, 30.607571]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [114.128573, 30.552409]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [114.371722, 30.463235]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [114.130216, 30.324358]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [114.194289, 30.466067]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [114.038213, 30.467483]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.062893, 30.210399]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [114.96016, 30.12398]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.035127, 30.114373]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.135084, 30.075937]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.585099, 29.705042]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.496249, 29.594042]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.632301, 29.589214]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.743364, 29.733978]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.626748, 29.81351]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.490696, 29.741211]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.506245, 29.690499]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.674118, 29.736458]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.56646, 29.780812]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.701488, 29.809315]
            }
          },
          {
            type: 'Feature',
            properties: {},
            geometry: {
              type: 'Point',
              coordinates: [115.901488, 29.809315]
            }
          }
        ]
      }

      // 根据size属性设置不同的颜色
      async function renderLayer() {
        await map.once('style.load')
        map.addSource('source', {
          type: 'geojson',
          data: data,
          // 开启点聚合
          cluster: true
        })
        map.addLayer({
          id: 'layer',
          source: 'source',
          type: 'circle',
          paint: {
            'circle-radius': {
              property: 'point_count',
              stops: [
                [5, 10],
                [10, 20],
                [15, 30],
                [20, 40]
              ]
            },
            'circle-color': {
              property: 'point_count',
              stops: [
                [5, '#652e80'],
                [10, 'green'],
                [15, 'yellow'],
                [20, 'red']
              ]
            }
          },
          filter: ['has', 'point_count']
        })
        map.addLayer({
          id: 'un_cluster_layer',
          type: 'circle',
          source: 'source',
          paint: {
            'circle-radius': 4,
            'circle-color': '#4164fb',
            'circle-stroke-width': 1,
            'circle-stroke-color': '#fff'
          },
          filter: ['!', ['has', 'point_count']]
        })
      }
      renderLayer()
    </script>
  </body>
</html>

5、热力图

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
    <title>Document</title>
    <!-- 导入依赖 -->
    <!-- openlayer  mapbox cesium js+css -->

    <script src="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js"></script>
    <link
      href="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css"
      rel="stylesheet"
    />
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      #map {
        width: 100vw;
        height: 100vh;
      }
    </style>
  </head>

  <body>
    <div id="map"></div>
    <script>
      //token
      mapboxgl.accessToken = 你的token'
      const gaode = {
        version: 8,
        sources: {
          'raster-tiles': {
            type: 'raster',
            tiles: ['http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}'],
            tileSize: 256
          }
        },
        layers: [
          {
            id: 'simple-tiles',
            type: 'raster',
            source: 'raster-tiles',
            minzoom: 0,
            maxzoom: 22
          }
        ]
      }
      const map = new mapboxgl.Map({
        container: 'map', // container ID
        style: gaode, // style URL
        center: [114.3, 30.5], // starting position [lng, lat]
        zoom: 1 // starting zoom
      })
      let data = 'https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson'
      map.on('style.load', () => {
        /* 使用热力图图层加载点数据 --数据非常多 */
        map.addSource('source', {
          type: 'geojson',
          data
        })
        map.addLayer({
          id: 'heat_layer',
          source: 'source',
          type: 'heatmap',
          /* zoom大于9的情况热力图就不会显示了 */
          maxzoom: 9,
          paint: {
            'heatmap-radius': [
              'interpolate',
              ['linear'],
              ['zoom'],
              2, //zoom
              5,
              6, //zoom
              10
            ]
          }
        })
        map.on('click', 'heat_layer', (evt) => {
          console.log(evt.features[0])
        })
      })
      /* line-progress */
    </script>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值