Skip to content

Commit ca4a4ea

Browse files
committed
Added GeoJSON and FeatureGroup
Fixes #438
1 parent 811ecd9 commit ca4a4ea

File tree

11 files changed

+472
-39
lines changed

11 files changed

+472
-39
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 4.4.0
2+
* Added support for [GeoJSON](https://leafletjs.com/reference.html#geojson) and [FeatureGroup](https://leafletjs.com/reference.html#featuregroup) #438
3+
14
## 4.3.1
25
* Add "draggable" property to LMarkerOptions #413 (thanks to @ChristianHoesel)
36

vaadin-maps-leaflet-flow-demo/src/main/java/software/xdev/vaadin/maps/leaflet/flow/DemoView.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import software.xdev.vaadin.maps.leaflet.flow.demo.ComplexDemo;
1717
import software.xdev.vaadin.maps.leaflet.flow.demo.EventDemo;
1818
import software.xdev.vaadin.maps.leaflet.flow.demo.FreeingUpResourceBenchmarkDemo;
19+
import software.xdev.vaadin.maps.leaflet.flow.demo.GeoJSONDemo;
1920
import software.xdev.vaadin.maps.leaflet.flow.demo.InitialResizeDemo;
2021
import software.xdev.vaadin.maps.leaflet.flow.demo.MinimalisticDemo;
2122
import software.xdev.vaadin.maps.leaflet.flow.demo.MultiLayerWithPyramidDemo;
@@ -93,6 +94,11 @@ protected void onAttach(final AttachEvent attachEvent)
9394
"using Pyramids ▲ and more...\n"
9495
+ "Showcases a selection of different layers and how they can be displayed on a map"
9596
),
97+
new Example(
98+
GeoJSONDemo.NAV,
99+
"GeoJSON",
100+
"Showcases how GeoJSON can be used"
101+
),
96102
new Example(
97103
FreeingUpResourceBenchmarkDemo.NAV,
98104
"Freeing up resources Benchmark",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package software.xdev.vaadin.maps.leaflet.flow.demo;
2+
3+
import com.vaadin.flow.component.html.Anchor;
4+
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
5+
import com.vaadin.flow.router.Route;
6+
7+
import software.xdev.vaadin.maps.leaflet.MapContainer;
8+
import software.xdev.vaadin.maps.leaflet.base.RawString;
9+
import software.xdev.vaadin.maps.leaflet.basictypes.LLatLng;
10+
import software.xdev.vaadin.maps.leaflet.layer.other.LGeoJSONLayer;
11+
import software.xdev.vaadin.maps.leaflet.layer.other.LGeoJSONLayerOptions;
12+
import software.xdev.vaadin.maps.leaflet.layer.raster.LTileLayer;
13+
import software.xdev.vaadin.maps.leaflet.map.LMap;
14+
import software.xdev.vaadin.maps.leaflet.registry.LComponentManagementRegistry;
15+
import software.xdev.vaadin.maps.leaflet.registry.LDefaultComponentManagementRegistry;
16+
17+
18+
@Route(GeoJSONDemo.NAV)
19+
@SuppressWarnings("checkstyle:MagicNumber")
20+
public class GeoJSONDemo extends VerticalLayout
21+
{
22+
public static final String NAV = "/geojson";
23+
24+
public GeoJSONDemo()
25+
{
26+
// Let the view use 100% of the site
27+
this.setSizeFull();
28+
29+
this.add(new Anchor("https://leafletjs.com/examples/geojson", "Based on this example"));
30+
31+
// Create the registry which is needed so that components can be reused and their methods invoked
32+
// Note: You normally don't need to invoke any methods of the registry and just hand it over to the components
33+
final LComponentManagementRegistry reg = new LDefaultComponentManagementRegistry(this);
34+
35+
// Create and add the MapContainer (which contains the map) to the UI
36+
final MapContainer mapContainer = new MapContainer(reg);
37+
mapContainer.setSizeFull();
38+
this.add(mapContainer);
39+
40+
final LMap map = mapContainer.getlMap();
41+
42+
// Add a (default) TileLayer so that we can see something on the map
43+
map.addLayer(LTileLayer.createDefaultForOpenStreetMapTileServer(reg));
44+
45+
// Set what part of the world should be shown
46+
map.setView(new LLatLng(reg, 39.75621, -104.99404), 17);
47+
48+
// Handle GeoJSON
49+
final String serializedGeoJSON = """
50+
{
51+
"type": "Feature",
52+
"properties": {
53+
"name": "Coors Field",
54+
"amenity": "Baseball Stadium",
55+
"popupContent": "This is where the Rockies play!"
56+
},
57+
"geometry": {
58+
"type": "Point",
59+
"coordinates": [-104.99404, 39.75621]
60+
}
61+
}
62+
""";
63+
new LGeoJSONLayer(
64+
reg,
65+
serializedGeoJSON,
66+
new LGeoJSONLayerOptions()
67+
.withOnEachFeature(new RawString("(feature, layer) => { "
68+
+ " if (feature.properties && feature.properties.popupContent) { "
69+
+ " layer.bindPopup(feature.properties.popupContent); "
70+
+ " }"
71+
+ " }"))
72+
)
73+
.addTo(map);
74+
}
75+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright © 2019 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.vaadin.maps.leaflet.base.has;
17+
18+
import software.xdev.vaadin.maps.leaflet.base.LComponent;
19+
import software.xdev.vaadin.maps.leaflet.layer.vector.LPathOptions;
20+
21+
22+
public interface LHasSetStylePath<S extends LHasSetStylePath<S>> extends LComponent<S>
23+
{
24+
default S setStyle(final LPathOptions<?> options)
25+
{
26+
this.invokeSelf(".setStyle(" + this.componentRegistry().writeOptionsOrEmptyObject(options) + ")");
27+
return this.self();
28+
}
29+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright © 2019 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.vaadin.maps.leaflet.layer;
17+
18+
import java.io.Serializable;
19+
20+
import software.xdev.vaadin.maps.leaflet.base.has.LHasSetZIndex;
21+
import software.xdev.vaadin.maps.leaflet.registry.LComponentManagementRegistry;
22+
23+
24+
/**
25+
* @see <a href="https://leafletjs.com/reference.html#layergroup">Leaflet docs</a>
26+
*/
27+
public class LAbstractLayerGroup<S extends LAbstractLayerGroup<S>> extends LLayer<S>
28+
implements LHasSetZIndex<S>
29+
{
30+
protected LAbstractLayerGroup(
31+
final LComponentManagementRegistry compReg,
32+
final String jsConstructorCallExpression,
33+
final Serializable... parameters)
34+
{
35+
super(compReg, jsConstructorCallExpression, parameters);
36+
}
37+
38+
/**
39+
* @see <a href="https://leafletjs.com/reference.html#layergroup-addlayer">Leaflet docs</a>
40+
*/
41+
public S addLayer(final LLayer<?> layer)
42+
{
43+
this.invokeSelf(".addLayer(" + layer.clientComponentJsAccessor() + ")");
44+
return this.self();
45+
}
46+
47+
/**
48+
* @see <a href="https://leafletjs.com/reference.html#layergroup-removelayer">Leaflet docs</a>
49+
*/
50+
public S removeLayer(final LLayer<?> layer)
51+
{
52+
this.invokeSelf(".removeLayer(" + layer.clientComponentJsAccessor() + ")");
53+
return this.self();
54+
}
55+
56+
/**
57+
* @see <a href="https://leafletjs.com/reference.html#layergroup-clearlayers">Leaflet docs</a>
58+
*/
59+
public S clearLayers()
60+
{
61+
this.invokeSelf(".clearLayers()");
62+
return this.self();
63+
}
64+
}

vaadin-maps-leaflet-flow/src/main/java/software/xdev/vaadin/maps/leaflet/layer/LLayerGroup.java

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
import java.util.stream.Collectors;
1919
import java.util.stream.Stream;
2020

21-
import software.xdev.vaadin.maps.leaflet.base.has.LHasSetZIndex;
2221
import software.xdev.vaadin.maps.leaflet.registry.LComponentManagementRegistry;
2322

2423

2524
/**
2625
* @see <a href="https://leafletjs.com/reference.html#layergroup">Leaflet docs</a>
2726
*/
28-
public class LLayerGroup extends LLayer<LLayerGroup> implements LHasSetZIndex<LLayerGroup>
27+
public class LLayerGroup extends LAbstractLayerGroup<LLayerGroup>
2928
{
3029
public LLayerGroup(
3130
final LComponentManagementRegistry compReg,
@@ -54,31 +53,4 @@ public LLayerGroup(
5453
{
5554
this(compReg, layers, null);
5655
}
57-
58-
/**
59-
* @see <a href="https://leafletjs.com/reference.html#layergroup-addlayer">Leaflet docs</a>
60-
*/
61-
public LLayerGroup addLayer(final LLayer<?> layer)
62-
{
63-
this.invokeSelf(".addLayer(" + layer.clientComponentJsAccessor() + ")");
64-
return this.self();
65-
}
66-
67-
/**
68-
* @see <a href="https://leafletjs.com/reference.html#layergroup-removelayer">Leaflet docs</a>
69-
*/
70-
public LLayerGroup removeLayer(final LLayer<?> layer)
71-
{
72-
this.invokeSelf(".removeLayer(" + layer.clientComponentJsAccessor() + ")");
73-
return this.self();
74-
}
75-
76-
/**
77-
* @see <a href="https://leafletjs.com/reference.html#layergroup-clearlayers">Leaflet docs</a>
78-
*/
79-
public LLayerGroup clearLayers()
80-
{
81-
this.invokeSelf(".clearLayers()");
82-
return this.self();
83-
}
8456
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright © 2019 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.vaadin.maps.leaflet.layer.other;
17+
18+
import java.io.Serializable;
19+
20+
import software.xdev.vaadin.maps.leaflet.base.has.LHasBringTo;
21+
import software.xdev.vaadin.maps.leaflet.base.has.LHasSetStylePath;
22+
import software.xdev.vaadin.maps.leaflet.layer.LAbstractLayerGroup;
23+
import software.xdev.vaadin.maps.leaflet.registry.LComponentManagementRegistry;
24+
25+
26+
/**
27+
* @see <a href="https://leafletjs.com/reference.html#featuregroup">Leaflet docs</a>
28+
*/
29+
public class LAbstractFeatureGroup<S extends LAbstractFeatureGroup<S>> extends LAbstractLayerGroup<S>
30+
implements LHasBringTo<S>, LHasSetStylePath<S>
31+
{
32+
protected LAbstractFeatureGroup(
33+
final LComponentManagementRegistry compReg,
34+
final String jsConstructorCallExpression,
35+
final Serializable... parameters)
36+
{
37+
super(compReg, jsConstructorCallExpression, parameters);
38+
}
39+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright © 2019 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.vaadin.maps.leaflet.layer.other;
17+
18+
import java.util.stream.Collectors;
19+
import java.util.stream.Stream;
20+
21+
import software.xdev.vaadin.maps.leaflet.layer.LAbstractLayerGroup;
22+
import software.xdev.vaadin.maps.leaflet.layer.LLayer;
23+
import software.xdev.vaadin.maps.leaflet.layer.LLayerOptions;
24+
import software.xdev.vaadin.maps.leaflet.registry.LComponentManagementRegistry;
25+
26+
27+
/**
28+
* @see <a href="https://leafletjs.com/reference.html#featuregroup">Leaflet docs</a>
29+
*/
30+
public class LFeatureGroup extends LAbstractLayerGroup<LFeatureGroup>
31+
{
32+
public LFeatureGroup(
33+
final LComponentManagementRegistry compReg,
34+
final LLayer<?>[] layers,
35+
final LLayerOptions<?>[] options)
36+
{
37+
super(
38+
compReg,
39+
"L.featureGroup("
40+
+ (layers != null
41+
? "[" + Stream.of(layers)
42+
.map(LLayer::clientComponentJsAccessor)
43+
.collect(Collectors.joining(",")) + "]"
44+
: "")
45+
+ (options != null
46+
? ", [" + Stream.of(options)
47+
.map(compReg::writeOptionsOrEmptyObject)
48+
.collect(Collectors.joining(",")) + "]"
49+
: "")
50+
+ ")");
51+
}
52+
53+
public LFeatureGroup(
54+
final LComponentManagementRegistry compReg,
55+
final LLayer<?>... layers)
56+
{
57+
this(compReg, layers, null);
58+
}
59+
}

0 commit comments

Comments
 (0)