mapbox如何在线串中添加标签?

时间:2016-11-04 19:19:33

标签: mapbox mapbox-gl

我想在线串中添加文字。基本上与街道名称在谷歌地图中显示的方式相同。因此,如果我放大或移动地图,文本仍会显示在该行上。

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

这是一个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%; }

2 个答案:

答案 0 :(得分:4)

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

使用properties.title向geojson添加属性,并在addlayer中为符号更改&#34; text-field&#34;到&#34; {title}&#34;

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

并在符号的addLayer中使用此

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

答案 1 :(得分:0)

我能够让它工作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": {}
});