|
32 | 32 | import com.vaadin.flow.component.Tag; |
33 | 33 | import com.vaadin.flow.component.dependency.JsModule; |
34 | 34 | import com.vaadin.flow.component.dependency.NpmPackage; |
| 35 | +import com.vaadin.flow.dom.DebouncePhase; |
35 | 36 | import com.vaadin.flow.dom.DomListenerRegistration; |
36 | 37 | import com.vaadin.flow.shared.Registration; |
37 | 38 | import elemental.json.JsonObject; |
@@ -553,4 +554,59 @@ public CompletableFuture<LatLonBounds> getBounds() { |
553 | 554 | public void setClusteringRenderer(String customRenderer) { |
554 | 555 | this.getElement().setProperty("customRenderer", customRenderer); |
555 | 556 | } |
| 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 | + |
556 | 612 | } |
0 commit comments