Skip to content

Commit 9270308

Browse files
committed
Provide workaround for instant map interactions before map is ready
Fixes #305
1 parent 523e4be commit 9270308

File tree

5 files changed

+128
-26
lines changed

5 files changed

+128
-26
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 4.0.2
2+
* Provide a workaround that fixes a problem where certain methods didn't work instantly after the map was created #305
3+
14
## 4.0.1
25
* Added shortcut method ``invokeSelfReturn`` #282
36
* Updated dependencies

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import software.xdev.vaadin.maps.leaflet.flow.demo.ComplexDemo;
1717
import software.xdev.vaadin.maps.leaflet.flow.demo.FreeingUpResourceBenchmarkDemo;
18+
import software.xdev.vaadin.maps.leaflet.flow.demo.InitialResizeDemo;
1819
import software.xdev.vaadin.maps.leaflet.flow.demo.MinimalisticDemo;
1920

2021

@@ -55,6 +56,12 @@ protected void onAttach(final AttachEvent attachEvent)
5556
"Minimalistic",
5657
"Showcasing the simplest form of using the API"
5758
),
59+
new Example(
60+
InitialResizeDemo.NAV,
61+
"Initial resize",
62+
"Some map methods - when called instantly after the map is created - will not work correctly."
63+
+ " This example shows how to workaround this restriction."
64+
),
5865
new Example(
5966
ComplexDemo.NAV,
6067
"Complex",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package software.xdev.vaadin.maps.leaflet.flow.demo;
2+
3+
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
4+
import com.vaadin.flow.router.Route;
5+
6+
import software.xdev.vaadin.maps.leaflet.MapContainer;
7+
import software.xdev.vaadin.maps.leaflet.basictypes.LLatLng;
8+
import software.xdev.vaadin.maps.leaflet.basictypes.LLatLngBounds;
9+
import software.xdev.vaadin.maps.leaflet.layer.raster.LTileLayer;
10+
import software.xdev.vaadin.maps.leaflet.layer.vector.LPolylineOptions;
11+
import software.xdev.vaadin.maps.leaflet.layer.vector.LRectangle;
12+
import software.xdev.vaadin.maps.leaflet.map.LMap;
13+
import software.xdev.vaadin.maps.leaflet.registry.LComponentManagementRegistry;
14+
import software.xdev.vaadin.maps.leaflet.registry.LDefaultComponentManagementRegistry;
15+
16+
17+
@Route(InitialResizeDemo.NAV)
18+
public class InitialResizeDemo extends VerticalLayout
19+
{
20+
public static final String NAV = "/initial-resize";
21+
22+
public InitialResizeDemo()
23+
{
24+
this.setSizeFull();
25+
26+
final LComponentManagementRegistry reg = new LDefaultComponentManagementRegistry(this);
27+
28+
// We want to show this area initially
29+
final LLatLngBounds bounds = new LLatLngBounds(
30+
reg,
31+
new LLatLng(reg, 49, 12),
32+
new LLatLng(reg, 50, 11)
33+
);
34+
35+
final MapContainer mapContainer = new MapContainer(reg, map -> {
36+
// This needs to be done after the map was initially resized
37+
// otherwise the view is calculated incorrectly
38+
map.fitBounds(bounds);
39+
});
40+
mapContainer.setSizeFull();
41+
this.add(mapContainer);
42+
43+
final LMap map = mapContainer.getlMap();
44+
map.addLayer(LTileLayer.createDefaultForOpenStreetMapTileServer(reg));
45+
46+
// Still required as the map refuses to work otherwise
47+
map.setView(new LLatLng(reg, 0, 0), 1);
48+
49+
// Create rectangle for reference
50+
new LRectangle(reg, bounds, new LPolylineOptions())
51+
.addTo(map);
52+
}
53+
}

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package software.xdev.vaadin.maps.leaflet;
1717

18+
import java.util.function.Consumer;
19+
20+
import com.vaadin.flow.component.ClientCallable;
1821
import com.vaadin.flow.component.Composite;
1922
import com.vaadin.flow.component.HasComponents;
2023
import com.vaadin.flow.component.HasSize;
@@ -26,6 +29,7 @@
2629
import com.vaadin.flow.component.dependency.NpmPackage;
2730
import com.vaadin.flow.component.html.Div;
2831

32+
import software.xdev.vaadin.maps.leaflet.basictypes.LLatLngBounds;
2933
import software.xdev.vaadin.maps.leaflet.map.LMap;
3034
import software.xdev.vaadin.maps.leaflet.registry.LComponentManagementRegistry;
3135

@@ -41,11 +45,64 @@ public class MapContainer extends Composite<Div> implements HasSize, HasStyle, H
4145
{
4246
private final LMap lMap;
4347

48+
private Consumer<LMap> afterInitialResize;
49+
4450
public MapContainer(final LComponentManagementRegistry reg)
4551
{
52+
this(reg, null);
53+
}
54+
55+
/**
56+
* @param afterInitialResize This is called after the map was initially resized/is ready.
57+
* <p/>
58+
* This is ONLY required when calling certain methods like e.g.
59+
* {@link LMap#fitBounds(LLatLngBounds)} instantly after the map is created.
60+
* <p/>
61+
* For performance reasons it's highly recommended to only use this when required.
62+
*/
63+
public MapContainer(
64+
final LComponentManagementRegistry reg,
65+
final Consumer<LMap> afterInitialResize)
66+
{
67+
this.afterInitialResize = afterInitialResize;
4668
this.getContent().setSizeFull();
69+
this.fixZIndex();
70+
71+
this.lMap = new LMap(
72+
reg,
73+
this.getContent());
74+
this.fixInitialSizeAfterCreation();
75+
}
76+
77+
protected String ensureId()
78+
{
79+
// Id is auto assigned by LMap so this will never throw
80+
return this.getContent().getId().orElseThrow();
81+
}
82+
83+
protected void fixZIndex()
84+
{
85+
LMap.fixZIndex(this.getContent());
86+
}
87+
88+
protected void fixInitialSizeAfterCreation()
89+
{
90+
this.lMap.fixInvalidSizeAfterCreation(this.afterInitialResize != null
91+
? "document.getElementById('" + this.ensureId() + "').$server.onInitialResize();"
92+
: null);
93+
}
94+
95+
@ClientCallable
96+
public void onInitialResize()
97+
{
98+
if(this.afterInitialResize == null)
99+
{
100+
return;
101+
}
47102

48-
this.lMap = new LMap(reg, this.getContent());
103+
this.afterInitialResize.accept(this.getlMap());
104+
// Free up
105+
this.afterInitialResize = null;
49106
}
50107

51108
public LMap getlMap()

vaadin-maps-leaflet-flow/src/main/java/software/xdev/vaadin/maps/leaflet/map/LMap.java

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public LMap(
5050
final LMapOptions options)
5151
{
5252
super(compReg, "L.map($0" + compReg.writeOptionsOptionalParameter(options) + ")", id);
53-
// https://stackoverflow.com/q/53879753
54-
this.fixInvalidSizeAfterCreation();
5553
}
5654

5755
public LMap(
@@ -65,23 +63,6 @@ public LMap(
6563
final LComponentManagementRegistry compReg,
6664
final Div bindDiv,
6765
final LMapOptions options)
68-
{
69-
this(compReg, bindDiv, options, true);
70-
}
71-
72-
public LMap(
73-
final LComponentManagementRegistry compReg,
74-
final Div bindDiv,
75-
final boolean fixDivZIndex)
76-
{
77-
this(compReg, bindDiv, null, fixDivZIndex);
78-
}
79-
80-
public LMap(
81-
final LComponentManagementRegistry compReg,
82-
final Div bindDiv,
83-
final LMapOptions options,
84-
final boolean fixDivZIndex)
8566
{
8667
this(
8768
compReg,
@@ -91,10 +72,6 @@ public LMap(
9172
return dynamicId;
9273
}),
9374
options);
94-
if(fixDivZIndex)
95-
{
96-
fixZIndex(bindDiv);
97-
}
9875
}
9976

10077
/**
@@ -105,10 +82,15 @@ public static void fixZIndex(final Div div)
10582
div.getStyle().set("z-index", "1");
10683
}
10784

108-
protected void fixInvalidSizeAfterCreation()
85+
public void fixInvalidSizeAfterCreation(final String callback)
10986
{
87+
// https://stackoverflow.com/q/53879753
88+
// This should no longer be required starting in Leaflet v2 https://github.com/Leaflet/Leaflet/pull/8612
11089
this.componentRegistry().execJs("let tempMap = " + this.clientComponentJsAccessor() + "; "
111-
+ "setTimeout(function () { tempMap.invalidateSize(false); }, 100)");
90+
+ "setTimeout(function () { "
91+
+ " tempMap.invalidateSize(false); "
92+
+ (callback != null ? callback : "")
93+
+ " }, 100)");
11294
}
11395

11496
// endregion

0 commit comments

Comments
 (0)