且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-01 22:30:45

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

    }