MapBox地图

使用Mapbox 地图

1、注册MapBox token

注册地址 Mapbox Access Tokens

2、引入mapbox.gl JS 和 css

使用mapbox 需要单独引入 mapbox

  1. <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.5.0/mapbox-gl.css' rel='stylesheet' />
  2. <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.5.0/mapbox-gl.js'></script>

引入 L7

  1. <script src="https://gw.alipayobjects.com/os/antv/pkg/_antv.l7-2.0.0/dist/l7.js"></script>

添加div 标签指定地图容器

同时需要为Div设置 高度和宽度

  1. <div id="map"></div>

初始化 L7 Scene

  1. const scene = new L7.Scene({
  2. id: 'map',
  3. map: new L7.Mapbox({
  4. style: 'dark', // 样式URL
  5. center: [120.19382669582967, 30.258134],
  6. pitch: 0,
  7. zoom: 12,
  8. token: 'mapbox token',
  9. }),
  10. });

这样我们就完成了通过L7 实例化mapbox地图

添加可视化图层

  1. fetch('https://gw.alipayobjects.com/os/rmsportal/oVTMqfzuuRFKiDwhPSFL.json')
  2. .then(res => res.json())
  3. .then(data => {
  4. const pointLayer = new L7.PointLayer({})
  5. .source(data.list, {
  6. parser: {
  7. type: 'json',
  8. x: 'j',
  9. y: 'w',
  10. },
  11. })
  12. .shape('cylinder')
  13. .size('t', function(level) {
  14. return [1, 2, level * 2 + 20];
  15. })
  16. .color('t', [
  17. '#094D4A',
  18. '#146968',
  19. '#1D7F7E',
  20. '#289899',
  21. '#34B6B7',
  22. '#4AC5AF',
  23. '#5FD3A6',
  24. '#7BE39E',
  25. '#A1EDB8',
  26. '#CEF8D6',
  27. ])
  28. .style({
  29. opacity: 1.0,
  30. });
  31. scene.addLayer(pointLayer);
  32. });

完整demo 代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>创建地图场景</title>
  6. <style> ::-webkit-scrollbar{display:none;}html,body{overflow:hidden;margin:0;}
  7. #map { position:absolute; top:0; bottom:0; width:100%; }
  8. </style>
  9. </head>
  10. <body>
  11. <div id="map"></div>
  12. <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.5.0/mapbox-gl.css' rel='stylesheet' />
  13. <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.5.0/mapbox-gl.js'></script>
  14. <script src="https://gw.alipayobjects.com/os/antv/pkg/_antv.l7-2.0.0/dist/l7.js"></script>
  15. <script>
  16. const scene = new L7.Scene({
  17. id: 'map',
  18. map: new L7.Mapbox({
  19. style: 'dark', // 样式URL
  20. center: [120.19382669582967, 30.258134],
  21. pitch: 0,
  22. zoom: 12,
  23. token: 'mapbox token',
  24. }),
  25. });
  26. fetch('https://gw.alipayobjects.com/os/rmsportal/oVTMqfzuuRFKiDwhPSFL.json')
  27. .then(res => res.json())
  28. .then(data => {
  29. const pointLayer = new L7.PointLayer({})
  30. .source(data.list, {
  31. parser: {
  32. type: 'json',
  33. x: 'j',
  34. y: 'w'
  35. }
  36. })
  37. .shape('cylinder')
  38. .size('t', function(level) {
  39. return [ 1, 2, level * 2 + 20 ];
  40. })
  41. .color('t', [
  42. '#094D4A',
  43. '#146968',
  44. '#1D7F7E',
  45. '#289899',
  46. '#34B6B7',
  47. '#4AC5AF',
  48. '#5FD3A6',
  49. '#7BE39E',
  50. '#A1EDB8',
  51. '#CEF8D6'
  52. ])
  53. .style({
  54. opacity: 1.0
  55. });
  56. scene.addLayer(pointLayer);
  57. });
  58. </script>
  59. </body>
  60. </html>