Skip to content

Commit 380c6bf

Browse files
authored
feat: set labels for map markers (#4632)
* feat: set labels for map markers * fix grammar * extend label IT to cover multiple labels
1 parent 20336fa commit 380c6bf

5 files changed

Lines changed: 251 additions & 2 deletions

File tree

  • vaadin-map-flow-parent
    • vaadin-map-flow-integration-tests/src
    • vaadin-map-flow/src/main
      • java/com/vaadin/flow/component/map/configuration
      • resources/META-INF/resources/frontend/vaadin-map/synchronization
    • vaadin-map-testbench/src/main/java/com/vaadin/flow/component/map/testbench
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.vaadin.flow.component.map;
2+
3+
import com.vaadin.flow.component.html.Div;
4+
import com.vaadin.flow.component.html.NativeButton;
5+
import com.vaadin.flow.component.map.configuration.Coordinate;
6+
import com.vaadin.flow.component.map.configuration.feature.MarkerFeature;
7+
import com.vaadin.flow.router.Route;
8+
9+
@Route("vaadin-map/feature-label")
10+
public class FeatureLabelPage extends Div {
11+
public FeatureLabelPage() {
12+
Map map = new Map();
13+
map.getFeatureLayer().setId("feature-layer");
14+
15+
MarkerFeature marker1 = new MarkerFeature();
16+
marker1.setId("marker1");
17+
marker1.setLabel("Marker label 1");
18+
map.getFeatureLayer().addFeature(marker1);
19+
20+
MarkerFeature marker2 = new MarkerFeature(new Coordinate(30, 0));
21+
marker2.setId("marker2");
22+
marker2.setLabel("Marker label 2");
23+
map.getFeatureLayer().addFeature(marker2);
24+
25+
MarkerFeature marker3 = new MarkerFeature(new Coordinate(60, 0));
26+
marker3.setId("marker3");
27+
marker3.setLabel("Marker label 3");
28+
map.getFeatureLayer().addFeature(marker3);
29+
30+
NativeButton updateLabelText = new NativeButton("Update label text",
31+
e -> {
32+
marker1.setLabel("Updated label 1");
33+
marker2.setLabel("Updated label 2");
34+
marker3.setLabel("Updated label 3");
35+
});
36+
updateLabelText.setId("update-label-text");
37+
38+
NativeButton removeLabelText = new NativeButton("Remove label texts",
39+
e -> {
40+
marker1.setLabel(null);
41+
marker2.setLabel(null);
42+
marker3.setLabel(null);
43+
});
44+
removeLabelText.setId("remove-label-text");
45+
46+
add(map);
47+
add(new Div(updateLabelText, removeLabelText));
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.vaadin.flow.components.map;
2+
3+
import com.vaadin.flow.component.map.testbench.MapElement;
4+
import com.vaadin.flow.testutil.TestPath;
5+
import com.vaadin.testbench.TestBenchElement;
6+
import com.vaadin.tests.AbstractComponentIT;
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
@TestPath("vaadin-map/feature-label")
12+
public class FeatureLabelIT extends AbstractComponentIT {
13+
private MapElement map;
14+
private TestBenchElement updateLabelText;
15+
private TestBenchElement removeLabelText;
16+
17+
@Before
18+
public void init() {
19+
open();
20+
map = $(MapElement.class).waitForFirst();
21+
updateLabelText = $(TestBenchElement.class).id("update-label-text");
22+
removeLabelText = $(TestBenchElement.class).id("remove-label-text");
23+
}
24+
25+
@Test
26+
public void defaultLabelStyle() {
27+
MapElement.TextReference text = getMarkerTextStyle("marker1");
28+
29+
Assert.assertEquals("13px sans-serif", text.getFont());
30+
Assert.assertEquals("#333", text.getFill().getColor());
31+
Assert.assertEquals("#fff", text.getStroke().getColor());
32+
Assert.assertEquals(3, text.getStroke().getWidth());
33+
Assert.assertEquals(0, text.getOffsetX());
34+
Assert.assertEquals(10, text.getOffsetY());
35+
}
36+
37+
@Test
38+
public void initialLabels() {
39+
Assert.assertEquals("Marker label 1",
40+
getMarkerTextStyle("marker1").getText());
41+
Assert.assertEquals("Marker label 2",
42+
getMarkerTextStyle("marker2").getText());
43+
Assert.assertEquals("Marker label 3",
44+
getMarkerTextStyle("marker3").getText());
45+
}
46+
47+
@Test
48+
public void updateLabels() {
49+
updateLabelText.click();
50+
51+
Assert.assertEquals("Updated label 1",
52+
getMarkerTextStyle("marker1").getText());
53+
Assert.assertEquals("Updated label 2",
54+
getMarkerTextStyle("marker2").getText());
55+
Assert.assertEquals("Updated label 3",
56+
getMarkerTextStyle("marker3").getText());
57+
}
58+
59+
@Test
60+
public void removeLabels() {
61+
removeLabelText.click();
62+
63+
Assert.assertNull(getMarkerTextStyle("marker1").getText());
64+
Assert.assertNull(getMarkerTextStyle("marker2").getText());
65+
Assert.assertNull(getMarkerTextStyle("marker3").getText());
66+
}
67+
68+
private MapElement.TextReference getMarkerTextStyle(String markerId) {
69+
MapElement.MapReference mapReference = map.getMapReference();
70+
MapElement.VectorSourceReference vectorSource = mapReference.getLayers()
71+
.getLayer("feature-layer").getSource().asVectorSource();
72+
MapElement.FeatureReference feature = vectorSource.getFeatures()
73+
.getFeature(markerId);
74+
return feature.getStyle().getText();
75+
}
76+
}

‎vaadin-map-flow-parent/vaadin-map-flow/src/main/java/com/vaadin/flow/component/map/configuration/Feature.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public abstract class Feature extends AbstractConfigurationObject {
2727

2828
private SimpleGeometry geometry;
2929
private Style style;
30+
private String label;
3031
private boolean draggable;
3132

3233
@Override
@@ -83,6 +84,27 @@ public void setStyle(Style style) {
8384
addChild(style);
8485
}
8586

87+
/**
88+
* The label that should be displayed next to the feature.
89+
*
90+
* @return the label string
91+
*/
92+
public String getLabel() {
93+
return label;
94+
}
95+
96+
/**
97+
* Sets the label that should be displayed next to the feature. Set to
98+
* {@code null} to remove the label.
99+
*
100+
* @param label
101+
* the new label string, or {@code null} to remove the label
102+
*/
103+
public void setLabel(String label) {
104+
this.label = label;
105+
markAsDirty();
106+
}
107+
86108
/**
87109
* Whether the feature can be dragged on the map using pointing devices or
88110
* not

‎vaadin-map-flow-parent/vaadin-map-flow/src/main/resources/META-INF/resources/frontend/vaadin-map/synchronization/index.js‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010
import Feature from 'ol/Feature';
1111
import Point from 'ol/geom/Point';
12+
import Fill from 'ol/style/Fill';
13+
import Stroke from 'ol/style/Stroke';
14+
import Text from 'ol/style/Text';
1215
import View from 'ol/View';
1316
import { synchronizeImageLayer, synchronizeTileLayer, synchronizeVectorLayer } from './layers.js';
1417
import {
@@ -21,6 +24,16 @@ import {
2124
import { synchronizeIcon, synchronizeFill, synchronizeStroke, synchronizeStyle } from './styles.js';
2225
import { convertToCoordinateArray, synchronizeCollection } from './util.js';
2326

27+
/**
28+
* Fallback text style to use for features that don't have a custom one
29+
*/
30+
const fallbackTextStyle = new Text({
31+
font: '13px sans-serif',
32+
offsetY: 10,
33+
fill: new Fill({ color: '#333' }),
34+
stroke: new Stroke({ color: '#fff', width: 3 })
35+
});
36+
2437
function synchronizeMap(target, source, context) {
2538
if (!target) {
2639
throw new Error('Existing map instance must be provided');
@@ -62,7 +75,31 @@ function synchronizeFeature(target, source, context) {
6275
}
6376

6477
target.setGeometry(context.lookup.get(source.geometry));
65-
target.setStyle(context.lookup.get(source.style));
78+
79+
// Define style function that is run before rendering each feature. The
80+
// function supports using a fallback text style for rendering labels in case
81+
// the feature doesn't define its own text style.
82+
// Acquire reference to style instance outside of style function, otherwise
83+
// there would be no reference to the instance, and it might get garbage
84+
// collected.
85+
const style = context.lookup.get(source.style);
86+
target.setStyle(() => {
87+
if (!style) {
88+
return undefined;
89+
}
90+
// If feature has a label but no custom text style, then use default text
91+
// style
92+
if (source.label && !style.getText()) {
93+
style.setText(fallbackTextStyle);
94+
}
95+
// Set the feature's label on the text style. This is safe even when using
96+
// the default text style instance, as for each feature using the default
97+
// text style, this function will be called again before rendering.
98+
if (style.getText()) {
99+
style.getText().setText(source.label);
100+
}
101+
return style;
102+
});
66103
target.draggable = source.draggable;
67104

68105
return target;

‎vaadin-map-flow-parent/vaadin-map-testbench/src/main/java/com/vaadin/flow/component/map/testbench/MapElement.java‎

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,12 @@ public long getLength() {
480480
public FeatureReference getFeature(int index) {
481481
return new FeatureReference(executor, path("item(%s)", index));
482482
}
483+
484+
public FeatureReference getFeature(String featureId) {
485+
return new FeatureReference(executor,
486+
path("getArray().find(feature => feature.id === '%s')",
487+
featureId));
488+
}
483489
}
484490

485491
public static class FeatureReference extends ConfigurationObjectReference {
@@ -493,7 +499,7 @@ public GeometryReference getGeometry() {
493499
}
494500

495501
public StyleReference getStyle() {
496-
return new StyleReference(executor, path("getStyle()"));
502+
return new StyleReference(executor, path("getStyle()()"));
497503
}
498504
}
499505

@@ -505,6 +511,10 @@ private StyleReference(ExpressionExecutor executor, String expression) {
505511
public IconReference getImage() {
506512
return new IconReference(executor, path("getImage()"));
507513
}
514+
515+
public TextReference getText() {
516+
return new TextReference(executor, path("getText()"));
517+
}
508518
}
509519

510520
public static class GeometryReference extends ConfigurationObjectReference {
@@ -554,4 +564,59 @@ public String getSrc() {
554564
return getString("getSrc()");
555565
}
556566
}
567+
568+
public static class TextReference extends ConfigurationObjectReference {
569+
private TextReference(ExpressionExecutor executor, String expression) {
570+
super(executor, expression);
571+
}
572+
573+
public String getText() {
574+
return getString("getText()");
575+
}
576+
577+
public String getFont() {
578+
return getString("getFont()");
579+
}
580+
581+
public int getOffsetX() {
582+
return getInt("getOffsetX()");
583+
}
584+
585+
public int getOffsetY() {
586+
return getInt("getOffsetY()");
587+
}
588+
589+
public FillReference getFill() {
590+
return new FillReference(executor, path("getFill()"));
591+
}
592+
593+
public StrokeReference getStroke() {
594+
return new StrokeReference(executor, path("getStroke()"));
595+
}
596+
}
597+
598+
public static class FillReference extends ConfigurationObjectReference {
599+
private FillReference(ExpressionExecutor executor, String expression) {
600+
super(executor, expression);
601+
}
602+
603+
public String getColor() {
604+
return getString("getColor()");
605+
}
606+
}
607+
608+
public static class StrokeReference extends ConfigurationObjectReference {
609+
private StrokeReference(ExpressionExecutor executor,
610+
String expression) {
611+
super(executor, expression);
612+
}
613+
614+
public String getColor() {
615+
return getString("getColor()");
616+
}
617+
618+
public Number getWidth() {
619+
return getInt("getWidth()");
620+
}
621+
}
557622
}

0 commit comments

Comments
 (0)