mapbox 如何向线串添加标签?

mapbox how can I add a label to a linestring?

我想向线串中添加文本。街道名称在 google 地图中的显示方式基本相同。因此,如果我放大或四处移动地图,文本仍会显示在线上。

我是否需要添加某种具有相同坐标的新图层?

这里是jsfiddle开头。

<body>

<div id='map'></div>

</body>

mapboxgl.accessToken = 'pk.eyJ1Ijoib2tpZWJ1YmJhIiwiYSI6ImNpdHZscGs3ajAwNXYyb284bW4ydWUzbGsifQ.1PoNrSP0F65WolWgqKhV4g';

var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v9',
    center: [-88.4, 33.4],
    zoom: 10
});

map.on('load', function () {
    map.addSource("route", {
        "type": "geojson",
        "data": {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [-88.451092, 33.325422],
                    [-88.248037, 33.436312]
                ]
            }
        }
    });

    map.addLayer({
        "id": "route",
        "type": "line",
        "source": "route",
        "layout": {
            "line-join": "round",
            "line-cap": "round"
        },
        "paint": {
            "line-color": "#888",
            "line-width": 8
        }
    });



});


        body { margin:0; padding:0; }
        #map { position:absolute; top:0; bottom:0; width:100%; }

我能够让它正常工作http://jsfiddle.net/brianssheldon/wm18a33d/8/

我添加了这段代码

map.addLayer({
    "id": "symbols",
    "type": "symbol",
    "source": "route",
    "layout": {
        "symbol-placement": "line",
        "text-font": ["Open Sans Regular"],
        "text-field": 'this is a test',
        "text-size": 32
    },
    "paint": {}
});

更好的解决方案 - http://jsfiddle.net/brianssheldon/wm18a33d/27/

使用 properties.title 向 geojson 添加属性,并在符号的添加层中将 "text-field" 更改为“{title}”

    "properties": {
      "title": 'aaaaaa' 
    }

在符号的 addLayer 中,使用这个

 "text-field": '{title}',