A Java library for integrating Leaflet maps into JavaFX applications with full Java Platform Module System (JPMS) support.
Project Source Code: https://github.com/makbn/java_leaflet
Leaflet is the leading open-source JavaScript library for mobile-friendly interactive maps. Weighing just about 38 KB of JS, it has all the mapping features most > developers ever need. Leaflet is designed with simplicity, performance and usability in mind. It works efficiently across all major desktop and mobile platforms, can be extended with > lots of plugins, has a beautiful, easy to use and well-documented API and a simple, readable source code that is a joy to contribute to.
This project is fully modularized using the Java Platform Module System (JPMS). The module name is io.github.makbn.jlmap
.
The module requires the following dependencies:
javafx.controls
, javafx.base
, javafx.swing
, javafx.web
, javafx.graphics
jdk.jsobject
org.apache.logging.log4j
, org.apache.logging.log4j.core
com.google.gson
, com.fasterxml.jackson.databind
org.jetbrains.annotations
, lombok
The following packages are exported for public use:
io.github.makbn.jlmap
- Main packageio.github.makbn.jlmap.layer
- Layer managementio.github.makbn.jlmap.layer.leaflet
- Leaflet-specific layer interfacesio.github.makbn.jlmap.listener
- Event listenersio.github.makbn.jlmap.model
- Data modelsio.github.makbn.jlmap.exception
- Custom exceptionsio.github.makbn.jlmap.geojson
- GeoJSON supportAdd the following dependency to your pom.xml
:
<dependency>
<groupId>io.github.makbn</groupId>
<artifactId>jlmap</artifactId>
<version>1.9.5</version>
</dependency>
When running your application, ensure you include the module in your module path:
mvn javafx:run
import io.github.makbn.jlmap.*;
import io.github.makbn.jlmap.model.JLLatLng;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class MapExample extends Application {
@Override
public void start(Stage stage) {
// Create a map view
JLMapView map = JLMapView.builder()
.mapType(JLProperties.MapType.OSM_MAPNIK)
.startCoordinate(JLLatLng.builder()
.lat(51.044)
.lng(114.07)
.build())
.showZoomController(true)
.build();
// Create the scene
AnchorPane root = new AnchorPane(map);
Scene scene = new Scene(root, 800, 600);
stage.setTitle("Java Leaflet Map");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Based on Leaflet JS, you can interact with map in different layers. in this project, you can access different functions with this layer:
map
for direct changes on mapmap.getUiLayer()
for changes on UI layer like markers.map.getVectorLayer()
represents the Vector layer on Leaflet map.map.getControlLayer()
represents the control layer for setting the zoom level.map.getGeoJsonLayer()
represents the GeoJson layer.// Add a marker to the UI layer
map.getUiLayer()
.addMarker(JLLatLng.builder()
.lat(35.63)
.lng(51.45)
.build(), "Tehran", true);
Controlling map’s zoom level and coordinate:
// change the current coordinate
map.setView(JLLatLng.builder()
.lng(10)
.lat(10)
.build());
// map zoom functionalities
map.getControlLayer().setZoom(5);
map.getControlLayer().zoomIn(2);
map.getControlLayer().zoomOut(1);
// Add a circle
map.getVectorLayer()
.addCircle(JLLatLng.builder()
.lat(35.63)
.lng(51.45)
.build(),
30000,
JLOptions.builder()
.color(Color.BLACK)
.build());
you can add a listener for some Objects on the map:
marker.setOnActionListener(new OnJLObjectActionListener<JLMarker>() {
@Override
public void click(JLMarker object, Action action) {
System.out.println("object click listener for marker:" + object);
}
@Override
public void move(JLMarker object, Action action) {
System.out.println("object move listener for marker:" + object);
}
});
# Clean and compile
mvn clean compile
# Run tests
mvn test
# Package
mvn package
# Install to local repository
mvn install
The project uses Maven’s module-aware compilation. The module-info.java
file defines the module structure and dependencies.
If you’re migrating from a non-modular version:
module-info.java
to your projectIf you encounter module path issues, verify:
# Check if the module is properly packaged
jar --describe-module --file target/jlmap-1.9.5.jar
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Mehdi Akbarian Rastaghi (@makbn)
Disclaimer: I’ve implemented this project for one of my academic paper in the area of geo-visualization. So, im not contributing actively! One more thing, I’m not a Javascript developer!
See the GitHub Branches for previous version information.