webgis开发之L7(1)

AntV L7

L7·蚂蚁地理空间数据可视化

蚂蚁集团 AntV 数据可视化团队推出的基于 WebGL 的开源大规模地理空间数据可视分析引擎。

一、地图初始化

01、地图初始化

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>mapbox</title>
    <!-- 1、引入mapbox库以及它的样式 -->
    <script src="../../lib/mapbox.js"></script>
    <link rel="stylesheet" href="../../lib/mapbox.css">
    <script src="../../lib/l7.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #map{
            width: 100vw;
            height: 100vh;
        }
    </style>
</head>
<body>
    <!-- 1、给地图准备一个容器 -->
    <div id="map"></div>
    <script>
        mapboxgl.accessToken = 'pk.eyJ1IjoiY2hlbmdjaGFvY2hhbyIsImEiOiJjbGU1aDZ2eWUwMXp4M29udmFnNnNyZjBhIn0.2Kd0ZX06ReEdBnZ9XU4XUA';
        // 3、创建地图
        const map = new mapboxgl.Map({
            container:'map',    //指定地图容器id
            style:'mapbox://styles/mapbox/streets-v12', //地图风格
            center:[114.30,30.50],  //地图中心坐标  
            zoom:12,    //缩放比例
        });
        
        const {Scene,Mapbox} = L7
        //L7来管理地图
        const scene = new L7.Scene({
            id:'map',
            map:new L7.Mapbox({
                mapInstance:map
            })
        })
        scene.on('click',(e)=>{
            console.log(e);
        })
    </script>
</body>
</html>

二、使用图层绘制要素

01、加载点图层

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pointLayer</title>
    <!-- 1、引入mapbox库以及它的样式 -->
    <script src="../../lib/mapbox.js"></script>
    <link rel="stylesheet" href="../../lib/mapbox.css">
    <script src="../../lib/l7.js"></script>
    <script src="../../lib/turf.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #map {
            width: 100vw;
            height: 100vh;
        }

        button {
            position: fixed;
            top: 5px;
            left: 5px;
            z-index: 1;
        }
    </style>
</head>

<body>
    <button onclick="toLog()" style="left: 80px;">查看</button>
    <button onclick="toDel()" style="left: 160px;">删除</button>
    <button onclick="toShow()">切换</button>
    <!-- 1、给地图准备一个容器 -->
    <div id="map"></div>
    <script>
        mapboxgl.accessToken = 'pk.eyJ1IjoiY2hlbmdjaGFvY2hhbyIsImEiOiJjbGU1aDZ2eWUwMXp4M29udmFnNnNyZjBhIn0.2Kd0ZX06ReEdBnZ9XU4XUA';
        // 3、创建地图
        const map = new mapboxgl.Map({
            container: 'map',    //指定地图容器id
            style: 'mapbox://styles/mapbox/streets-v12', //地图风格
            center: [114.30, 30.50],  //地图中心坐标  
            zoom: 12,    //缩放比例
        });
        // 官方文档:https://l7.antv.antgroup.com/api/point_layer/pointlayer
        const { Scene, Mapbox, PointLayer,LayerSwitch } = L7
        //L7来管理地图
        const scene = new L7.Scene({
            id: 'map',
            map: new L7.Mapbox({
                mapInstance: map
            })
        })

        const data = turf.featureCollection([
            turf.point([114.34, 30.52], { num: 200 }),
            turf.point([114.35, 30.49], { num: 300 }),
            turf.point([114.355, 30.51], { num: 400 })
        ]);

        let pointLayer
        scene.on('load', () => {
            pointLayer = new PointLayer()
                .source(data)   //数据源 跟mapbox不一样,它只能放要素集合
                .shape('cylinder')
                .size(50)
                .color('red')
                // .active('blue')
                .filter('num', (num) => {
                    return num > 250
                })
            scene.addLayer(pointLayer)

            const layerSwitch = new LayerSwitch({
                layers: [pointLayer],
            });
            scene.addControl(layerSwitch);
        })

        function toLog() {
            console.log(pointLayer);
        }
        function toDel() {
            scene.removeLayer(pointLayer)   //删除图层
        }
        let isShow = true
        // 控制图层显隐
        function toShow() {
            if (isShow) {
                pointLayer.hide()
                isShow = !isShow
            } else {
                pointLayer.show()
                isShow = !isShow
            }
        }
    </script>
