如何使用Mapbox Android SDK更改多边形笔触的大小

时间:2016-10-27 04:20:02

标签: android mapbox mapbox-gl-js

我正在尝试使用Mapbox Android SDK版本4.2.0.beta4增加围绕多边形外部的1px笔划的宽度。

首先我尝试使用PolygonOptions,但没有可用的选项。

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);
}

然后我尝试使用样式规范中定义的新运行时样式API,但找不到合适的图层ID,我试过"背景" https://www.mapbox.com/mapbox-gl-style-spec/

public void changeStrokeWidth(MapboxMap mapboxMap) {
        Layer styleLayer = mapboxMap.getLayer("background");

        styleLayer.setProperties(PropertyFactory.lineWidth(10f));
}

有没有人知道如何使这项工作或我只需要在多边形顶部创建我自己的折线组来模拟行为?

2 个答案:

答案 0 :(得分:0)

这里有几个选项。目前我们还没有提供增加多边形笔划宽度的方法,但您可以:

  1. 使用构成多边形的相同class A{ private int i,j; public void get(int i, int j) { this.i=i; this.j=j; } public void show() { System.out.println(i + " " + j); } } public class App { public static void main(String[] args) { A[] c = new A[11]; for(int i=0; i<10; i++) { c[i].get(i, i); } for(int j=0; j<10; j++) { c[j].show(); } } } 添加折线。这是迄今为止最简单的方法,但您必须考虑在地图上添加折线(偶尔覆盖标签)。

  2. 您的第二个选择是喜欢您提到的,使用与多边形相同的几何图形向地图添加线条图。这里的an example展示了如何获取积分并创建这样的图层。

  3. 如果您还有其他问题,请与我们联系。

答案 1 :(得分:0)

//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"));

下面我们将创建一个多边形边界坐标列表。

                    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);

    }