Skip to content

Commit fa3d4d2

Browse files
thiasospaodb
authored andcommitted
feat: add google-map-idle listener, add google-map-bounds_changed
1 parent 310e39c commit fa3d4d2

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.vaadin.flow.component.Tag;
3333
import com.vaadin.flow.component.dependency.JsModule;
3434
import com.vaadin.flow.component.dependency.NpmPackage;
35+
import com.vaadin.flow.dom.DebouncePhase;
3536
import com.vaadin.flow.dom.DomListenerRegistration;
3637
import com.vaadin.flow.shared.Registration;
3738
import elemental.json.JsonObject;
@@ -553,4 +554,59 @@ public CompletableFuture<LatLonBounds> getBounds() {
553554
public void setClusteringRenderer(String customRenderer) {
554555
this.getElement().setProperty("customRenderer", customRenderer);
555556
}
557+
558+
/**
559+
* Adds a GoogleMapIdleEvent listener. The listener is called when the map is in idle state (after
560+
* pan or zoom)
561+
*
562+
* @param listener
563+
* @return
564+
*/
565+
public Registration addMapIdleListener(ComponentEventListener<GoogleMapIdleEvent> listener) {
566+
DomListenerRegistration registration =
567+
this.getElement().addEventListener("google-map-idle", ev -> {
568+
listener.onComponentEvent(new GoogleMapIdleEvent(this, true));
569+
});
570+
return registration::remove;
571+
}
572+
573+
public static class GoogleMapIdleEvent extends ComponentEvent<GoogleMap> {
574+
public GoogleMapIdleEvent(GoogleMap source, boolean fromClient) {
575+
super(source, true);
576+
}
577+
}
578+
579+
/**
580+
* Adds a GoogleMapBoundsChangedEvent listener. The listener is called when the map is moved or
581+
* zoomed
582+
*
583+
* @param listener
584+
* @return
585+
*/
586+
public Registration addGoogleMapBoundsChangedListener(
587+
ComponentEventListener<GoogleMapBoundsChangedEvent> listener) {
588+
589+
DomListenerRegistration registration =
590+
this.getElement().addEventListener("google-map-bounds_changed", ev -> {
591+
listener.onComponentEvent(new GoogleMapBoundsChangedEvent(this, true,
592+
new LatLonBounds(ev.getEventData().get("event.detail"))));
593+
}).debounce(1000, DebouncePhase.TRAILING).addEventData("event.detail");
594+
return registration::remove;
595+
}
596+
597+
public static class GoogleMapBoundsChangedEvent extends ComponentEvent<GoogleMap> {
598+
599+
private final LatLonBounds bounds;
600+
601+
public GoogleMapBoundsChangedEvent(GoogleMap source, boolean fromClient,
602+
LatLonBounds latLonBounds) {
603+
super(source, true);
604+
this.bounds = latLonBounds;
605+
}
606+
607+
public LatLonBounds getBounds() {
608+
return bounds;
609+
}
610+
}
611+
556612
}

0 commit comments

Comments
 (0)