</body>

</html>

02、加载面图层

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>mapbox</title>
    <!-- 1、引入mapbox库以及它的样式 -->
    <script src="../../lib/mapbox.js"></script>
    <link rel="stylesheet" href="../../lib/mapbox.css">
    <script src="../../lib/l7.js"></script>
    <script src="../../lib/turf.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

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

<body>
    <!-- 1、给地图准备一个容器 -->
    <div id="map"></div>
    <script>
        mapboxgl.accessToken = 'pk.eyJ1IjoiY2hlbmdjaGFvY2hhbyIsImEiOiJjbGU1aDZ2eWUwMXp4M29udmFnNnNyZjBhIn0.2Kd0ZX06ReEdBnZ9XU4XUA';
        // 3、创建地图
        const map = new mapboxgl.Map({
            container: 'map',    //指定地图容器id
            style: 'mapbox://styles/mapbox/streets-v12', //地图风格
            center: [114.30, 30.50],  //地图中心坐标  
            zoom: 4,    //缩放比例
            projection: 'mercator',
        });
        // 官方文档:https://l7.antv.antgroup.com/api/point_layer/pointlayer
        const { Scene, Mapbox, PointLayer,PolygonLayer } = L7
        //L7来管理地图
        const scene = new L7.Scene({
            id: 'map',
            map: new L7.Mapbox({
                mapInstance: map
            })
        })

        const data = turf.featureCollection([
            turf.point([114.34, 30.52], { num: 200 }),
            turf.point([114.35, 30.49], { num: 300 }),
            turf.point([114.355, 30.51], { num: 400 })
        ]);

        
        const colors = ['#303841', '#ff2e63', '#30e3ca', '#fcbad3', '#aa96da', '#cca8e9', '#ff9a00', '#625772', '#b61aae', '#a7ff83', '#ba52ed']
        scene.on('load', () => {
            fetch('中国.json').then(res=>res.json()).then(res=>{
                let layer = new PolygonLayer()
                    .source(res)
                    .color('name',colors)   //用name字段 映射 得到一个颜色
                    // ['a','b','c'] -----> [1,2,3,4]
                    .active(true)
                    .style({
                        opacity:0.5
                    })
                scene.addLayer(layer)
            })
        })

    </script>
</body>

</html>

03、加载logo

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>zhongdi Layer</title>
    <!-- 1、引入mapbox库以及它的样式 -->
    <script src="../../lib/mapbox.js"></script>
    <link rel="stylesheet" href="../../lib/mapbox.css">
    <script src="../../lib/l7.js"></script>
    <script src="../../lib/turf.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #map {
            width: 100vw;
            height: 100vh;
        }

        button {
            position: fixed;
            top: 5px;
            left: 5px;
            z-index: 1;
        }
    </style>
</head>

<body>
    <button onclick="toLog()" style="left: 80px;">查看</button>
    <button onclick="toDel()" style="left: 160px;">删除</button>
    <button onclick="toShow()">切换</button>
    <!-- 1、给地图准备一个容器 -->
    <div id="map"></div>
    <script>
        mapboxgl.accessToken = 'pk.eyJ1IjoiY2hlbmdjaGFvY2hhbyIsImEiOiJjbGU1aDZ2eWUwMXp4M29udmFnNnNyZjBhIn0.2Kd0ZX06ReEdBnZ9XU4XUA';
        // 3、创建地图
        const map = new mapboxgl.Map({
            container: 'map',    //指定地图容器id
            style: 'mapbox://styles/mapbox/streets-v12', //地图风格
            center: [114.30, 30.50],  //地图中心坐标  
            zoom: 12,    //缩放比例
        });
        // 官方文档:https://l7.antv.antgroup.com/api/point_layer/pointlayer
        const { Scene, Mapbox, PointLayer,PolygonLayer } = L7
        //L7来管理地图
        const scene = new L7.Scene({
            id: 'map',
            map: new L7.Mapbox({
                mapInstance: map
            })
        })

        const data = turf.featureCollection([
            turf.point([114.34, 30.52], { num: 200 }),
            turf.point([114.35, 30.49], { num: 300 }),
            turf.point([114.355, 30.51], { num: 400 })
        ]);

        let pointLayer
        scene.on("loaded", () => {
            fetch("http://39.103.151.139:8000/gis/zhongdi").then(res => res.json())
                .then(data => {
                    // console.log(data);
                    // map.center([data.properties.center[0],data.properties.center[1]])
                    const zhongdiLayer = new PolygonLayer();
                    zhongdiLayer.source(data)
                        .color("#652e80")
                        .shape("extrude")   //把平面拉伸成柱体
                        // .size(200)
                        /* 可以直接获取properties中的参数 */
                        .size("people", p => {
                            console.log(p)
                            return p / 5;
                        })
                    scene.addLayer(zhongdiLayer)
                })
        })

        function toLog() {
            console.log(pointLayer);
        }
        function toDel() {
            scene.removeLayer(pointLayer)   //删除图层
        }
        let isShow = true
        // 控制图层显隐
        function toShow() {
            if (isShow) {
                pointLayer.hide()
                isShow = !isShow
            } else {
                pointLayer.show()
                isShow = !isShow
            }
        }
    </script>
