代码之家  ›  专栏  ›  技术社区  ›  Anton Pilyak

如何在MapBox GL JS中从GeoJson源绘制双线?

  •  1
  • Anton Pilyak  · 技术社区  · 6 年前

    我在Mapbox中使用数据驱动样式来定制我从GeoJson源中获得的行的样式。除了把线变成多边形或在附近画第二条线之外,还有什么方法可以画一条双线吗?

    1 回复  |  直到 6 年前
        1
  •  4
  •   Lucas Wojciechowski    6 年前

    您可以考虑使用 line-gap-width 所有物

    在线的实际路径外绘制线套管。值表示内部间隙的宽度。

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset='utf-8' />
        <title>Add a GeoJSON line</title>
        <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
        <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.js'></script>
        <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.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.eyJ1IjoibHVjYXN3b2oiLCJhIjoiY2l5Nmg4cWU1MDA0ejMzcDJtNHJmZzJkcyJ9.WhcEdTYQH6sSw2pm0RSP9Q';
    var map = new mapboxgl.Map({
        container: 'map',
        style: 'mapbox://styles/mapbox/streets-v9',
        center: [-122.486052, 37.830348],
        zoom: 15
    });
    
    map.on('load', function () {
    
        map.addLayer({
            "id": "route",
            "type": "line",
            "source": {
                "type": "geojson",
                "data": {
                    "type": "Feature",
                    "properties": {},
                    "geometry": {
                        "type": "LineString",
                        "coordinates": [
                            [-122.48369693756104, 37.83381888486939],
                            [-122.48348236083984, 37.83317489144141],
                            [-122.48339653015138, 37.83270036637107],
                            [-122.48356819152832, 37.832056363179625],
                            [-122.48404026031496, 37.83114119107971],
                            [-122.48404026031496, 37.83049717427869],
                            [-122.48348236083984, 37.829920943955045],
                            [-122.48356819152832, 37.82954808664175],
                            [-122.48507022857666, 37.82944639795659],
                            [-122.48610019683838, 37.82880236636284],
                            [-122.48695850372314, 37.82931081282506],
                            [-122.48700141906738, 37.83080223556934],
                            [-122.48751640319824, 37.83168351665737],
                            [-122.48803138732912, 37.832158048267786],
                            [-122.48888969421387, 37.83297152392784],
                            [-122.48987674713133, 37.83263257682617],
                            [-122.49043464660643, 37.832937629287755],
                            [-122.49125003814696, 37.832429207817725],
                            [-122.49163627624512, 37.832564787218985],
                            [-122.49223709106445, 37.83337825839438],
                            [-122.49378204345702, 37.83368330777276]
                        ]
                    }
                }
            },
            "paint": {
                "line-color": "#888",
                "line-width": 8,
                "line-gap-width": 10
            }
        });
    });
    </script>
    
    </body>
    </html>