java.lang.Object
com.example.cab302project.LeafletLoader
Utility for loading Leaflet.js into a JavaFX WebEngine.
The problem with using a CDN link for Leaflet in JavaFX's WebView:
- On macOS, WebKit's sandbox often blocks external network requests,
so the CDN script never loads and the map is blank.
- On Windows it works but adds startup latency.
Solution: bundle leaflet.min.js as a classpath resource and inject it
directly via engine.executeScript() once the HTML page has loaded.
The local tile proxy (TileProxyServer) is used for tiles on all platforms.
The Java side also passes the WebView's measured width and height to the
JavaScript on every resize. The HTML uses those explicit pixel values to
size the map div rather than relying on percentage-based CSS, which avoids
Leaflet measuring the container at the wrong moment and rendering tiles
in misaligned positions.
-
Method Summary
-
Method Details
-
loadMap
Loads the given HTML map file into the WebView and, once loaded: 1. Injects the bundled Leaflet CSS and JS from classpath resources. 2. Calls initMap(tileProxyPort, width, height) in the HTML page using the WebView's measured pixel dimensions, so Leaflet sees the correct size. 3. Registers width and height listeners so the map updates whenever the WebView is resized by JavaFX layout. 4. Invokes the provided onReady callback so the caller can push data.- Parameters:
mapView- the WebView from a controller (size is read off this node)htmlFile- the filename of the HTML resource (e.g. "hotspots-map.html")onReady- callback invoked on the FX thread after initMap() succeeds
-