</body>

</html>

三、组件

01、标注与弹框

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>zhongdi Layer</title>
    <!-- 1、引入mapbox库以及它的样式 -->
    <script src="../../lib/mapbox.js"></script>
    <link rel="stylesheet" href="../../lib/mapbox.css">
    <script src="../../lib/l7.js"></script>
    <script src="../../lib/turf.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #map {
            width: 100vw;
            height: 100vh;
        }

        button {
            position: fixed;
            top: 5px;
            left: 5px;
            z-index: 1;
        }
    </style>
</head>

<body>
    <!-- 1、给地图准备一个容器 -->
    <div id="map"></div>
    <script>
        mapboxgl.accessToken = 'pk.eyJ1IjoiY2hlbmdjaGFvY2hhbyIsImEiOiJjbGU1aDZ2eWUwMXp4M29udmFnNnNyZjBhIn0.2Kd0ZX06ReEdBnZ9XU4XUA';
        // 3、创建地图
        const map = new mapboxgl.Map({
            container: 'map',    //指定地图容器id
            style: 'mapbox://styles/mapbox/streets-v12', //地图风格
            center: [114.30, 30.50],  //地图中心坐标  
            zoom: 12,    //缩放比例
        });
        // 官方文档:https://l7.antv.antgroup.com/api/point_layer/pointlayer
        const { Scene, Mapbox, Marker, Popup } = L7
        //L7来管理地图
        const scene = new L7.Scene({
            id: 'map',
            map: new L7.Mapbox({
                mapInstance: map
            })
        })

        scene.on('loaded', () => {
            const marker = new Marker()
            marker.setLnglat([114.30, 30.5])
            scene.addMarker(marker)

            const popup = new Popup({
                offsets: [0, 45],
                // 初始锚点经纬度
                lngLat: {
                    lng: 114.30,
                    lat: 30.5,
                },
                // Popup 标题
                title: 'Popup Title',
                // Popup 内容
                html: '武汉',
            })
            scene.addPopup(popup)

        });
    </script>
</body>

</html>

02、地图控件

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>zhongdi Layer</title>
    <!-- 1、引入mapbox库以及它的样式 -->
    <script src="../../lib/mapbox.js"></script>
    <link rel="stylesheet" href="../../lib/mapbox.css">
    <script src="../../lib/l7.js"></script>
    <script src="../../lib/turf.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #map {
            width: 100vw;
            height: 100vh;
        }

        button {
            position: fixed;
            top: 5px;
            left: 5px;
            z-index: 1;
        }
    </style>
</head>

<body>
    <!-- 1、给地图准备一个容器 -->
    <div id="map"></div>
    <script>
        mapboxgl.accessToken = 'pk.eyJ1IjoiY2hlbmdjaGFvY2hhbyIsImEiOiJjbGU1aDZ2eWUwMXp4M29udmFnNnNyZjBhIn0.2Kd0ZX06ReEdBnZ9XU4XUA';
        // 3、创建地图
        const map = new mapboxgl.Map({
            container: 'map',    //指定地图容器id
            style: 'mapbox://styles/mapbox/streets-v12', //地图风格
            center: [114.30, 30.50],  //地图中心坐标  
            zoom: 12,    //缩放比例
        });
        // 官方文档:https://l7.antv.antgroup.com/api/point_layer/pointlayer
        const { Scene, Mapbox, Marker, Popup,Zoom,Scale,MapTheme } = L7
        //L7来管理地图
        const scene = new L7.Scene({
            id: 'map',
            map: new L7.Mapbox({
                mapInstance: map
            })
        })

        scene.on('loaded', () => {
            const zoom = new Zoom({
                zoomInTitle: '放大',
                zoomOutTitle: '缩小',
            });
            scene.addControl(zoom);

            const scale = new Scale({
                zoomInTitle: '放大',
                zoomOutTitle: '缩小',
            });
            scene.addControl(scale);

            const mapTheme = new MapTheme({           
            })
            scene.addControl(mapTheme)
        });
    </script>
