样式

使用 Style 对象可以自定义地图 要素 的颜色、大小、字体和其他可视化属性。

样式也可以在 图层 级别应用,从而为图层中的所有要素定义一致的外观。

PointFeature 样式

如需更改 PointFeature 的样式,可以定义一个 Style 对象,设置所需的外观属性(如 imagefillstroke 等),然后将该样式应用于 PointFeature

PointFeature styleFeature = new PointFeature(GeometryUtils.createPoint(22, 25))
        .withStyles(new Style()
                .withImage(new CircleStyle()
                        .withRadius(7)
                        .withFill(new Fill("#E7003E"))
                        .withStroke(new Stroke()
                                .withWidth(2.0)
                                .withColor("#710067"))));
vectorSource.addFeature(styleFeature);
style point

PointFeature 设置文本而非图像,可以直接在地图上的指定坐标处显示文本标注,从而为显示的数据提供清晰的上下文。

PointFeature 使用文本时,可以通过调整 fontfilltextAlign 等属性来自定义文本的外观。

下面的示例演示了如何为 PointFeature 设置文本标签而不是图像:

PointFeature textFeature = new PointFeature(GeometryUtils.createPoint(15, 15))
        .withStyles(new Style()
                .withText(new TextStyle()
                        .withText("Africa")
                        .withFont("20px sans-serif")
                        .withFill(new Fill("#5E4BD8"))
                        .withStroke(new Stroke()
                                .withWidth(2.)
                                .withColor("#A368D5"))));
vectorSource.addFeature(textFeature);
style point text

MarkerFeature 样式

如需修改标记的图标,可以参考下面的示例:

MarkerFeature feature = new MarkerFeature(GeometryUtils.createPoint(20, 20));
feature.removeAllStyles();
feature.addStyles(new Style()
                .withImage(new IconStyle()
                        .withSrc("icons/icon.png")
                        .withScale(0.05)));
vectorSource.addFeature(feature);

此外,根据地理对象属性自定义标记图标的示例在 使用自定义标记 章节。

style marker

LineStringFeature 样式

如需更改 LineStringFeature 的样式,可以定义一个 Style 对象,设置所需的外观属性(如 fillstroke 等),然后将该样式应用于 LineStringFeature

LineString lineString = geometries.createLineString(new Coordinate[]{
        new Coordinate(13, 20),
        new Coordinate(13, 32),
        new Coordinate(25, 17)});

LineStringFeature feature = new LineStringFeature(lineString)
        .withStyles(new Style()
                        .withStroke(new Stroke()
                                .withWidth(3.)
                                .withColor("#F60018")));
vectorSource.addFeature(feature);
style line string

PolygonFeature 样式

如需更改 PolygonFeature 的样式,可以定义一个 Style 对象,设置所需的外观属性(如 fillstroke 等),然后将该样式应用于 PolygonFeature

LinearRing shell = geometries.createLinearRing(new Coordinate[]{
        new Coordinate(1.2457020544488762, 42.476628901048684),
        new Coordinate(-0.054875980233204155, 52.77260344863316),
        new Coordinate(29.858418817454655, 46.105591288830624),
        new Coordinate(1.2457020544488762, 42.476628901048684),
});

PolygonFeature feature = new PolygonFeature(geometries.createPolygon(shell))
        .withStyles(new Style()
                .withFill(new Fill("rgba(1, 147, 154, 0.2)"))
                .withStroke(new Stroke()
                        .withWidth(3.)
                        .withColor("#123EAB")));
vectorSource.addFeature(feature);
style polygon