如何在 Mapbox SDK 的 LineString 上设置文本?

How to set text on LineString in Mapbox SDK?

我正在使用 Mapbox SDK 中的 LineLayer,但在让文本显示在线上时遇到了问题。有来自 Mapbox 的 example

这是我目前拥有的:

// Create a list to store our line coordinates.
routeCoordinates = new ArrayList<>();
routeCoordinates.add(Point.fromLngLat(-95.9928, 36.1540));
routeCoordinates.add(Point.fromLngLat(-95.9870, 36.1397));

// Create the LineString from the list of coordinates and then make a GeoJSON
// FeatureCollection so we can add the line to our map as a layer.
LineString lineString = LineString.fromLngLats(routeCoordinates);
FeatureCollection featureCollection = FeatureCollection.fromFeatures(new com.mapbox.geojson.Feature[] {com.mapbox.geojson.Feature.fromGeometry(lineString)});
Source geoJsonSource = new GeoJsonSource("line-source", featureCollection);
mapbox.addSource(geoJsonSource);

LineLayer lineLayer = new LineLayer("linelayer", "line-source");
// The layer properties for our line. This is where we make the line dotted, set the
// color, etc.
lineLayer.setProperties(
     PropertyFactory.lineCap(Property.LINE_CAP_ROUND),
     PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
     PropertyFactory.lineWidth(5f),
     PropertyFactory.lineColor(Color.parseColor("#e55e5e")),
     PropertyFactory.textField("some text"),
     PropertyFactory.textColor(Color.parseColor("#ffffff"))
);
mapbox.addLayer(lineLayer);

它不会将字符串添加到如下所示的行中。

样式规范文档中的 LineLayer class is based on the line layer in Mapbox GL JS. You can see available properties of this type of layer here。如您所见,textField 和 textColor 不是可用属性。

实现此目的的一种方法是创建一个附加层 - 这次类型为 SymbolLayerhttps://docs.mapbox.com/android/api/map-sdk/7.0.0/com/mapbox/mapboxsdk/style/layers/SymbolLayer.html). This layer would have the same line data from your GeoJsonSource. You can read more about symbol layers here, but you can set text for this layer, and then set the symbol-placement 属性 至 lineline-center 以便文本正确对齐。