2424import com .flowingcode .vaadin .addons .googlemaps .GoogleMap .MapType ;
2525import com .vaadin .flow .component .button .Button ;
2626import com .vaadin .flow .component .notification .Notification ;
27+ import com .vaadin .flow .component .orderedlayout .HorizontalLayout ;
2728import com .vaadin .flow .router .PageTitle ;
2829import com .vaadin .flow .router .Route ;
2930
3334@ SuppressWarnings ("serial" )
3435public class TrackLocationDemo extends AbstractGoogleMapsDemo {
3536
36- private Integer trackLocationId = null ;
37-
3837 @ Override
3938 protected void createGoogleMapsDemo (String apiKey ) {
4039 GoogleMap gmaps = new GoogleMap (apiKey , null , null );
@@ -43,23 +42,32 @@ protected void createGoogleMapsDemo(String apiKey) {
4342 gmaps .setZoom (15 );
4443 add (gmaps );
4544
46- // create button to activate location tracking
47- Button startLocationTrackingButton =
48- new Button ("Start tracking my location" , e -> gmaps .trackLocation ());
49- // create button to stop location tracking
50- Button stopLocationTrackingButton =
51- new Button ("Stop tracking my location" , e -> gmaps .stopTrackLocation (trackLocationId ));
52- add (startLocationTrackingButton , stopLocationTrackingButton );
45+ // create buttons to activate/stop location tracking
46+ Button startLocationTrackingButton = new Button ("Start tracking my location" );
47+ Button stopLocationTrackingButton = new Button ("Stop tracking my location" );
48+ startLocationTrackingButton .addClickListener (e -> {
49+ gmaps .trackLocation ();
50+ stopLocationTrackingButton .setEnabled (true );
51+ });
52+ startLocationTrackingButton .setDisableOnClick (true );
53+ stopLocationTrackingButton .addClickListener (e -> {
54+ gmaps .stopTrackLocation ();
55+ startLocationTrackingButton .setEnabled (true );
56+ });
57+ stopLocationTrackingButton .setEnabled (false );
58+ stopLocationTrackingButton .setDisableOnClick (true );
59+ add (new HorizontalLayout (startLocationTrackingButton , stopLocationTrackingButton ));
5360
5461 // create marker to track location
5562 GoogleMapMarker locationMarker = new GoogleMapMarker ();
5663 locationMarker .setCaption ("You're here" );
5764 locationMarker .setDraggable (false );
5865 gmaps .addMarker (locationMarker );
5966
60- // add listener to obtain id when track location is activated
67+ // add listener to show notification as track location is activated
6168 gmaps .addLocationTrackingActivatedEventListener (ev -> {
62- trackLocationId = ev .getTrackLocationId ();
69+ Notification
70+ .show ("Location tracking was activated with track id: " + gmaps .getTrackLocationId ());
6371 });
6472
6573 // add listener to know when location was updated and update location marker position
@@ -68,9 +76,8 @@ protected void createGoogleMapsDemo(String apiKey) {
6876 });
6977
7078 // add listener to capture geolocation error
71- gmaps .addGeolocationErrorEventListener (e -> {
72- if (!e .isBrowserHasGeolocationSupport ())
73- Notification .show ("Your browser doesn't support geolocation." );
74- });
79+ gmaps .addGeolocationErrorEventListener (e -> Notification .show (e .isBrowserHasGeolocationSupport ()
80+ ? "The geolocation service failed on retrieving your location."
81+ : "Your browser doesn't support geolocation." ));
7582 }
7683}
0 commit comments