Skip to content

Commit

Permalink
refactor!: internally manage trackLocationId
Browse files Browse the repository at this point in the history
Close #133
  • Loading branch information
paodb committed Jun 10, 2024
1 parent 682906b commit d049058
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
@JsModule("./googlemaps/geolocation.js")
public class GoogleMap extends Component implements HasSize {

private Integer trackLocationId = null;

/** Base map types supported by Google Maps. */
public enum MapType {
ROADMAP,
Expand Down Expand Up @@ -619,28 +621,37 @@ public Registration addGeolocationErrorEventListener(
*
* <p>Geolocation requires that the user gives consent to location sharing when prompted by the
* browser.</p>
*
* @throws IllegalStateException if a tracking location session is already active.
* The current session must be stopped before starting a new one.
*/
public void trackLocation() {
getElement().executeJs("return geolocation.trackLocation($0)", this).then(Integer.class,
trackLocationId -> {
ComponentUtil.fireEvent(this,
new LocationTrackingActivatedEvent(this, false, trackLocationId));
});
if (this.getTrackLocationId() == null) {
getElement().executeJs("return geolocation.trackLocation($0)", this).then(Integer.class,
trackLocationId -> {
this.trackLocationId = trackLocationId;
ComponentUtil.fireEvent(this, new LocationTrackingActivatedEvent(this, false));
});
} else {
throw new IllegalStateException(
"A tracking location session is already active. Please stop the current session before starting a new one.");
}
}

/**
* Returns track location id if a location tracking was activated.
*
* @return the location tracking id
*/
public Integer getTrackLocationId() {
return trackLocationId;
}

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

private Integer trackLocationId;

public LocationTrackingActivatedEvent(GoogleMap source, boolean fromClient,
Integer trackLocationId) {
public LocationTrackingActivatedEvent(GoogleMap source, boolean fromClient) {
super(source, fromClient);
this.trackLocationId = trackLocationId;
}

public Integer getTrackLocationId() {
return trackLocationId;
}
}

Expand All @@ -657,14 +668,15 @@ public Registration addLocationTrackingActivatedEventListener(
}

/**
* Stops location tracking.
*
* @param trackLocationId the id of the current activated location tracking
* Stops the current location tracking session.
*/
public void stopTrackLocation(Integer trackLocationId) {
getElement().executeJs("geolocation.clearTracking($0)", trackLocationId);
public void stopTrackLocation() {
if(trackLocationId != null) {
getElement().executeJs("geolocation.clearTracking($0)", trackLocationId);
trackLocationId = null;
}
}

/**
* Returns a {@link CompletableFuture} containing the map current {@link LatLonBounds bounds}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.flowingcode.vaadin.addons.googlemaps.GoogleMap.MapType;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;

Expand All @@ -33,8 +34,6 @@
@SuppressWarnings("serial")
public class TrackLocationDemo extends AbstractGoogleMapsDemo {

private Integer trackLocationId = null;

@Override
protected void createGoogleMapsDemo(String apiKey) {
GoogleMap gmaps = new GoogleMap(apiKey, null, null);
Expand All @@ -43,23 +42,32 @@ protected void createGoogleMapsDemo(String apiKey) {
gmaps.setZoom(15);
add(gmaps);

// create button to activate location tracking
Button startLocationTrackingButton =
new Button("Start tracking my location", e -> gmaps.trackLocation());
// create button to stop location tracking
Button stopLocationTrackingButton =
new Button("Stop tracking my location", e -> gmaps.stopTrackLocation(trackLocationId));
add(startLocationTrackingButton, stopLocationTrackingButton);
// create buttons to activate/stop location tracking
Button startLocationTrackingButton = new Button("Start tracking my location");
Button stopLocationTrackingButton = new Button("Stop tracking my location");
startLocationTrackingButton.addClickListener(e -> {
gmaps.trackLocation();
stopLocationTrackingButton.setEnabled(true);
});
startLocationTrackingButton.setDisableOnClick(true);
stopLocationTrackingButton.addClickListener(e -> {
gmaps.stopTrackLocation();
startLocationTrackingButton.setEnabled(true);
});
stopLocationTrackingButton.setEnabled(false);
stopLocationTrackingButton.setDisableOnClick(true);
add(new HorizontalLayout(startLocationTrackingButton, stopLocationTrackingButton));

// create marker to track location
GoogleMapMarker locationMarker = new GoogleMapMarker();
locationMarker.setCaption("You're here");
locationMarker.setDraggable(false);
gmaps.addMarker(locationMarker);

// add listener to obtain id when track location is activated
// add listener to show notification as track location is activated
gmaps.addLocationTrackingActivatedEventListener(ev -> {
trackLocationId = ev.getTrackLocationId();
Notification
.show("Location tracking was activated with track id: " + gmaps.getTrackLocationId());
});

// add listener to know when location was updated and update location marker position
Expand All @@ -68,9 +76,8 @@ protected void createGoogleMapsDemo(String apiKey) {
});

// add listener to capture geolocation error
gmaps.addGeolocationErrorEventListener(e -> {
if (!e.isBrowserHasGeolocationSupport())
Notification.show("Your browser doesn't support geolocation.");
});
gmaps.addGeolocationErrorEventListener(e -> Notification.show(e.isBrowserHasGeolocationSupport()
? "The geolocation service failed on retrieving your location."
: "Your browser doesn't support geolocation."));
}
}

0 comments on commit d049058

Please sign in to comment.