2020
2121package com .flowingcode .vaadin .addons .googlemaps ;
2222
23- import java .util .HashMap ;
24- import java .util .Map ;
25- import java .util .Optional ;
2623import com .flowingcode .vaadin .addons .demo .DemoSource ;
2724import com .flowingcode .vaadin .addons .googlemaps .GoogleMap .MapType ;
25+ import com .vaadin .flow .component .HtmlComponent ;
26+ import com .vaadin .flow .component .Text ;
2827import com .vaadin .flow .component .button .Button ;
28+ import com .vaadin .flow .component .checkbox .Checkbox ;
2929import com .vaadin .flow .component .combobox .ComboBox ;
30+ import com .vaadin .flow .component .html .Div ;
31+ import com .vaadin .flow .component .icon .Icon ;
32+ import com .vaadin .flow .component .icon .VaadinIcon ;
33+ import com .vaadin .flow .component .notification .Notification ;
3034import com .vaadin .flow .component .orderedlayout .FlexLayout ;
3135import com .vaadin .flow .component .orderedlayout .FlexLayout .FlexWrap ;
36+ import com .vaadin .flow .component .orderedlayout .HorizontalLayout ;
3237import com .vaadin .flow .router .PageTitle ;
3338import com .vaadin .flow .router .Route ;
39+ import java .util .HashMap ;
40+ import java .util .Map ;
41+ import java .util .Optional ;
3442
3543@ PageTitle ("Add Markers Demo" )
3644@ DemoSource
@@ -54,23 +62,62 @@ protected void createGoogleMapsDemo(String apiKey) {
5462 markerColorsMap .put ("Yellow" , Markers .YELLOW );
5563 markerColorsMap .put ("Orange" , Markers .ORANGE );
5664 markerColorsMap .put ("Light blue" , Markers .LIGHTBLUE );
57- ComboBox <String > colorCB = new ComboBox <>();
65+
66+ ComboBox <String > colorCB = new ComboBox <>("Color" );
5867 ReflectionUtil .setItems (colorCB , markerColorsMap .keySet ());
59- colorCB .setPlaceholder ("Marker color" );
68+ colorCB .setPlaceholder ("Choose color" );
69+
70+ Checkbox draggable = new Checkbox ("Draggable" );
71+ Checkbox withRightClick = new Checkbox ("Right Click" );
72+
6073 Button addMarker =
6174 new Button (
6275 "Add Marker" ,
6376 ev -> {
6477 String markerColor =
6578 Optional .ofNullable (markerColorsMap .get (colorCB .getValue ())).orElse ("" );
66- gmaps .addMarker ("New Marker" , gmaps .getCenter (), true , markerColor );
79+ Boolean isDraggable = draggable .getValue ();
80+ GoogleMapMarker marker =
81+ new GoogleMapMarker ("New Marker" , gmaps .getCenter (), isDraggable , markerColor );
82+
83+ if (isDraggable ) {
84+ marker .addDragEndEventListener (e -> Notification .show ("Dragged to -> Latitude: "
85+ + e .getLatitude () + " - Longitude: " + e .getLongitude ()));
86+ }
87+
88+ if (withRightClick .getValue ()) {
89+ marker .addRightClickListener (e -> {
90+ Div text = new Div (new Text ("Alt key: " + e .isAltKey ()), new HtmlComponent ("br" ),
91+ new Text ("Shift key: " + e .isShiftKey ()), new HtmlComponent ("br" ),
92+ new Text ("Ctrol key: " + e .isCtrlKey ()), new HtmlComponent ("br" ),
93+ new Text ("Click counts: " + e .getClickCount ()), new HtmlComponent ("br" ),
94+ new Text ("Latitude: " + e .getLatitude ()), new HtmlComponent ("br" ),
95+ new Text ("Longitude: " + e .getLongitude ()));
96+
97+ Notification notification = new Notification ();
98+
99+ Button closeButton = new Button (new Icon (VaadinIcon .CLOSE_SMALL ));
100+ closeButton .addClickListener (event -> {
101+ notification .close ();
102+ });
103+
104+ HorizontalLayout layout = new HorizontalLayout (text , closeButton );
105+ layout .setAlignItems (Alignment .CENTER );
106+
107+ notification .add (layout );
108+ notification .open ();
109+ });
110+ }
111+ gmaps .addMarker (marker );
67112 });
68113
69114 FlexLayout layout = new FlexLayout ();
70- layout .setFlexWrap (FlexWrap .WRAP );
71- addMarker .addClassName ("margin-button" );
72- colorCB .addClassName ("margin-button" );
73- layout .add (addMarker , colorCB );
115+ layout .setFlexWrap (FlexWrap .WRAP ); // hide-source
116+ addMarker .addClassName ("margin-button" ); // hide-source
117+ colorCB .addClassName ("margin-button" ); // hide-source
118+ layout .add (colorCB , draggable , withRightClick , addMarker );
119+ layout .setAlignItems (Alignment .BASELINE ); // hide-source
120+ layout .getStyle ().set ("margin-top" , "0" ); // hide-source
74121 add (gmaps , layout );
75122 }
76123}
0 commit comments