Skip to content

Commit 2616038

Browse files
paodbjavier-godoy
authored andcommitted
feat: add geolocation to center map on user's location
Close #36
1 parent 25a3fbc commit 2616038

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
package com.flowingcode.vaadin.addons.googlemaps;
2121

2222
import com.vaadin.flow.component.ClickEvent;
23+
import com.vaadin.flow.component.ClientCallable;
2324
import com.vaadin.flow.component.Component;
25+
import com.vaadin.flow.component.ComponentEvent;
2426
import com.vaadin.flow.component.ComponentEventListener;
27+
import com.vaadin.flow.component.ComponentUtil;
2528
import com.vaadin.flow.component.EventData;
2629
import com.vaadin.flow.component.HasSize;
2730
import com.vaadin.flow.component.Synchronize;
@@ -39,6 +42,7 @@
3942
@Tag("google-map")
4043
@JsModule("@flowingcode/google-map/google-map.js")
4144
@NpmPackage(value = "@flowingcode/google-map", version = "3.0.2")
45+
@JsModule("./googlemaps/geolocation.js")
4246
public class GoogleMap extends Component implements HasSize {
4347

4448
/** Base map types supported by Google Maps. */
@@ -293,4 +297,79 @@ public Registration addRightClickListener(ComponentEventListener<GoogleMapClickE
293297
.addEventData("event.detail.latLng");
294298
return registration::remove;
295299
}
300+
301+
/**
302+
* Sets current location on map.
303+
*
304+
* <p>Setting geolocation requires that the user gives consent to location sharing when prompted
305+
* by the browser.
306+
*/
307+
public void goToCurrentLocation() {
308+
getElement().executeJs("geolocation.get($0)", this);
309+
}
310+
311+
@ClientCallable
312+
private void handleGeolocation(double latitude, double longitude) {
313+
this.setCenter(new LatLon(latitude, longitude));
314+
ComponentUtil.fireEvent(this, new CurrentLocationEvent(this, false));
315+
}
316+
317+
/**
318+
* Handles what to do if browser doesn't have permission to get the location or if browser doesn't
319+
* support geolocation.
320+
*
321+
* @param browserHasGeolocationSupport whether browser supports geolocation or not
322+
*/
323+
@ClientCallable
324+
private void handleGeolocationError(boolean browserHasGeolocationSupport) {
325+
ComponentUtil.fireEvent(
326+
this, new GeolocationErrorEvent(this, false, browserHasGeolocationSupport));
327+
}
328+
329+
/** Event that is fired when setting current location on map. */
330+
public class CurrentLocationEvent extends ComponentEvent<GoogleMap> {
331+
332+
public CurrentLocationEvent(GoogleMap source, boolean fromClient) {
333+
super(source, fromClient);
334+
}
335+
}
336+
337+
/** Event that is called when current location can't be found. */
338+
public class GeolocationErrorEvent extends ComponentEvent<GoogleMap> {
339+
340+
private boolean browserHasGeolocationSupport;
341+
342+
public GeolocationErrorEvent(
343+
GoogleMap source, boolean fromClient, boolean browserHasGeolocationSupport) {
344+
super(source, fromClient);
345+
this.browserHasGeolocationSupport = browserHasGeolocationSupport;
346+
}
347+
348+
public boolean isBrowserHasGeolocationSupport() {
349+
return browserHasGeolocationSupport;
350+
}
351+
}
352+
353+
/**
354+
* Adds a CurrentLocationEvent listener. The listener is called when setting the current location.
355+
*
356+
* @param listener
357+
* @return
358+
*/
359+
public Registration addCurrentLocationEventListener(
360+
ComponentEventListener<CurrentLocationEvent> listener) {
361+
return addListener(CurrentLocationEvent.class, listener);
362+
}
363+
364+
/**
365+
* Adds a GeolocationErrorEvent listener. The listener is called when current location can't be
366+
* found.
367+
*
368+
* @param listener
369+
* @return
370+
*/
371+
public Registration addGeolocationErrorEventListener(
372+
ComponentEventListener<GeolocationErrorEvent> listener) {
373+
return addListener(GeolocationErrorEvent.class, listener);
374+
}
296375
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
window.geolocation = {
2+
3+
get: function(map) {
4+
// if browser supports geolocation, return current location
5+
if(navigator.geolocation) {
6+
7+
navigator.geolocation.getCurrentPosition(
8+
position => {
9+
map.$server.handleGeolocation(position.coords.latitude, position.coords.longitude);
10+
},
11+
() => {
12+
map.$server.handleGeolocationError(true);
13+
});
14+
15+
} else { // browser doesn't support geolocation
16+
map.$server.handleGeolocationError(false);
17+
}
18+
}
19+
20+
}

0 commit comments

Comments
 (0)