Mapbox加载带有箭头方向的线

1、Maobox加载带有箭头放心的线,需要分别加载线数据和箭头icon数据;

2、mapbox先加载线数据:

// 加载轨迹线数据  
function addRoutelayer() {
    map.addLayer({
        'id': 'routeLayer',
        'type': 'line',
        'source': {
            'type': 'geojson',
            'lineMetrics': true,
            'data': {
                type: "FeatureCollection",
                features: [
                    {
                        'type': 'Feature',
                        'geometry': {
                            'type': 'LineString',
                            'coordinates': [
                                [116.391844, 39.898457],
                                [116.377947, 39.898595],
                                [116.366892, 39.947263],
                                [116.387537, 39.947568],
                                [116.401988, 39.947764],
                                [116.410824, 39.947929],
                                [116.42674, 39.947558],
                                [116.427338, 39.9397],
                                [116.405321, 39.906622],
                                [116.394954, 39.906324],
                                [116.391264, 39.906308],
                            ]
                        }
                    }],
            }
        },
        'paint': {
            'line-width': 5,
            'line-opacity': 1,
            'line-color': '#009EFF',
        }
    });
}

3、mapbox加载箭头数据:

// 加载箭头数据
function addArrowlayer() {
    map.addLayer({
        'id': 'arrowLayer',
        'type': 'symbol',
        'source': {
            'type': 'geojson',
            'lineMetrics': true,
            'data': {
                type: "FeatureCollection",
                features: [
                    {
                        'type': 'Feature',
                        'geometry': {
                            'type': 'LineString',
                            'coordinates': [
                                [116.391844, 39.898457],
                                [116.377947, 39.898595],
                                [116.366892, 39.947263],
                                [116.387537, 39.947568],
                                [116.401988, 39.947764],
                                [116.410824, 39.947929],
                                [116.42674, 39.947558],
                                [116.427338, 39.9397],
                                [116.405321, 39.906622],
                                [116.394954, 39.906324],
                                [116.391264, 39.906308],
                            ]
                        }
                    }],
            }
        },
        'layout': {
            'symbol-placement': 'line',
            'symbol-spacing': 50,  // 图标间隔,默认为250
            'icon-image': 'arrowIcon', //箭头图标
            'icon-size': 0.5,
            'icon-rotate': 90
        }
    });
}

4、其中箭头icon图标:

要在mapbox中实现动态线方向,可以利用mapbox-gl图层Layer中的icon-rotate属性来旋转箭头图标的角度。首先需要注意的是,默认情况下,目标与正北方向之间有90°的夹角,所以需要将目标的方向角度(bearing)减去90°。具体的代码如下: let iconRotate = Number(bearing - 90); 然后,在配置layout中添加symbol-placement和symbol-spacing属性,并设置symbol-placement为'line',symbol-spacing为图标之间的间隔(默认为250)。同时,还需要设置icon-image为箭头图标的名称,icon-size为图标的大小。以下是添加箭头图层的核心代码: function addArrowlayer() { map.addLayer({ 'id': 'arrowLayer', 'type': 'symbol', 'source': { 'type': 'geojson', 'data': routeGeoJson //轨迹geojson格式数据 }, 'layout': { 'symbol-placement': 'line', 'symbol-spacing': 50, // 图标间隔,默认为250 'icon-image': 'arrowIcon', //箭头图标 'icon-size': 0.5 } }); } 通过以上代码,可以在mapbox中实现动态线方向箭头图标会根据目标的方向角度进行旋转。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [mapbox-gl 实现末尾带箭头线](https://blog.csdn.net/qq_24343389/article/details/130868812)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [mapboxgl实现带箭头轨迹线](https://blog.csdn.net/gisarmory/article/details/112034622)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ponGISer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值