Skip to content

Commit

Permalink
feat: add google-map-idle listener, add google-map-bounds_changed
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and paodb committed Oct 4, 2023
1 parent 310e39c commit fa3d4d2
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.dependency.NpmPackage;
import com.vaadin.flow.dom.DebouncePhase;
import com.vaadin.flow.dom.DomListenerRegistration;
import com.vaadin.flow.shared.Registration;
import elemental.json.JsonObject;
Expand Down Expand Up @@ -553,4 +554,59 @@ public CompletableFuture<LatLonBounds> getBounds() {
public void setClusteringRenderer(String customRenderer) {
this.getElement().setProperty("customRenderer", customRenderer);
}

/**
* Adds a GoogleMapIdleEvent listener. The listener is called when the map is in idle state (after
* pan or zoom)
*
* @param listener
* @return
*/
public Registration addMapIdleListener(ComponentEventListener<GoogleMapIdleEvent> listener) {
DomListenerRegistration registration =
this.getElement().addEventListener("google-map-idle", ev -> {
listener.onComponentEvent(new GoogleMapIdleEvent(this, true));
});
return registration::remove;
}

public static class GoogleMapIdleEvent extends ComponentEvent<GoogleMap> {
public GoogleMapIdleEvent(GoogleMap source, boolean fromClient) {
super(source, true);
}
}

/**
* Adds a GoogleMapBoundsChangedEvent listener. The listener is called when the map is moved or
* zoomed
*
* @param listener
* @return
*/
public Registration addGoogleMapBoundsChangedListener(
ComponentEventListener<GoogleMapBoundsChangedEvent> listener) {

DomListenerRegistration registration =
this.getElement().addEventListener("google-map-bounds_changed", ev -> {
listener.onComponentEvent(new GoogleMapBoundsChangedEvent(this, true,
new LatLonBounds(ev.getEventData().get("event.detail"))));
}).debounce(1000, DebouncePhase.TRAILING).addEventData("event.detail");
return registration::remove;
}

public static class GoogleMapBoundsChangedEvent extends ComponentEvent<GoogleMap> {

private final LatLonBounds bounds;

public GoogleMapBoundsChangedEvent(GoogleMap source, boolean fromClient,
LatLonBounds latLonBounds) {
super(source, true);
this.bounds = latLonBounds;
}

public LatLonBounds getBounds() {
return bounds;
}
}

}

0 comments on commit fa3d4d2

Please sign in to comment.