Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internally manage trackLocationId #135

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

<groupId>com.flowingcode.vaadin.addons</groupId>
<artifactId>google-maps</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<name>Google Maps Addon</name>
<description>Integration of google-map for Vaadin platform</description>

<properties>
<vaadin.version>14.10.3</vaadin.version>
<vaadin.version>14.11.11</vaadin.version>

<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
Expand Down
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());
javier-godoy marked this conversation as resolved.
Show resolved Hide resolved
});

// 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."));
}
}
Loading