Skip to content

Commit 4f45aa7

Browse files
sissbrueckerhdamr
andauthored
feat: map feature click events (#2604)
* feat: feature click events * run formatter * switch to using singleclick event * fix jsdoc Co-authored-by: Hadi Amiri <84700249+hdamr@users.noreply.github.com>
1 parent d438db2 commit 4f45aa7

10 files changed

Lines changed: 419 additions & 36 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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;
7+
import com.vaadin.flow.component.map.configuration.feature.MarkerFeature;
8+
import com.vaadin.flow.component.map.configuration.layer.FeatureLayer;
9+
import com.vaadin.flow.component.map.configuration.layer.VectorLayer;
10+
import com.vaadin.flow.component.map.configuration.source.VectorSource;
11+
import com.vaadin.flow.router.Route;
12+
13+
@Route("vaadin-map/feature-events")
14+
public class FeatureEventsPage extends Div {
15+
public FeatureEventsPage() {
16+
Map map = new Map();
17+
map.setWidthFull();
18+
map.setHeight("400px");
19+
map.getView().setCenter(new Coordinate(0, 0));
20+
map.getView().setZoom(3);
21+
22+
// Setup first feature layer with one marker
23+
FeatureLayer firstFeatureLayer = new FeatureLayer();
24+
firstFeatureLayer.setId("first-feature-layer");
25+
firstFeatureLayer.getSource().setId("first-source");
26+
map.addLayer(firstFeatureLayer);
27+
28+
MarkerFeature firstMarkerFeature = new MarkerFeature(
29+
new Coordinate(0, 0));
30+
firstMarkerFeature.setId("first-marker-feature");
31+
firstFeatureLayer.addFeature(firstMarkerFeature);
32+
33+
// Setup second feature layer with one marker
34+
FeatureLayer secondFeatureLayer = new FeatureLayer();
35+
secondFeatureLayer.setId("second-feature-layer");
36+
secondFeatureLayer.getSource().setId("second-source");
37+
map.addLayer(secondFeatureLayer);
38+
39+
MarkerFeature secondMarkerFeature = new MarkerFeature(
40+
new Coordinate(2000000, 0));
41+
secondMarkerFeature.setId("second-marker-feature");
42+
secondFeatureLayer.addFeature(secondMarkerFeature);
43+
44+
Div eventLog = new Div();
45+
eventLog.setId("event-log");
46+
eventLog.getElement().getStyle().set("white-space", "pre");
47+
48+
NativeButton addGlobalFeatureClickListener = new NativeButton(
49+
"Add global feature click listener", e -> {
50+
map.addFeatureClickListener(event -> {
51+
Feature feature = event.getFeature();
52+
VectorLayer layer = event.getLayer();
53+
VectorSource source = event.getVectorSource();
54+
String eventInfoText = String.format(
55+
"click: feature=%s | layer=%s | source=%s%n",
56+
feature.getId(), layer.getId(), source.getId());
57+
58+
eventLog.setText(eventLog.getText() + eventInfoText);
59+
});
60+
});
61+
addGlobalFeatureClickListener
62+
.setId("add-global-feature-click-listener");
63+
64+
NativeButton addFirstLayerFeatureClickListener = new NativeButton(
65+
"Add feature click listener for first layer only", e -> {
66+
map.addFeatureClickListener(firstFeatureLayer, event -> {
67+
Feature feature = event.getFeature();
68+
VectorLayer layer = event.getLayer();
69+
VectorSource source = event.getVectorSource();
70+
String eventInfoText = String.format(
71+
"click: feature=%s | layer=%s | source=%s%n",
72+
feature.getId(), layer.getId(), source.getId());
73+
74+
eventLog.setText(eventLog.getText() + eventInfoText);
75+
});
76+
});
77+
addFirstLayerFeatureClickListener
78+
.setId("add-first-layer-feature-click-listener");
79+
80+
add(map, new Div(addGlobalFeatureClickListener,
81+
addFirstLayerFeatureClickListener), eventLog);
82+
}
83+
}
Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.vaadin.flow.component.map;
22

33
import com.vaadin.flow.component.html.Div;
4+
import com.vaadin.flow.component.html.NativeButton;
45
import com.vaadin.flow.component.map.configuration.View;
56
import com.vaadin.flow.router.Route;
67

@@ -18,29 +19,38 @@ public MapEventsPage() {
1819
map.setWidth("400px");
1920
map.getView().setZoom(3);
2021

21-
map.addViewMoveEndEventListener(event -> {
22-
View mapView = map.getView();
23-
String stateText = mapView.getCenter().getX() + ";"
24-
+ mapView.getCenter().getY() + ";";
25-
stateText += mapView.getRotation() + ";";
26-
stateText += mapView.getZoom();
27-
28-
viewState.setText(stateText);
29-
30-
String eventDataText = event.getCenter().getX() + ";"
31-
+ event.getCenter().getY() + ";" + event.getRotation() + ";"
32-
+ event.getZoom();
33-
34-
eventData.setText(eventDataText);
35-
});
36-
37-
map.addClickEventListener(event -> {
38-
eventData.setText(event.getCoordinate().getX() + ";"
39-
+ event.getCoordinate().getY() + ";"
40-
+ event.getMouseDetails().getAbsoluteX() + ";"
41-
+ event.getMouseDetails().getAbsoluteY());
42-
});
43-
44-
add(map, viewState, eventData);
22+
NativeButton addMoveEndListener = new NativeButton(
23+
"Add move end listener", e -> {
24+
map.addViewMoveEndEventListener(event -> {
25+
View mapView = map.getView();
26+
String stateText = mapView.getCenter().getX() + ";"
27+
+ mapView.getCenter().getY() + ";";
28+
stateText += mapView.getRotation() + ";";
29+
stateText += mapView.getZoom();
30+
31+
viewState.setText(stateText);
32+
33+
String eventDataText = event.getCenter().getX() + ";"
34+
+ event.getCenter().getY() + ";"
35+
+ event.getRotation() + ";" + event.getZoom();
36+
37+
eventData.setText(eventDataText);
38+
});
39+
});
40+
addMoveEndListener.setId("add-move-end-listener");
41+
42+
NativeButton addClickListener = new NativeButton("Add click listener",
43+
e -> {
44+
map.addClickEventListener(event -> {
45+
eventData.setText(event.getCoordinate().getX() + ";"
46+
+ event.getCoordinate().getY() + ";"
47+
+ event.getMouseDetails().getAbsoluteX() + ";"
48+
+ event.getMouseDetails().getAbsoluteY());
49+
});
50+
});
51+
addClickListener.setId("add-click-listener");
52+
53+
add(map, viewState, eventData,
54+
new Div(addMoveEndListener, addClickListener));
4555
}
4656
}

‎vaadin-map-flow-parent/vaadin-map-flow-integration-tests/src/main/java/com/vaadin/flow/component/map/MapView.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public MapView() {
2828
new Coordinate(1233058.1696443919, 6351912.406929109));
2929
map.getFeatureLayer().addFeature(nurembergMarker);
3030

31+
map.addFeatureClickListener(e -> {
32+
System.out.println(
33+
"Feature click: featureId=" + e.getFeature().getId()
34+
+ ", layerId=" + e.getLayer().getId());
35+
});
36+
3137
NativeButton toggleLayerVisible = new NativeButton("Toggle Layer",
3238
e -> {
3339
Layer layer = map.getBackgroundLayer();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
import java.util.Arrays;
12+
13+
@TestPath("vaadin-map/feature-events")
14+
public class FeatureEventsIT extends AbstractComponentIT {
15+
private MapElement map;
16+
private TestBenchElement addGlobalFeatureClickListener;
17+
private TestBenchElement addFirstLayerFeatureClickListener;
18+
private TestBenchElement eventLog;
19+
20+
@Before
21+
public void init() {
22+
open();
23+
map = $(MapElement.class).waitForFirst();
24+
map.disableInteractions();
25+
addGlobalFeatureClickListener = $("button")
26+
.id("add-global-feature-click-listener");
27+
addFirstLayerFeatureClickListener = $("button")
28+
.id("add-first-layer-feature-click-listener");
29+
eventLog = $("div").id("event-log");
30+
}
31+
32+
@Test
33+
public void globalClickListener_capturesEventsFromAllLayers() {
34+
addGlobalFeatureClickListener.click();
35+
36+
// Click on first marker
37+
map.clickAtCoordinates(0, 0);
38+
// To prevent double-clicking, wait before we trigger the next event
39+
waitSeconds(1);
40+
// Click on second marker
41+
map.clickAtCoordinates(2000000, 0);
42+
// Click events are delayed by around 250ms, wait for last event
43+
waitSeconds(1);
44+
45+
// Should have two events, one from each marker
46+
assertEventLogHasNumberOfEvents(2);
47+
assertEventLogContainsEvent(
48+
"click: feature=first-marker-feature | layer=first-feature-layer | source=first-source");
49+
assertEventLogContainsEvent(
50+
"click: feature=second-marker-feature | layer=second-feature-layer | source=second-source");
51+
}
52+
53+
@Test
54+
public void layerClickListener_capturesEventsFromSingleLayer() {
55+
addFirstLayerFeatureClickListener.click();
56+
57+
// Click on first marker
58+
map.clickAtCoordinates(0, 0);
59+
// To prevent double-clicking, wait before we trigger the next event
60+
waitSeconds(1);
61+
// Click on second marker
62+
map.clickAtCoordinates(2000000, 0);
63+
// Click events are delayed by around 250ms, wait for last event
64+
waitSeconds(1);
65+
66+
// Should have only one event, from the first marker
67+
assertEventLogHasNumberOfEvents(1);
68+
assertEventLogContainsEvent(
69+
"click: feature=first-marker-feature | layer=first-feature-layer | source=first-source");
70+
}
71+
72+
private void assertEventLogHasNumberOfEvents(int expectedEvents) {
73+
String[] eventLines = eventLog.getText().split(System.lineSeparator());
74+
Assert.assertEquals(expectedEvents, eventLines.length);
75+
}
76+
77+
private void assertEventLogContainsEvent(String expectedEvent) {
78+
String[] eventLines = eventLog.getText().split(System.lineSeparator());
79+
Assert.assertTrue("Event was not triggered: " + expectedEvent,
80+
Arrays.asList(eventLines).contains(expectedEvent));
81+
}
82+
83+
private void waitSeconds(int seconds) {
84+
long start = System.currentTimeMillis();
85+
waitUntil(driver -> {
86+
long now = System.currentTimeMillis();
87+
double delta = Math.floor((now - start) / 1000f);
88+
return delta > seconds;
89+
});
90+
}
91+
}

‎vaadin-map-flow-parent/vaadin-map-flow-integration-tests/src/test/java/com/vaadin/flow/components/map/MapEventsIT.java‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,32 @@
1010

1111
@TestPath("vaadin-map/map-events")
1212
public class MapEventsIT extends AbstractComponentIT {
13+
private MapElement map;
14+
private TestBenchElement viewStateDiv;
15+
private TestBenchElement eventDataDiv;
16+
private TestBenchElement addMoveEndListener;
17+
private TestBenchElement addClickListener;
18+
1319
@Before
1420
public void init() {
1521
open();
22+
map = $(MapElement.class).waitForFirst();
23+
viewStateDiv = $("div").id("view-state");
24+
eventDataDiv = $("div").id("event-data");
25+
addMoveEndListener = $("button").id("add-move-end-listener");
26+
addClickListener = $("button").id("add-click-listener");
1627
}
1728

1829
@Test
1930
public void changeViewPort_viewStateUpdated() {
20-
MapElement map = $(MapElement.class).first();
21-
TestBenchElement stateTextDiv = $("div").id("view-state");
22-
31+
addMoveEndListener.click();
2332
// We are simulating user changing the view port of the map
2433
map.evaluateOLExpression(
2534
"map.getView().setCenter([4849385.650796606, 5487570.011434158]);");
2635
map.evaluateOLExpression("map.getView().setRotation(5);");
2736
map.evaluateOLExpression("map.getView().setZoom(6)");
2837

29-
String[] parts = stateTextDiv.getText().split(";");
38+
String[] parts = viewStateDiv.getText().split(";");
3039
double centerX = Double.parseDouble(parts[0]);
3140
double centerY = Double.parseDouble(parts[1]);
3241
float rotation = Float.parseFloat(parts[2]);
@@ -40,9 +49,7 @@ public void changeViewPort_viewStateUpdated() {
4049

4150
@Test
4251
public void changeViewPort_correctEventDataReceived() {
43-
MapElement map = $(MapElement.class).first();
44-
TestBenchElement eventDataDiv = $("div").id("event-data");
45-
52+
addMoveEndListener.click();
4653
// We are simulating user changing the view port of the map
4754
map.evaluateOLExpression(
4855
"map.getView().setCenter([4849385.650796606, 5487570.011434158]);");
@@ -63,11 +70,13 @@ public void changeViewPort_correctEventDataReceived() {
6370

6471
@Test
6572
public void mapClick_correctEventDataReceived() {
66-
MapElement map = $(MapElement.class).waitForFirst();
67-
TestBenchElement eventDataDiv = $("div").id("event-data");
73+
addClickListener.click();
6874

6975
map.clickAtCoordinates(-1956787.9241005122, 1956787.9241005122);
7076

77+
// Click events are delayed, so wait until event data shows up
78+
waitUntilHasText(eventDataDiv);
79+
7180
String[] parts = eventDataDiv.getText().split(";");
7281

7382
double xCoordinate = Double.parseDouble(parts[0]);
@@ -81,4 +90,8 @@ public void mapClick_correctEventDataReceived() {
8190
Assert.assertEquals(1956787.9241005122, yCoordinate, 0.00000001);
8291
}
8392

93+
private void waitUntilHasText(TestBenchElement element) {
94+
waitUntil(driver -> element.getText() != null
95+
&& !element.getText().isEmpty());
96+
}
8497
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.vaadin.flow.component.map.configuration.Configuration;
2626
import com.vaadin.flow.component.map.configuration.Coordinate;
2727
import com.vaadin.flow.component.map.configuration.View;
28+
import com.vaadin.flow.component.map.configuration.layer.VectorLayer;
29+
import com.vaadin.flow.component.map.events.MapFeatureClickEvent;
2830
import com.vaadin.flow.component.map.configuration.Extent;
2931
import com.vaadin.flow.component.map.events.MapClickEvent;
3032
import com.vaadin.flow.component.map.events.MapViewMoveEndEvent;
@@ -34,6 +36,7 @@
3436
import elemental.json.JsonValue;
3537

3638
import java.beans.PropertyChangeEvent;
39+
import java.util.Objects;
3740

3841
public abstract class MapBase extends Component implements HasSize {
3942
private final Configuration configuration;
@@ -160,6 +163,45 @@ public Registration addClickEventListener(
160163
return addListener(MapClickEvent.class, listener);
161164
}
162165

166+
/**
167+
* Adds a click listener for geographical features. The listener will be
168+
* invoked for a click on any feature in the specified layer. For clicks on
169+
* overlapping features, the listener will be invoked individually for each
170+
* feature at the clicked position.
171+
*
172+
* @param listener
173+
* the listener to trigger
174+
* @return registration for the listener
175+
* @see com.vaadin.flow.component.map.configuration.Feature
176+
*/
177+
public Registration addFeatureClickListener(VectorLayer layer,
178+
ComponentEventListener<MapFeatureClickEvent> listener) {
179+
return addListener(MapFeatureClickEvent.class, event -> {
180+
// Filter events for specified layer
181+
if (!Objects.equals(layer, event.getLayer()))
182+
return;
183+
listener.onComponentEvent(event);
184+
});
185+
}
186+
187+
/**
188+
* Adds a click listener for geographical features. The listener will be
189+
* invoked for a click on any feature, in any layer. To listen for feature
190+
* clicks in a specific layer, see
191+
* {@link #addFeatureClickListener(VectorLayer, ComponentEventListener)}.
192+
* For clicks on overlapping features, the listener will be invoked
193+
* individually for each feature at the clicked position.
194+
*
195+
* @param listener
196+
* the listener to trigger
197+
* @return registration for the listener
198+
* @see com.vaadin.flow.component.map.configuration.Feature
199+
*/
200+
public Registration addFeatureClickListener(
201+
ComponentEventListener<MapFeatureClickEvent> listener) {
202+
return addListener(MapFeatureClickEvent.class, listener);
203+
}
204+
163205
/**
164206
* Checks whether the map component feature flag is active. Succeeds if the
165207
* flag is enabled, and throws otherwise.

0 commit comments

Comments
 (0)