Skip to content

Commit e4fa8d5

Browse files
ngonzalezpazFCjavier-godoy
authored andcommitted
feat: add left click and right click mouse events on map
Close #18
1 parent 83e720a commit e4fa8d5

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMap.java

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,15 +20,20 @@
2020
package com.flowingcode.vaadin.addons.googlemaps;
2121

2222
import java.util.List;
23-
2423
import org.apache.commons.lang3.StringUtils;
25-
24+
import com.vaadin.flow.component.ClickEvent;
2625
import com.vaadin.flow.component.Component;
26+
import com.vaadin.flow.component.ComponentEventListener;
27+
import com.vaadin.flow.component.DomEvent;
28+
import com.vaadin.flow.component.EventData;
2729
import com.vaadin.flow.component.HasSize;
2830
import com.vaadin.flow.component.Synchronize;
2931
import com.vaadin.flow.component.Tag;
3032
import com.vaadin.flow.component.dependency.JsModule;
3133
import com.vaadin.flow.component.dependency.NpmPackage;
34+
import com.vaadin.flow.shared.Registration;
35+
import elemental.json.JsonObject;
36+
import elemental.json.JsonValue;
3237

3338
@SuppressWarnings("serial")
3439
@Tag("google-map")
@@ -155,8 +160,9 @@ public void removePolygon(GoogleMapPolygon polygon) {
155160
*/
156161
public void addMarker(GoogleMapMarker marker) {
157162
this.getElement().appendChild(marker.getElement());
158-
if (this.getElement().getParent() != null)
163+
if (this.getElement().getParent() != null) {
159164
this.getElement().executeJs("this._updateMarkers()");
165+
}
160166
}
161167

162168
@SuppressWarnings("squid:S3242")
@@ -236,4 +242,54 @@ public int getMinZoom() {
236242
return this.getElement().getProperty("minZoom", 1);
237243
}
238244

245+
@DomEvent("google-map-click")
246+
public static class GoogleMapClickEvent extends ClickEvent<GoogleMap> {
247+
private final double lat;
248+
private final double lon;
249+
public double getLatitude() {
250+
return this.lat;
251+
}
252+
public double getLongitude() {
253+
return this.lon;
254+
}
255+
public GoogleMapClickEvent(GoogleMap source, boolean fromClient,
256+
@EventData(value = "event.detail.latLng") JsonValue latLng) {
257+
super(source);
258+
this.lat = ((JsonObject) latLng).getNumber("lat");
259+
this.lon = ((JsonObject) latLng).getNumber("lng");
260+
}
261+
}
262+
263+
public Registration addClickListener(
264+
ComponentEventListener<GoogleMapClickEvent> listener) {
265+
this.getElement().setProperty("clickable", true);
266+
this.getElement().setProperty("clickEvents", true);
267+
return addListener(GoogleMapClickEvent.class, listener);
268+
}
269+
270+
@DomEvent("google-map-rightclick")
271+
public static class GoogleMapRightClickEvent extends ClickEvent<GoogleMap> {
272+
private final double lat;
273+
private final double lon;
274+
public double getLatitude() {
275+
return this.lat;
276+
}
277+
public double getLongitude() {
278+
return this.lon;
279+
}
280+
public GoogleMapRightClickEvent(GoogleMap source, boolean fromClient,
281+
@EventData(value = "event.detail.latLng") JsonValue latLng) {
282+
super(source);
283+
this.lat = ((JsonObject)latLng).getNumber("lat");
284+
this.lon = ((JsonObject)latLng).getNumber("lng");
285+
}
286+
}
287+
288+
public Registration addRightClickListener(
289+
ComponentEventListener<GoogleMapRightClickEvent> listener) {
290+
this.getElement().setProperty("clickable", true);
291+
this.getElement().setProperty("clickEvents", true);
292+
return addListener(GoogleMapRightClickEvent.class, listener);
293+
}
294+
239295
}

src/test/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMapsDemo.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public GoogleMapsDemo() {
4949
gmaps.setMapType(MapType.SATELLITE);
5050
gmaps.setSizeFull();
5151
gmaps.setCenter(new LatLon(-31.636036, -60.7055271));
52+
53+
gmaps.addClickListener(ev -> Notification
54+
.show("Left click at latitude: " + ev.getLatitude()
55+
+ "; Longitude: " + ev.getLongitude()));
56+
gmaps.addRightClickListener(ev -> Notification
57+
.show("Right click at latitude: " + ev.getLatitude()
58+
+ "; Longitude: " + ev.getLongitude()));
59+
5260
GoogleMapMarker flowingmarker = gmaps.addMarker("Center", new LatLon(-31.636036, -60.7055271), true,
5361
"https://www.flowingcode.com/wp-content/uploads/2020/06/FCMarker.png");
5462
flowingmarker.addInfoWindow("<h1>Flowing Code</h1>");

0 commit comments

Comments
 (0)