mapbox 绘制虚线

<!DOCTYPE html>
<html>

<head>
    <meta charset='utf-8' />
    <title>添加 GeoJSON 线</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.1/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.1/mapbox-gl.css' rel='stylesheet' />
    <style>
        body {
            margin: 0;
            padding: 0;
        }

        #map {
            position: absolute;
            top: 0;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>

<body>

    <div id='map'></div>
    <script>
        mapboxgl.accessToken =
            'pk.eyJ1IjoibHh0aWFudGlhbiIsImEiOiJjaXd2ZjlkYnQwMTZvMnRtYWZnM2lpbHFvIn0.ef3rFZTr9psmLWahrqap2A';
        var map = new mapboxgl.Map({
            container: 'map',
            style: 'mapbox://styles/mapbox/streets-v11',
            center: [-122.486052, 37.830348],
            zoom: 15
        });

        let coordinates = [],
            layerActive = false;

        map.on('click', (e) => addLayer(e))

        map.on('mousemove', (e) => updateLayer(e))

        function addLayer(e) {
            if (layerActive) {
                removeLayer();
                layerActive = !layerActive;
                return;
            }

            const { lat, lng } = e.lngLat;
            coordinates[0] = [lng, lat];
            map.addSource('sourceId', {
                type: 'geojson',
                data: {
                    "type": "Feature",
                    "properties": {},
                    "geometry": {
                        "type": "LineString",
                        "coordinates": coordinates
                    }
                }
            });
            map.addLayer({
                "id": "route",
                "type": "line",
                "source": "sourceId",
                "layout": {
                    "line-join": "round",
                    "line-cap": "round"
                },
                "paint": {
                    "line-color": "#888",
                    "line-width": 8,
                    "line-dasharray": [2, 4]
                }
            });
            layerActive = !layerActive;
        }

        function updateLayer(e) {
            const {
                lat,
                lng
            } = e.lngLat;
            coordinates[1] = [lng, lat];
            const datas = {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "LineString",
                    "coordinates": coordinates
                }
            }
            if (map.getSource('sourceId')) {
                map.getSource('sourceId').setData(datas);
            }
        }

        function removeLayer() {
            if (map.getLayer('route')) {
                map.removeLayer('route');
            }
            if (map.getSource('sourceId')) {
                map.removeSource('sourceId');
            }
        }
    </script>

</body>

</html>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值