如何使用Mapbox Android SDK更改多边形笔触的大小 [英] How to change size of polygon stroke using Mapbox Android SDK
问题描述
我正在尝试使用Mapbox Android SDK版本4.2.0.beta4来增加多边形外部1px笔触的宽度.
I am trying to increase the width of the 1px stroke around the outside of a polygon using Mapbox Android SDK version 4.2.0.beta4.
首先,我尝试使用PolygonOptions,但没有可用的选项.
First I tried using the PolygonOptions but there was no option available.
public Polygon addPolygon(MapboxMap mapboxMap) {
PolygonOptions polygonOptions = new PolygonOptions()
.addAll(latLngs)
.alpha(ALPHA_UNSELECTED)
.fillColor(FILL_COLOR)
.strokeColor(BORDER_COLOR);
// no option for stroke width
return mapboxMap.addPolygon(polygonOptions);
}
然后,我尝试使用样式规范中定义的新的Runtime Style API,但找不到合适的图层ID,我尝试了背景" https://www.mapbox.com/mapbox-gl-style-spec/
I then tried using the using the new Runtime Style API as defined in the Style Spec but could not find a suitable layer ID, I tried "background" https://www.mapbox.com/mapbox-gl-style-spec/
public void changeStrokeWidth(MapboxMap mapboxMap) {
Layer styleLayer = mapboxMap.getLayer("background");
styleLayer.setProperties(PropertyFactory.lineWidth(10f));
}
有人知道如何进行这项工作,还是我只需要在多边形顶部创建自己的多段线来模拟行为?
Does anyone know how to make this work or do I just have to create my own set of polylines over the top of the polygon to emulate the behaviour?
推荐答案
//Hi, Guys. Below is the simple example to change the properties of polygon border. Hope it will help you.//
{
List<LatLng> plotPolygon = new ArrayList<>();
List<com.mapbox.services.commons.models.Position> coordinates = new ArrayList<>();
plotPolygon.add(new LatLng(18.9965099,75.7316343));
plotPolygon.add(new LatLng(20.8858018,74.7288414));
plotPolygon.add(new LatLng(21.1612315,79.0024702));
plotPolygon.add(new LatLng(18.7918749,78.899195));
plotPolygon.add(new LatLng(18.9965099,75.7316343));
Polygon polygon1 = mapboxMap.addPolygon(new PolygonOptions()
.addAll(plotPolygon)
);
polygon1.setFillColor(Color.parseColor("#3bb2d0"));
下面我们将创建多边形边界的坐标列表.
Below we are creating a list of coordinates of polygon border.
coordinates.add(com.mapbox.services.commons.models.Position.fromCoordinates(75.7316343 , 18.9965099));
coordinates.add(com.mapbox.services.commons.models.Position.fromCoordinates(74.7288414 , 20.8858018));
coordinates.add(com.mapbox.services.commons.models.Position.fromCoordinates(79.0024702 , 21.1612315));
coordinates.add(com.mapbox.services.commons.models.Position.fromCoordinates(78.899195 , 18.7918749));
coordinates.add(com.mapbox.services.commons.models.Position.fromCoordinates(75.7316343 , 18.9965099));
changeStrokeProperties(mapboxMap , coordinates);
}
public void changeStrokeProperties(MapboxMap mapboxMap , List<com.mapbox.services.commons.models.Position> coordinates) {
// 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.//
final Feature lineFeature = Feature.fromGeometry(LineString.fromCoordinates(coordinates));
final GeoJsonSource source = new GeoJsonSource(
"route", FeatureCollection.fromFeatures(new Feature[] { lineFeature }));
mapboxMap.addSource(source);
LineLayer lineLayer = new LineLayer("linelayer", "route");
lineLayer.setProperties(
PropertyFactory.lineDasharray(new Float[]{0.01f, 2f}),
PropertyFactory.lineCap(Property.LINE_CAP_ROUND),
PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
PropertyFactory.lineWidth(5f),
PropertyFactory.lineColor(Color.parseColor("#e55e5e"))
);
mapboxMap.addLayer(lineLayer);
}
这篇关于如何使用Mapbox Android SDK更改多边形笔触的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!