簡體   English   中英

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

[英]How to set text on LineString in Mapbox SDK?

我正在使用Mapbox SDK中的LineLayer,但在獲取文本以顯示在行上時遇到問題。 Mapbox提供了一個示例

這是我到目前為止的內容:

// 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類基於Mapbox GL JS中的線層。 您可以在樣式說明文檔中的此處查看此類圖層的可用屬性。 如您所見,textField和textColor不是可用的屬性。

實現此目的的一種方法是創建一個附加層-這次是SymbolLayer類型( https://docs.mapbox.com/android/api/map-sdk/7.0.0/com/mapbox/mapboxsdk/style/layers /SymbolLayer.html )。 該層將具有來自GeoJsonSource的相同線數據。 您可以在此處閱讀有關符號圖層的更多信息,但可以為該圖層設置文本,然后將symbol-placement屬性設置為lineline-center,以便文本正確對齊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM