Skip to content

Commit 70f58a9

Browse files
paodbjavier-godoy
authored andcommitted
refactor!: internally manage trackLocationId
Close #133
1 parent 682906b commit 70f58a9

File tree

2 files changed

+54
-35
lines changed

2 files changed

+54
-35
lines changed

src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMap.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
@JsModule("./googlemaps/geolocation.js")
5252
public class GoogleMap extends Component implements HasSize {
5353

54+
private Integer trackLocationId = null;
55+
5456
/** Base map types supported by Google Maps. */
5557
public enum MapType {
5658
ROADMAP,
@@ -619,28 +621,37 @@ public Registration addGeolocationErrorEventListener(
619621
*
620622
* <p>Geolocation requires that the user gives consent to location sharing when prompted by the
621623
* browser.</p>
624+
*
625+
* @throws IllegalStateException if a tracking location session is already active.
626+
* The current session must be stopped before starting a new one.
622627
*/
623628
public void trackLocation() {
624-
getElement().executeJs("return geolocation.trackLocation($0)", this).then(Integer.class,
625-
trackLocationId -> {
626-
ComponentUtil.fireEvent(this,
627-
new LocationTrackingActivatedEvent(this, false, trackLocationId));
628-
});
629+
if (this.getTrackLocationId() == null) {
630+
getElement().executeJs("return geolocation.trackLocation($0)", this).then(Integer.class,
631+
trackLocationId -> {
632+
this.trackLocationId = trackLocationId;
633+
ComponentUtil.fireEvent(this, new LocationTrackingActivatedEvent(this, false));
634+
});
635+
} else {
636+
throw new IllegalStateException(
637+
"A tracking location session is already active. Please stop the current session before starting a new one.");
638+
}
639+
}
640+
641+
/**
642+
* Returns track location id if a location tracking was activated.
643+
*
644+
* @return the location tracking id
645+
*/
646+
public Integer getTrackLocationId() {
647+
return trackLocationId;
629648
}
630649

631650
/** Event that is fired when activating location tracking. */
632651
public class LocationTrackingActivatedEvent extends ComponentEvent<GoogleMap> {
633652

634-
private Integer trackLocationId;
635-
636-
public LocationTrackingActivatedEvent(GoogleMap source, boolean fromClient,
637-
Integer trackLocationId) {
653+
public LocationTrackingActivatedEvent(GoogleMap source, boolean fromClient) {
638654
super(source, fromClient);
639-
this.trackLocationId = trackLocationId;
640-
}
641-
642-
public Integer getTrackLocationId() {
643-
return trackLocationId;
644655
}
645656
}
646657

@@ -657,14 +668,15 @@ public Registration addLocationTrackingActivatedEventListener(
657668
}
658669

659670
/**
660-
* Stops location tracking.
661-
*
662-
* @param trackLocationId the id of the current activated location tracking
671+
* Stops the current location tracking session.
663672
*/
664-
public void stopTrackLocation(Integer trackLocationId) {
665-
getElement().executeJs("geolocation.clearTracking($0)", trackLocationId);
673+
public void stopTrackLocation() {
674+
if(trackLocationId != null) {
675+
getElement().executeJs("geolocation.clearTracking($0)", trackLocationId);
676+
trackLocationId = null;
677+
}
666678
}
667-
679+
668680
/**
669681
* Returns a {@link CompletableFuture} containing the map current {@link LatLonBounds bounds}.
670682
*

src/test/java/com/flowingcode/vaadin/addons/googlemaps/TrackLocationDemo.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.flowingcode.vaadin.addons.googlemaps.GoogleMap.MapType;
2525
import com.vaadin.flow.component.button.Button;
2626
import com.vaadin.flow.component.notification.Notification;
27+
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
2728
import com.vaadin.flow.router.PageTitle;
2829
import com.vaadin.flow.router.Route;
2930

@@ -33,8 +34,6 @@
3334
@SuppressWarnings("serial")
3435
public 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

Comments
 (0)