|
20 | 20 | package com.flowingcode.vaadin.addons.googlemaps; |
21 | 21 |
|
22 | 22 | import com.vaadin.flow.component.ClickEvent; |
| 23 | +import com.vaadin.flow.component.ClientCallable; |
23 | 24 | import com.vaadin.flow.component.Component; |
| 25 | +import com.vaadin.flow.component.ComponentEvent; |
24 | 26 | import com.vaadin.flow.component.ComponentEventListener; |
| 27 | +import com.vaadin.flow.component.ComponentUtil; |
25 | 28 | import com.vaadin.flow.component.EventData; |
26 | 29 | import com.vaadin.flow.component.HasSize; |
27 | 30 | import com.vaadin.flow.component.Synchronize; |
|
39 | 42 | @Tag("google-map") |
40 | 43 | @JsModule("@flowingcode/google-map/google-map.js") |
41 | 44 | @NpmPackage(value = "@flowingcode/google-map", version = "3.0.2") |
| 45 | +@JsModule("./googlemaps/geolocation.js") |
42 | 46 | public class GoogleMap extends Component implements HasSize { |
43 | 47 |
|
44 | 48 | /** Base map types supported by Google Maps. */ |
@@ -293,4 +297,79 @@ public Registration addRightClickListener(ComponentEventListener<GoogleMapClickE |
293 | 297 | .addEventData("event.detail.latLng"); |
294 | 298 | return registration::remove; |
295 | 299 | } |
| 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 | + } |
296 | 375 | } |
0 commit comments