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

时间:2019-01-15 17:00:49

标签: java android mapbox mapbox-android

我正在使用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);

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

enter image description here

1 个答案:

答案 0 :(得分:2)

LineLayer class基于Mapbox GL JS中的线层。您可以在样式说明文档中查看此类here层的可用属性。如您所见,textField和textColor不是可用的属性。

一种实现此目的的方法是创建一个附加层-这次是 SymbolLayer https://docs.mapbox.com/android/api/map-sdk/7.0.0/com/mapbox/mapboxsdk/style/layers/SymbolLayer.html)类型。该层将具有来自GeoJsonSource的相同线数据。您可以阅读有关符号图层here的更多信息,但是可以为该图层设置文本,然后将symbol-placement属性设置为 line line-center strong>,以便文本正确对齐。