|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Google Maps Addon |
| 4 | + * %% |
| 5 | + * Copyright (C) 2020 - 2021 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 com.flowingcode.vaadin.addons.googlemaps.GoogleMap.MapType; |
| 23 | +import com.vaadin.flow.component.button.Button; |
| 24 | +import com.vaadin.flow.component.html.H2; |
| 25 | +import com.vaadin.flow.component.notification.Notification; |
| 26 | +import com.vaadin.flow.component.orderedlayout.FlexLayout; |
| 27 | +import com.vaadin.flow.component.orderedlayout.FlexLayout.FlexWrap; |
| 28 | +import com.vaadin.flow.component.orderedlayout.VerticalLayout; |
| 29 | + |
| 30 | +@SuppressWarnings("serial") |
| 31 | +public class GeolocationDemo extends VerticalLayout { |
| 32 | + |
| 33 | + public GeolocationDemo() { |
| 34 | + this.setSizeFull(); |
| 35 | + String apiKey = System.getProperty("google.maps.api"); |
| 36 | + if (apiKey == null) { |
| 37 | + add( |
| 38 | + new H2( |
| 39 | + "Api key is needded to run the demo, pass it using the following system property: '-Dgoogle.maps.api=<your-api-key>'")); |
| 40 | + } else { |
| 41 | + GoogleMap gmaps = new GoogleMap(apiKey, null, null); |
| 42 | + gmaps.setMapType(MapType.ROADMAP); |
| 43 | + gmaps.setSizeFull(); |
| 44 | + |
| 45 | + FlexLayout layout = new FlexLayout(); |
| 46 | + layout.setFlexWrap(FlexWrap.WRAP); |
| 47 | + layout.add(new Button("Go to current location", e -> gmaps.goToCurrentLocation())); |
| 48 | + add(gmaps, layout); |
| 49 | + |
| 50 | + gmaps.addCurrentLocationEventListener( |
| 51 | + e -> |
| 52 | + gmaps.addMarker( |
| 53 | + new GoogleMapMarker("You are here!", gmaps.getCenter(), false, Markers.GREEN))); |
| 54 | + |
| 55 | + gmaps.addGeolocationErrorEventListener( |
| 56 | + e -> |
| 57 | + Notification.show( |
| 58 | + e.isBrowserHasGeolocationSupport() |
| 59 | + ? "The geolocation service failed on retrieving your location." |
| 60 | + : "Your browser doesn't support geolocation.")); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments