-
Notifications
You must be signed in to change notification settings - Fork 10
Getting Started
This guide will help you get up and running with Java Leaflet in minutes, whether you're building a JavaFX desktop application or a Vaadin web application.
- Java 17 or higher
- Maven 3.6+ or Gradle 7+
- For JavaFX: JavaFX SDK 19+
- For Vaadin: Node.js 16+ (for frontend compilation)
Add the JavaFX dependency to your pom.xml
:
<dependencies>
<!-- Java Leaflet for JavaFX -->
<dependency>
<groupId>io.github.makbn</groupId>
<artifactId>jlmap-fx</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
If using Gradle:
dependencies {
implementation 'io.github.makbn:jlmap-fx:2.0.0'
}
For Vaadin applications, add the Vaadin dependency:
<dependencies>
<!-- Java Leaflet for Vaadin -->
<dependency>
<groupId>io.github.makbn</groupId>
<artifactId>jlmap-vaadin</artifactId>
<version>2.0.0</version>
</dependency>
<!-- Vaadin Spring Boot Starter (if using Spring Boot) -->
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
<version>24.8.6</version>
</dependency>
</dependencies>
Create a simple JavaFX application with an interactive map:
package com.example;
import io.github.makbn.jlmap.fx.JLMapView;
import io.github.makbn.jlmap.map.JLMapProvider;
import io.github.makbn.jlmap.model.JLLatLng;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MyFirstMap extends Application {
@Override
public void start(Stage primaryStage) {
// Create the map view
JLMapView mapView = JLMapView.builder()
.jlMapProvider(JLMapProvider.OSM_MAPNIK.build())
.startCoordinate(new JLLatLng(51.505, -0.09)) // London
.showZoomController(true)
.build();
// Create the scene
BorderPane root = new BorderPane(mapView);
Scene scene = new Scene(root, 800, 600);
// Configure the stage
primaryStage.setTitle("My First Java Leaflet Map");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Run the application:
mvn javafx:run
Create a Vaadin view with an interactive map:
package com.example.views;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import io.github.makbn.jlmap.map.JLMapProvider;
import io.github.makbn.jlmap.model.JLLatLng;
import io.github.makbn.jlmap.vaadin.JLMapView;
@Route("")
public class MainView extends VerticalLayout {
public MainView() {
setSizeFull();
setPadding(false);
// Create the map view
JLMapView mapView = JLMapView.builder()
.jlMapProvider(JLMapProvider.OSM_MAPNIK.build())
.startCoordinate(new JLLatLng(48.864716, 2.349014)) // Paris
.showZoomController(true)
.build();
mapView.setSizeFull();
add(mapView);
}
}
Run the application:
mvn spring-boot:run
Then open http://localhost:8080 in your browser.
Java Leaflet supports multiple tile providers:
// OpenStreetMap Mapnik (default)
JLMapProvider.OSM_MAPNIK.build()
// MapTiler (requires API key)
JLMapProvider.MAP_TILER
.parameter(new JLMapOption.Parameter("key", "YOUR_API_KEY"))
.build()
// Water Color
JLMapProvider.WATER_COLOR
.parameter(new JLMapOption.Parameter("key", "YOUR_API_KEY"))
.build()
Add interactive markers to your map:
// After map is loaded
mapView.getUiLayer().addMarker(
new JLLatLng(51.505, -0.09),
"Hello London!",
true // draggable
);
Java Leaflet organizes functionality into four main layers:
// UI Layer - markers, popups, images
mapView.getUiLayer().addMarker(...);
// Vector Layer - polygons, polylines, circles
mapView.getVectorLayer().addCircle(...);
// Control Layer - zoom, pan, bounds
mapView.getControlLayer().setZoom(12);
// GeoJSON Layer - GeoJSON data
mapView.getGeoJsonLayer().addFromFile(...);
Respond to user interactions:
JLMarker marker = mapView.getUiLayer().addMarker(...);
marker.setOnActionListener((source, event) -> {
if (event instanceof ClickEvent clickEvent) {
System.out.println("Marker clicked at: " + clickEvent.center());
}
});
JLPopup popup = mapView.getUiLayer().addPopup(
new JLLatLng(51.5, -0.09),
"<h3>Hello!</h3><p>This is a popup</p>"
);
// Draw a circle
mapView.getVectorLayer().addCircle(
new JLLatLng(51.508, -0.11),
500, // radius in meters
JLOptions.builder()
.color(JLColor.BLUE)
.fillColor(JLColor.BLUE)
.fillOpacity(0.3)
.build()
);
// Draw a polygon
JLLatLng[] vertices = {
new JLLatLng(51.509, -0.08),
new JLLatLng(51.503, -0.06),
new JLLatLng(51.51, -0.047)
};
mapView.getVectorLayer().addPolygon(
new JLLatLng[][][] { new JLLatLng[][] { vertices } },
JLOptions.builder()
.color(JLColor.RED)
.build()
);
// Pan to a location
mapView.getControlLayer().panTo(new JLLatLng(51.505, -0.09));
// Animate to a location
mapView.getControlLayer().flyTo(new JLLatLng(48.8566, 2.3522), 10);
// Zoom
mapView.getControlLayer().zoomIn(2);
mapView.getControlLayer().setZoom(12);
// Fit bounds
JLBounds bounds = JLBounds.builder()
.southWest(new JLLatLng(51.5, -0.1))
.northEast(new JLLatLng(51.52, -0.08))
.build();
mapView.getControlLayer().fitBounds(bounds);
Here's a more complete example showing multiple features:
import io.github.makbn.jlmap.fx.JLMapView;
import io.github.makbn.jlmap.map.JLMapProvider;
import io.github.makbn.jlmap.model.*;
import io.github.makbn.jlmap.listener.event.ClickEvent;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ComprehensiveExample extends Application {
@Override
public void start(Stage primaryStage) {
// Create map
JLMapView mapView = JLMapView.builder()
.jlMapProvider(JLMapProvider.OSM_MAPNIK.build())
.startCoordinate(new JLLatLng(51.505, -0.09))
.showZoomController(true)
.build();
// Add a marker with popup
JLMarker marker = mapView.getUiLayer().addMarker(
new JLLatLng(51.505, -0.09),
"Click me!",
true
);
// Add click listener
marker.setOnActionListener((source, event) -> {
if (event instanceof ClickEvent) {
System.out.println("Marker clicked!");
}
});
// Draw a circle around the marker
mapView.getVectorLayer().addCircle(
new JLLatLng(51.505, -0.09),
500,
JLOptions.builder()
.color(JLColor.BLUE)
.fillOpacity(0.2)
.build()
);
// Create scene
BorderPane root = new BorderPane(mapView);
Scene scene = new Scene(root, 1000, 700);
primaryStage.setTitle("Java Leaflet - Comprehensive Example");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If the map doesn't appear:
- Check JavaFX modules - Ensure JavaFX modules are in your module path
- Wait for map load - The map loads asynchronously. Use event listeners if needed
- Check console - Look for JavaScript errors in the console
If you get frontend compilation errors:
- Install Node.js - Vaadin requires Node.js for frontend compilation
-
Run
mvn clean install
- This will download and install frontend dependencies - Check Vaadin version - Ensure you're using Vaadin 24+
If you encounter module issues:
module com.example.myapp {
requires io.github.makbn.jlmap.fx; // For JavaFX
// OR
requires io.github.makbn.jlmap.vaadin; // For Vaadin
requires javafx.controls; // For JavaFX
requires javafx.web; // For JavaFX
}
Now that you have a basic map running, explore more features:
- Context Menus - Add right-click menus to map objects
- Journey System - Create animated journeys
- API Reference - Explore the complete API
- Examples & Tutorials - Learn from more examples
- Architecture - Understand the internals
- JavaFX Documentation - Learn more about JavaFX
- Vaadin Documentation - Learn more about Vaadin
- Leaflet.js - Original JavaScript library
- GitHub Repository - Source code and examples
Previous: ← Home | Next: Architecture Overview →