|
42 | 42 | import elemental.json.JsonArray; |
43 | 43 | import elemental.json.JsonObject; |
44 | 44 | import elemental.json.JsonValue; |
| 45 | +import java.util.HashSet; |
45 | 46 | import java.util.List; |
| 47 | +import java.util.Set; |
46 | 48 | import java.util.concurrent.CompletableFuture; |
47 | 49 | import org.apache.commons.lang3.StringUtils; |
48 | 50 |
|
|
54 | 56 | @JsModule("./googlemaps/geolocation.js") |
55 | 57 | public class GoogleMap extends Component implements HasSize { |
56 | 58 |
|
57 | | - private Integer trackLocationId = null; |
| 59 | + private Integer trackLocationId = null; |
| 60 | + |
| 61 | + private Set<CustomControl> customControls = new HashSet<CustomControl>(); |
58 | 62 |
|
59 | 63 | /** Base map types supported by Google Maps. */ |
60 | 64 | public enum MapType { |
@@ -776,21 +780,44 @@ public void addCustomControls(CustomControl... customControls) { |
776 | 780 | } |
777 | 781 | this.getElement().setPropertyJson("customControls", jsonArray); |
778 | 782 | } |
779 | | - |
| 783 | + |
780 | 784 | /** |
781 | 785 | * Sets the custom control buttons to be displayed in the map. |
782 | 786 | * |
783 | 787 | * @param customControls list of custom controls to add to the map |
784 | 788 | */ |
785 | 789 | public void setCustomControls(CustomControl... customControls) { |
786 | | - JsonArray jsonArray = Json.createArray(); |
787 | | - for (int i = 0; i < customControls.length; i++) { |
788 | | - CustomControl customControl = customControls[i]; |
789 | | - jsonArray.set(i, customControl.getJson(i)); |
790 | | - customControl.getControlButton().getElement().setAttribute("slot", "customControlSlot_" + i); |
791 | | - this.getElement().appendChild(customControl.getControlButton().getElement()); |
792 | | - } |
793 | | - this.getElement().setPropertyJson("customControls", jsonArray); |
| 790 | + this.getElement().executeJs("this._removeCustomControls()").then((e) -> { |
| 791 | + JsonArray jsonArray = Json.createArray(); |
| 792 | + for (int i = 0; i < customControls.length; i++) { |
| 793 | + CustomControl customControl = customControls[i]; |
| 794 | + jsonArray.set(i, customControl.getJson(i)); |
| 795 | + customControl.getControlButton().getElement().setAttribute("slot", "customControlSlot_" + i); |
| 796 | + this.getElement().appendChild(customControl.getControlButton().getElement()); |
| 797 | + this.customControls.add(customControl); |
| 798 | + } |
| 799 | + this.getElement().setPropertyJson("customControls", jsonArray); |
| 800 | + }); |
| 801 | + } |
| 802 | + |
| 803 | + /** |
| 804 | + * Adds a custom control to be displayed in the map. |
| 805 | + * |
| 806 | + * @param customControl the custom control to add to the map |
| 807 | + */ |
| 808 | + public void addCustomControl(CustomControl customControl) { |
| 809 | + this.customControls.add(customControl); |
| 810 | + this.setCustomControls(this.customControls.stream().toArray(CustomControl[]::new)); |
| 811 | + } |
| 812 | + |
| 813 | + /** |
| 814 | + * Removes a custom control added to the map. |
| 815 | + * |
| 816 | + * @param customControl the custom control to be removed |
| 817 | + */ |
| 818 | + public void removeCustomControl(CustomControl customControl) { |
| 819 | + this.customControls.remove(customControl); |
| 820 | + this.setCustomControls(this.customControls.stream().toArray(CustomControl[]::new)); |
794 | 821 | } |
795 | 822 |
|
796 | 823 | /** |
|
0 commit comments