Skip to content

Commit 9ab7000

Browse files
authored
feat: add API to define geolocation options if needed
Close #151
1 parent f3d895e commit 9ab7000

File tree

4 files changed

+125
-14
lines changed

4 files changed

+125
-14
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*-
2+
* #%L
3+
* Google Maps Addon
4+
* %%
5+
* Copyright (C) 2020 - 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.addons.googlemaps;
21+
22+
import elemental.json.Json;
23+
import elemental.json.JsonObject;
24+
import lombok.AllArgsConstructor;
25+
import lombok.Getter;
26+
import lombok.Setter;
27+
28+
/**
29+
* Representation of optional parameters for a geolocation request.
30+
*
31+
* @see <a href=
32+
* https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition#options">Options
33+
* documentation</a>
34+
*/
35+
@Getter
36+
@Setter
37+
@AllArgsConstructor
38+
public class GeolocationOptions {
39+
/**
40+
* A boolean value that indicates the application would like to receive the best possible results.
41+
* If true and if the device is able to provide a more accurate position, it will do so. This may
42+
* result in slower response times or increased power consumption.
43+
*/
44+
private boolean enableHighAccuracy;
45+
46+
/**
47+
* A positive long value representing the maximum length of time (in milliseconds) the device is
48+
* allowed to take in order to return a position.
49+
* If null, the device won't timeout until the position is available.
50+
*/
51+
private Long timeout;
52+
53+
/**
54+
* A positive long value representing the maximum age (in milliseconds) of a possible cached
55+
* position that is acceptable to return.
56+
*/
57+
private long maximumAge;
58+
59+
/**
60+
* Converts the options to a JsonObject to be sent to the client-side.
61+
*
62+
* @return a JsonObject representing the configured options
63+
*/
64+
public JsonObject toJson() {
65+
JsonObject json = Json.createObject();
66+
json.put("enableHighAccuracy", enableHighAccuracy);
67+
json.put("maximumAge", maximumAge);
68+
if (timeout != null) {
69+
json.put("timeout", timeout);
70+
}
71+
return json;
72+
}
73+
}

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

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Google Maps Addon
44
* %%
5-
* Copyright (C) 2020 - 2024 Flowing Code
5+
* Copyright (C) 2020 - 2025 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -535,15 +535,28 @@ public Registration addRightClickListener(ComponentEventListener<GoogleMapClickE
535535
}).addEventData("event.detail.latLng");
536536
return registration::remove;
537537
}
538-
538+
539539
/**
540-
* Sets current location on map.
540+
* Sets current location on map using default geolocation options.
541541
*
542542
* <p>Setting geolocation requires that the user gives consent to location sharing when prompted
543543
* by the browser.
544544
*/
545545
public void goToCurrentLocation() {
546-
getElement().executeJs("geolocation.get($0)", this);
546+
goToCurrentLocation(null);
547+
}
548+
549+
/**
550+
* Sets current location on map using specific geolocation options.
551+
*
552+
* <p>Setting geolocation requires that the user gives consent to location sharing when prompted
553+
* by the browser.
554+
*
555+
* @param options the geolocation options (enableHighAccuracy, timeout, maximumAge)
556+
*/
557+
public void goToCurrentLocation(GeolocationOptions options) {
558+
JsonObject optionsJson = options == null ? Json.createObject() : options.toJson();
559+
getElement().executeJs("geolocation.get($0, $1)", this, optionsJson);
547560
}
548561

549562
@ClientCallable
@@ -610,6 +623,24 @@ public Registration addGeolocationErrorEventListener(
610623
ComponentEventListener<GeolocationErrorEvent> listener) {
611624
return addListener(GeolocationErrorEvent.class, listener);
612625
}
626+
627+
/**
628+
* Activates tracking current location on map using default geolocation options.
629+
*
630+
* <p>Uses <a href=
631+
* "https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition">geolocation#watchPosition</a>
632+
* method to track current position.</p>
633+
*
634+
* <p>Geolocation requires that the user gives consent to location sharing when prompted by the
635+
* browser.</p>
636+
*
637+
* @throws IllegalStateException if a tracking location session is already active.
638+
* The current session must be stopped before starting a new one.
639+
*/
640+
public void trackLocation() {
641+
trackLocation(null);
642+
}
643+
613644

614645
/**
615646
* Activates tracking current location on map.
@@ -621,13 +652,15 @@ public Registration addGeolocationErrorEventListener(
621652
* <p>Geolocation requires that the user gives consent to location sharing when prompted by the
622653
* browser.</p>
623654
*
655+
* @param options the geolocation options (enableHighAccuracy, timeout, maximumAge)
624656
* @throws IllegalStateException if a tracking location session is already active.
625657
* The current session must be stopped before starting a new one.
626658
*/
627-
public void trackLocation() {
659+
public void trackLocation(GeolocationOptions options) {
628660
if (getTrackLocationId() == null) {
629-
getElement().executeJs("return geolocation.trackLocation($0)", this).then(Integer.class,
630-
trackLocationId -> {
661+
JsonObject optionsJson = options == null ? Json.createObject() : options.toJson();
662+
getElement().executeJs("return geolocation.trackLocation($0, $1)", this, optionsJson)
663+
.then(Integer.class, trackLocationId -> {
631664
this.trackLocationId = trackLocationId;
632665
ComponentUtil.fireEvent(this, new LocationTrackingActivatedEvent(this, false));
633666
});

src/main/resources/META-INF/resources/frontend/googlemaps/geolocation.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Google Maps Addon
44
* %%
5-
* Copyright (C) 2020 - 2024 Flowing Code
5+
* Copyright (C) 2020 - 2025 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
1919
*/
2020
window.geolocation = {
2121

22-
get: function(map) {
22+
get: function(map, options) {
2323
// if browser supports geolocation, return current location
2424
if (navigator.geolocation) {
2525

@@ -29,15 +29,16 @@ window.geolocation = {
2929
},
3030
() => {
3131
this._handleGeolocationError(true, map);
32-
}
32+
},
33+
options
3334
);
3435

3536
} else { // browser doesn't support geolocation
3637
this._handleGeolocationError(false, map);
3738
}
3839
},
3940

40-
trackLocation: function(map) {
41+
trackLocation: function(map, options) {
4142
let trackLocationId;
4243
if (navigator.geolocation) {
4344

@@ -47,7 +48,8 @@ window.geolocation = {
4748
},
4849
() => {
4950
this._handleGeolocationError(true, map);
50-
}
51+
},
52+
options
5153
);
5254
} else { // browser doesn't support geolocation
5355
this._handleGeolocationError(false, map);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Google Maps Addon
44
* %%
5-
* Copyright (C) 2020 - 2024 Flowing Code
5+
* Copyright (C) 2020 - 2025 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -41,12 +41,15 @@ protected void createGoogleMapsDemo(String apiKey) {
4141
gmaps.setSizeFull();
4242
gmaps.setZoom(15);
4343
add(gmaps);
44+
45+
// define geolocation options for more accurate tracking
46+
GeolocationOptions options = new GeolocationOptions(true, 5000L, 0L);
4447

4548
// create buttons to activate/stop location tracking
4649
Button startLocationTrackingButton = new Button("Start tracking my location");
4750
Button stopLocationTrackingButton = new Button("Stop tracking my location");
4851
startLocationTrackingButton.addClickListener(e -> {
49-
gmaps.trackLocation();
52+
gmaps.trackLocation(options);
5053
stopLocationTrackingButton.setEnabled(true);
5154
});
5255
startLocationTrackingButton.setDisableOnClick(true);

0 commit comments

Comments
 (0)