Skip to content

Commit 4fa064d

Browse files
paodbjavier-godoy
authored andcommitted
feat(demo): add new demo for current user's location
1 parent 2616038 commit 4fa064d

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ public class GooglemapsDemoView extends TabbedDemo {
3232
private static final String GMAPS_DEMO = "Google Maps Demo";
3333
private static final String GMAPS_SOURCE =
3434
"https://github.com/FlowingCode/GoogleMapsAddon/blob/master/src/test/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMapsDemo.java";
35+
private static final String GEOLOCATION_DEMO = "Geolocation Demo";
36+
private static final String GEOLOCATION_SOURCE =
37+
"https://github.com/FlowingCode/GoogleMapsAddon/blob/master/src/test/java/com/flowingcode/vaadin/addons/googlemaps/GeolocationDemo.java";
3538

3639
public GooglemapsDemoView() {
3740
addDemo(new GoogleMapsDemo(), GMAPS_DEMO, GMAPS_SOURCE);
41+
addDemo(new GeolocationDemo(), GEOLOCATION_DEMO, GEOLOCATION_SOURCE);
3842
setSizeFull();
3943
}
4044
}

0 commit comments

Comments
 (0)