Skip to content

Commit 280e364

Browse files
authored
fix: prevent duplicate feature click events for overlapping markers (#2650)
* fix: prevent duplicate feature click events for overlapping markers * add integration test * run formatter * send feature click only for top-level feature * adds feature event details to map click event * improve JavaDoc * make feature list immutable
1 parent ce85257 commit 280e364

11 files changed

Lines changed: 239 additions & 54 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public FeatureEventsPage() {
4141
secondMarkerFeature.setId("second-marker-feature");
4242
secondFeatureLayer.addFeature(secondMarkerFeature);
4343

44+
// Setup several overlapping markers to verify that we receive only one
45+
// event when clicking that location
46+
int numOverlappingMarkers = 3;
47+
for (int i = 0; i < numOverlappingMarkers; i++) {
48+
MarkerFeature overlappingMarker = new MarkerFeature(
49+
new Coordinate(4000000, 0));
50+
overlappingMarker.setId("overlapping-marker-feature-" + (i + 1));
51+
firstFeatureLayer.addFeature(overlappingMarker);
52+
}
53+
4454
Div eventLog = new Div();
4555
eventLog.setId("event-log");
4656
eventLog.getElement().getStyle().set("white-space", "pre");

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
import com.vaadin.flow.component.html.Div;
44
import com.vaadin.flow.component.html.NativeButton;
5+
import com.vaadin.flow.component.map.configuration.Coordinate;
56
import com.vaadin.flow.component.map.configuration.View;
7+
import com.vaadin.flow.component.map.configuration.feature.MarkerFeature;
68
import com.vaadin.flow.router.Route;
79

10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
813
@Route("vaadin-map/map-events")
914
public class MapEventsPage extends Div {
1015
public MapEventsPage() {
@@ -13,12 +18,24 @@ public MapEventsPage() {
1318

1419
Div eventData = new Div();
1520
eventData.setId("event-data");
21+
eventData.getStyle().set("white-space", "pre"); // Allow storing
22+
// newlines in div
1623

1724
Map map = new Map();
1825
map.setHeight("400px");
1926
map.setWidth("400px");
2027
map.getView().setZoom(3);
2128

29+
// Add several overlapping markers to test whether map click event
30+
// contains feature event details
31+
int numOverlappingMarkers = 3;
32+
for (int i = 0; i < numOverlappingMarkers; i++) {
33+
MarkerFeature overlappingMarker = new MarkerFeature(
34+
new Coordinate(2000000, 0));
35+
overlappingMarker.setId("overlapping-marker-feature-" + (i + 1));
36+
map.getFeatureLayer().addFeature(overlappingMarker);
37+
}
38+
2239
NativeButton addMoveEndListener = new NativeButton(
2340
"Add move end listener", e -> {
2441
map.addViewMoveEndEventListener(event -> {
@@ -42,10 +59,18 @@ public MapEventsPage() {
4259
NativeButton addClickListener = new NativeButton("Add click listener",
4360
e -> {
4461
map.addClickEventListener(event -> {
45-
eventData.setText(event.getCoordinate().getX() + ";"
46-
+ event.getCoordinate().getY() + ";"
47-
+ event.getMouseDetails().getAbsoluteX() + ";"
48-
+ event.getMouseDetails().getAbsoluteY());
62+
String coordinatesInfo = event.getCoordinate().getX()
63+
+ ";" + event.getCoordinate().getY();
64+
String pixelPositionInfo = event.getMouseDetails()
65+
.getAbsoluteX() + ";"
66+
+ event.getMouseDetails().getAbsoluteY();
67+
String featureIds = event.getFeatures().stream()
68+
.map(details -> details.getFeature().getId())
69+
.collect(Collectors.joining(";"));
70+
String eventDataText = String.join(
71+
System.lineSeparator(), List.of(coordinatesInfo,
72+
pixelPositionInfo, featureIds));
73+
eventData.setText(eventDataText);
4974
});
5075
});
5176
addClickListener.setId("add-click-listener");

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ public void layerClickListener_capturesEventsFromSingleLayer() {
6969
"click: feature=first-marker-feature | layer=first-feature-layer | source=first-source");
7070
}
7171

72+
@Test
73+
public void overlappingFeatures_singleEventFromTopLevelFeature() {
74+
addFirstLayerFeatureClickListener.click();
75+
76+
// Click on overlapping markers
77+
map.clickAtCoordinates(4000000, 0);
78+
// Click events are delayed by around 250ms, wait for event
79+
waitSeconds(1);
80+
81+
// Should have only one event, from the marker that was last added
82+
assertEventLogHasNumberOfEvents(1);
83+
assertEventLogContainsEvent(
84+
"click: feature=overlapping-marker-feature-3 | layer=first-feature-layer | source=first-source");
85+
}
86+
7287
private void assertEventLogHasNumberOfEvents(int expectedEvents) {
7388
String[] eventLines = eventLog.getText().split(System.lineSeparator());
7489
Assert.assertEquals(expectedEvents, eventLines.length);

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,52 @@ public void changeViewPort_correctEventDataReceived() {
6969
}
7070

7171
@Test
72-
public void mapClick_correctEventDataReceived() {
72+
public void mapClick_correctCoordinatesAndPosition() {
7373
addClickListener.click();
7474

7575
map.clickAtCoordinates(-1956787.9241005122, 1956787.9241005122);
7676

7777
// Click events are delayed, so wait until event data shows up
7878
waitUntilHasText(eventDataDiv);
7979

80-
String[] parts = eventDataDiv.getText().split(";");
80+
String[] eventParts = eventDataDiv.getText()
81+
.split(System.lineSeparator());
82+
String[] coordinateParts = eventParts[0].split(";");
83+
String[] pixelPositionParts = eventParts[1].split(";");
8184

82-
double xCoordinate = Double.parseDouble(parts[0]);
83-
double yCoordinate = Double.parseDouble(parts[1]);
84-
double xPixel = Double.parseDouble(parts[2]);
85-
double yPixel = Double.parseDouble(parts[3]);
85+
double xCoordinate = Double.parseDouble(coordinateParts[0]);
86+
double yCoordinate = Double.parseDouble(coordinateParts[1]);
87+
double xPixel = Double.parseDouble(pixelPositionParts[0]);
88+
double yPixel = Double.parseDouble(pixelPositionParts[1]);
8689

8790
Assert.assertEquals(100, xPixel, 0.1);
8891
Assert.assertEquals(100, yPixel, 0.1);
8992
Assert.assertEquals(-1956787.9241005122, xCoordinate, 0.00000001);
9093
Assert.assertEquals(1956787.9241005122, yCoordinate, 0.00000001);
9194
}
9295

96+
@Test
97+
public void mapClick_containsAllFeaturesAtLocation() {
98+
addClickListener.click();
99+
// Click on location with markers
100+
map.clickAtCoordinates(2000000, 0);
101+
102+
// Click events are delayed, so wait until event data shows up
103+
waitUntilHasText(eventDataDiv);
104+
105+
String[] eventParts = eventDataDiv.getText()
106+
.split(System.lineSeparator());
107+
String[] featureIds = eventParts.length > 2 ? eventParts[2].split(";")
108+
: new String[] {};
109+
110+
// Should have 3 features
111+
Assert.assertEquals(3, featureIds.length);
112+
// In the order that they are displayed
113+
Assert.assertEquals("overlapping-marker-feature-3", featureIds[0]);
114+
Assert.assertEquals("overlapping-marker-feature-2", featureIds[1]);
115+
Assert.assertEquals("overlapping-marker-feature-1", featureIds[2]);
116+
}
117+
93118
private void waitUntilHasText(TestBenchElement element) {
94119
waitUntil(driver -> element.getText() != null
95120
&& !element.getText().isEmpty());

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.vaadin.flow.component.UI;
2525
import com.vaadin.flow.component.map.configuration.Configuration;
2626
import com.vaadin.flow.component.map.configuration.Coordinate;
27+
import com.vaadin.flow.component.map.configuration.Feature;
2728
import com.vaadin.flow.component.map.configuration.View;
2829
import com.vaadin.flow.component.map.configuration.layer.VectorLayer;
2930
import com.vaadin.flow.component.map.events.MapFeatureClickEvent;
@@ -153,7 +154,11 @@ public Registration addViewMoveEndEventListener(
153154
}
154155

155156
/**
156-
* Adds event listener for OpenLayers' @code{click} event.
157+
* Adds a click listener for the map.
158+
* <p>
159+
* Note that the listener will also be invoked when clicking on a
160+
* {@link Feature}. Use {@link MapClickEvent#getFeatures()} to distinguish
161+
* whether a feature exists at the clicked location.
157162
*
158163
* @param listener
159164
* @return a registration object for removing the added listener
@@ -166,13 +171,13 @@ public Registration addClickEventListener(
166171
/**
167172
* Adds a click listener for geographical features. The listener will be
168173
* 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.
174+
* overlapping features, the listener will be invoked only for the top-level
175+
* feature at that location.
171176
*
172177
* @param listener
173178
* the listener to trigger
174179
* @return registration for the listener
175-
* @see com.vaadin.flow.component.map.configuration.Feature
180+
* @see Feature
176181
*/
177182
public Registration addFeatureClickListener(VectorLayer layer,
178183
ComponentEventListener<MapFeatureClickEvent> listener) {
@@ -189,13 +194,13 @@ public Registration addFeatureClickListener(VectorLayer layer,
189194
* invoked for a click on any feature, in any layer. To listen for feature
190195
* clicks in a specific layer, see
191196
* {@link #addFeatureClickListener(VectorLayer, ComponentEventListener)}.
192-
* For clicks on overlapping features, the listener will be invoked
193-
* individually for each feature at the clicked position.
197+
* For clicks on overlapping features, the listener will be invoked only for
198+
* the top-level feature at that location.
194199
*
195200
* @param listener
196201
* the listener to trigger
197202
* @return registration for the listener
198-
* @see com.vaadin.flow.component.map.configuration.Feature
203+
* @see Feature
199204
*/
200205
public Registration addFeatureClickListener(
201206
ComponentEventListener<MapFeatureClickEvent> listener) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.vaadin.flow.component.map.events;
2+
3+
import com.vaadin.flow.component.map.configuration.Feature;
4+
import com.vaadin.flow.component.map.configuration.layer.VectorLayer;
5+
import com.vaadin.flow.component.map.configuration.source.VectorSource;
6+
7+
/**
8+
* Stores information about a {@link Feature} that is part of an event
9+
*/
10+
public class FeatureEventDetails {
11+
private final Feature feature;
12+
private final VectorSource source;
13+
private final VectorLayer layer;
14+
15+
public FeatureEventDetails(Feature feature, VectorSource source,
16+
VectorLayer layer) {
17+
this.feature = feature;
18+
this.source = source;
19+
this.layer = layer;
20+
}
21+
22+
/**
23+
* The feature of the event
24+
*/
25+
public Feature getFeature() {
26+
return feature;
27+
}
28+
29+
/**
30+
* The {@link VectorSource} that contains the feature
31+
*/
32+
public VectorSource getSource() {
33+
return source;
34+
}
35+
36+
/**
37+
* The {@link VectorLayer} that contains the feature
38+
*/
39+
public VectorLayer getLayer() {
40+
return layer;
41+
}
42+
}

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,29 @@
33
import com.vaadin.flow.component.ComponentEvent;
44
import com.vaadin.flow.component.DomEvent;
55
import com.vaadin.flow.component.EventData;
6+
import com.vaadin.flow.component.map.Map;
67
import com.vaadin.flow.component.map.MapBase;
78
import com.vaadin.flow.component.map.configuration.Coordinate;
89
import elemental.json.JsonArray;
910

11+
import java.util.ArrayList;
12+
import java.util.Collections;
13+
import java.util.List;
14+
1015
/**
1116
* Representing OpenLayers' @code{click} event
1217
*/
1318
@DomEvent("map-click")
1419
public class MapClickEvent extends ComponentEvent<MapBase> {
1520

1621
private final Coordinate coordinate;
22+
private final List<FeatureEventDetails> features;
1723
private final MouseEventDetails details;
1824

19-
public MapClickEvent(MapBase source, boolean fromClient,
25+
public MapClickEvent(Map source, boolean fromClient,
2026
@EventData("event.detail.coordinate") JsonArray coordinate,
27+
@EventData("event.detail.features.map(feature => feature.feature.id)") JsonArray featureIds,
28+
@EventData("event.detail.features.map(feature => feature.layer.id)") JsonArray layerIds,
2129
@EventData("event.detail.originalEvent.pageX") int pageX,
2230
@EventData("event.detail.originalEvent.pageY") int pageY,
2331
@EventData("event.detail.originalEvent.altKey") boolean altKey,
@@ -30,8 +38,18 @@ public MapClickEvent(MapBase source, boolean fromClient,
3038
this.coordinate = new Coordinate(coordinate.getNumber(0),
3139
coordinate.getNumber(1));
3240

33-
details = new MouseEventDetails();
41+
List<FeatureEventDetails> features = new ArrayList<>();
42+
for (int i = 0; i < featureIds.length(); i++) {
43+
String featureId = featureIds.getString(i);
44+
String layerId = layerIds.getString(i);
45+
FeatureEventDetails featureEventDetails = MapEventUtil
46+
.getFeatureEventDetails(source.getRawConfiguration(),
47+
layerId, featureId);
48+
features.add(featureEventDetails);
49+
}
50+
this.features = Collections.unmodifiableList(features);
3451

52+
details = new MouseEventDetails();
3553
details.setAbsoluteX(pageX);
3654
details.setAbsoluteY(pageY);
3755
details.setButton(MouseEventDetails.MouseButton.of(button));
@@ -50,6 +68,19 @@ public Coordinate getCoordinate() {
5068
return coordinate;
5169
}
5270

71+
/**
72+
* List of map features at the clicked position. Can be used to distinguish
73+
* whether the click was on the background, or on a feature. The features
74+
* are sorted by their display order, meaning the top-most feature is the
75+
* first item in the list.
76+
*
77+
* @return the list of features at the clicked position, or an empty list
78+
* otherwise
79+
*/
80+
public List<FeatureEventDetails> getFeatures() {
81+
return features;
82+
}
83+
5384
/**
5485
* Gets the click's mouse event details.
5586
*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.vaadin.flow.component.map.events;
2+
3+
import com.vaadin.flow.component.map.configuration.Configuration;
4+
import com.vaadin.flow.component.map.configuration.Feature;
5+
import com.vaadin.flow.component.map.configuration.layer.VectorLayer;
6+
import com.vaadin.flow.component.map.configuration.source.VectorSource;
7+
8+
import java.util.Objects;
9+
import java.util.Optional;
10+
11+
class MapEventUtil {
12+
static FeatureEventDetails getFeatureEventDetails(
13+
Configuration configuration, String layerId, String featureId) {
14+
Optional<VectorLayer> maybeLayer = configuration.getLayers().stream()
15+
.filter(layer -> layer instanceof VectorLayer
16+
&& Objects.equals(layer.getId(), layerId))
17+
.findFirst().map(layer -> (VectorLayer) layer);
18+
Optional<VectorSource> maybeSource = maybeLayer
19+
.map(layer -> (VectorSource) layer.getSource());
20+
Optional<Feature> maybeFeature = maybeSource.flatMap(
21+
vectorSource -> vectorSource.getFeatures().stream().filter(
22+
feature -> Objects.equals(feature.getId(), featureId))
23+
.findFirst());
24+
25+
return new FeatureEventDetails(maybeFeature.orElse(null),
26+
maybeSource.orElse(null), maybeLayer.orElse(null));
27+
}
28+
}

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
import com.vaadin.flow.component.map.configuration.layer.VectorLayer;
99
import com.vaadin.flow.component.map.configuration.source.VectorSource;
1010

11-
import java.util.Objects;
12-
import java.util.Optional;
13-
1411
/**
1512
* Provides data for click events on geographic features
1613
*/
@@ -34,21 +31,12 @@ public MapFeatureClickEvent(Map source, boolean fromClient,
3431
@EventData("event.detail.originalEvent.button") int button) {
3532
super(source, fromClient);
3633

37-
Optional<VectorLayer> maybeLayer = source.getRawConfiguration()
38-
.getLayers().stream()
39-
.filter(layer -> layer instanceof VectorLayer
40-
&& Objects.equals(layer.getId(), layerId))
41-
.findFirst().map(layer -> (VectorLayer) layer);
42-
Optional<VectorSource> maybeVectorSource = maybeLayer
43-
.map(layer -> (VectorSource) layer.getSource());
44-
Optional<Feature> maybeFeature = maybeVectorSource.flatMap(
45-
vectorSource -> vectorSource.getFeatures().stream().filter(
46-
feature -> Objects.equals(feature.getId(), featureId))
47-
.findFirst());
48-
49-
this.layer = maybeLayer.orElse(null);
50-
this.vectorSource = maybeVectorSource.orElse(null);
51-
this.feature = maybeFeature.orElse(null);
34+
FeatureEventDetails featureEventDetails = MapEventUtil
35+
.getFeatureEventDetails(source.getRawConfiguration(), layerId,
36+
featureId);
37+
this.layer = featureEventDetails.getLayer();
38+
this.vectorSource = featureEventDetails.getSource();
39+
this.feature = featureEventDetails.getFeature();
5240

5341
details = new MouseEventDetails();
5442
details.setAbsoluteX(pageX);

0 commit comments

Comments
 (0)