mapbox 基于turf 绘制扇形

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>绘制扇形</title>
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
    <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>
    <script src="https://cdn.bootcdn.net/ajax/libs/Turf.js/6.5.0/turf.min.js"></script>
    <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';

        const map = new mapboxgl.Map({
            container: 'map', // container ID
            style: 'mapbox://styles/mapbox/light-v10', // style URL
            center: [-75, 40], // starting position
            zoom: 10 // starting zoom
        });

        map.on('load', () => {
            const arr = {
                "北": [330,30],
                "东北": [15, 75],
                "西北": [285,345],
                "南": [150,210],
                "东南": [105,165],
                "西南": [195,255],
                "东": [60,120],
                "西": [240,300]
            };

            Object.keys(arr).forEach((item, index) => {
                initLayer(index,arr[item]);
            });
        });

        function initLayer(i, item){
            var center = turf.point([-75, 40]);
            var radius = 10;
            var bearing1 = item[0];
            var bearing2 = item[1];

            var data = turf.sector(center, radius, bearing1, bearing2);

            map.addSource(`maine${i}`, {
                'type': 'geojson',
                'data':data
            });

            // Add a new layer to visualize the polygon.
            map.addLayer({
                'id': `maine${i}`,
                'type': 'fill',
                'source': `maine${i}`, // reference the data source
                'layout': {},
                'paint': {
                    'fill-color': '#0080ff', // blue color fill
                    'fill-opacity': 0.5
                }
            });
            // Add a black outline around the polygon.
            map.addLayer({
                'id': `outline${i}`,
                'type': 'line',
                'source': `maine${i}`,
                'layout': {},
                'paint': {
                    'line-color': '#000',
                    'line-width': 3
                }
            });
        }
    </script>

</body>

</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值