|
| 1 | +package software.xdev.vaadin.maps.leaflet.flow.demo; |
| 2 | + |
| 3 | +import com.vaadin.flow.component.button.Button; |
| 4 | +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; |
| 5 | +import com.vaadin.flow.component.orderedlayout.VerticalLayout; |
| 6 | +import com.vaadin.flow.router.Route; |
| 7 | + |
| 8 | +import software.xdev.vaadin.maps.leaflet.MapContainer; |
| 9 | +import software.xdev.vaadin.maps.leaflet.basictypes.LLatLng; |
| 10 | +import software.xdev.vaadin.maps.leaflet.layer.raster.LTileLayer; |
| 11 | +import software.xdev.vaadin.maps.leaflet.layer.ui.LMarker; |
| 12 | +import software.xdev.vaadin.maps.leaflet.map.LMap; |
| 13 | +import software.xdev.vaadin.maps.leaflet.registry.LComponentManagementRegistry; |
| 14 | +import software.xdev.vaadin.maps.leaflet.registry.LDefaultComponentManagementRegistry; |
| 15 | + |
| 16 | + |
| 17 | +@Route(EventDemo.NAV) |
| 18 | +@SuppressWarnings("checkstyle:MagicNumber") |
| 19 | +public class EventDemo extends VerticalLayout |
| 20 | +{ |
| 21 | + public static final String NAV = "/event"; |
| 22 | + |
| 23 | + public EventDemo() |
| 24 | + { |
| 25 | + // Let the view use 100% of the site |
| 26 | + this.setSizeFull(); |
| 27 | + |
| 28 | + // Create the registry which is needed so that components can be reused and their methods invoked |
| 29 | + // Note: You normally don't need to invoke any methods of the registry and just hand it over to the components |
| 30 | + final LComponentManagementRegistry reg = new LDefaultComponentManagementRegistry(this); |
| 31 | + |
| 32 | + // Create and add the MapContainer (which contains the map) to the UI |
| 33 | + final MapContainer mapContainer = new MapContainer(reg); |
| 34 | + mapContainer.setSizeFull(); |
| 35 | + this.add(mapContainer); |
| 36 | + |
| 37 | + final LMap map = mapContainer.getlMap(); |
| 38 | + |
| 39 | + // Add a (default) TileLayer so that we can see something on the map |
| 40 | + map.addLayer(LTileLayer.createDefaultForOpenStreetMapTileServer(reg)); |
| 41 | + |
| 42 | + // Set what part of the world should be shown |
| 43 | + map.setView(new LLatLng(reg, 49.6751, 12.1607), 17); |
| 44 | + |
| 45 | + // Create a new marker |
| 46 | + new LMarker(reg, new LLatLng(reg, 49.6756, 12.1610)) |
| 47 | + // Bind a popup which is displayed when clicking the marker |
| 48 | + .bindPopup("XDEV Software") |
| 49 | + // Add it to the map |
| 50 | + .addTo(map); |
| 51 | + |
| 52 | + final HorizontalLayout hlButtons = new HorizontalLayout(); |
| 53 | + |
| 54 | + // Bind clickFunc to map so that it gets disposed when the map is disposed |
| 55 | + final String clickFuncReference = map.clientComponentJsAccessor() + ".myCoolClickFunc"; |
| 56 | + reg.execJs(clickFuncReference + "=e => alert('You clicked: ' + e.latlng)"); |
| 57 | + |
| 58 | + final String type = "click"; |
| 59 | + hlButtons.add(new Button("Register click", ev -> map.on(type, clickFuncReference))); |
| 60 | + hlButtons.add(new Button("Register click once", ev -> map.once(type, clickFuncReference))); |
| 61 | + hlButtons.add(new Button("Unregister click", ev -> map.off(type, clickFuncReference))); |
| 62 | + |
| 63 | + hlButtons.setWidthFull(); |
| 64 | + this.add(hlButtons); |
| 65 | + } |
| 66 | +} |
0 commit comments