</body>

</html>

03、画笔

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>zhongdi Layer</title>
    <!-- 1、引入mapbox库以及它的样式 -->
    <script src="../../lib/mapbox.js"></script>
    <link rel="stylesheet" href="../../lib/mapbox.css">
    <script src="../../lib/l7.js"></script>
    <script src="../../lib/l7-draw.js"></script>
    <script src="../../lib/turf.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #map {
            width: 100vw;
            height: 100vh;
            overflow: hidden;
        }

        button {
            position: fixed;
            top: 5px;
            left: 5px;
            z-index: 1;
        }
    </style>
</head>

<body>
    <!-- 1、给地图准备一个容器 -->
    <div id="map"></div>
    <script>
        mapboxgl.accessToken = 'pk.eyJ1IjoiY2hlbmdjaGFvY2hhbyIsImEiOiJjbGU1aDZ2eWUwMXp4M29udmFnNnNyZjBhIn0.2Kd0ZX06ReEdBnZ9XU4XUA';
        // 3、创建地图
        const map = new mapboxgl.Map({
            container: 'map',    //指定地图容器id
            style: 'mapbox://styles/mapbox/streets-v12', //地图风格
            center: [114.30, 30.50],  //地图中心坐标  
            zoom: 12,    //缩放比例
        });
        // 官方文档:https://l7.antv.antgroup.com/api/point_layer/pointlayer
        const { Scene, Mapbox, Marker, Popup, Zoom, Scale } = L7
        //L7来管理地图
        const scene = new L7.Scene({
            id: 'map',
            map: new L7.Mapbox({
                mapInstance: map
            })
        })

        const { DrawPoint, DrawEvent,DrawLine,DrawPolygon,DrawRect,DrawCircle } = L7.Draw

        let draw = new DrawPoint(scene, {})
        draw.enable()   //激活画笔
        draw.on(DrawEvent.Change, (arr) => {
            if (arr.length == 3) {
                draw.disable(); //取消画笔
                // draw.clear()    //清除画笔要素
                // draw.destroy()  //销毁画笔,画布的内容也没有了
                // 转换为 GeoJSON 对象
                const geojson = turf.featureCollection(arr);
                console.log(arr);


                // 将 GeoJSON 转换为 JSON 字符串
                // const jsonStr = JSON.stringify(geojson, null, 2);

                // // 创建Blob对象
                // const blob = new Blob([jsonStr], { type: 'application/json' });

                // // 创建URL对象
                // const url = URL.createObjectURL(blob);

                // // 创建一个<a>元素并设置下载属性
                // const a = document.createElement('a');
                // a.href = url;
                // a.download = 'output.json';

                // // 将<a>元素添加到DOM中
                // document.body.appendChild(a);

                // // 模拟点击下载
                // a.click();

                // // 清理
                // document.body.removeChild(a);
                // URL.revokeObjectURL(url);
            }
            // console.log(arr);
        })

        // 画线
        // let draw = new DrawLine(scene,{
        //     distanceOptions:{}  // 展示距离
        // })
        // draw.enable()

        // 画面
        // let draw = new DrawPolygon(scene,{
        //     distanceOptions:{},
        //     areaOptions:{}  // 展示面积
        // })
        // draw.enable()

        // 画矩形
        // let draw = new DrawRect(scene,{
        //     distanceOptions:{},
        //     areaOptions:{}  // 展示面积
        // })
        // draw.enable()

        // 画圆
        // let draw = new DrawCircle(scene,{
        //     distanceOptions:{},
        //     areaOptions:{}  // 展示面积
        // })
        // draw.enable()
        // draw.on(DrawEvent.Change,(arr)=>{
        //     const geojson = turf.featureCollection(arr)
        //     console.log(arr);
        // })

    </script>
</body>

</html>

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值