java_leaflet

Java Leaflet (JLeaflet)

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

Java-Leaflet Test

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.

Features

Requirements

Module Information

This project is fully modularized using the Java Platform Module System (JPMS). The module name is io.github.makbn.jlmap.

Module Dependencies

The module requires the following dependencies:

Module Exports

The following packages are exported for public use:

Installation

Maven

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.makbn</groupId>
    <artifactId>jlmap</artifactId>
    <version>1.9.5</version>
</dependency>

Module Path

When running your application, ensure you include the module in your module path:

mvn javafx:run

Quick Start

Basic Map Setup

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:

Adding Markers

// 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);

Adding Shapes

// 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);
       }
   });

Building from Source

Prerequisites

Build Commands

# Clean and compile
mvn clean compile

# Run tests
mvn test

# Package
mvn package

# Install to local repository
mvn install

Module-Aware Building

The project uses Maven’s module-aware compilation. The module-info.java file defines the module structure and dependencies.

Migration from Non-Modular Version

If you’re migrating from a non-modular version:

  1. Update Dependencies: Ensure all dependencies are module-compatible
  2. Module Declaration: Add module-info.java to your project
  3. Import Updates: Update any internal JavaFX imports
  4. Build Configuration: Update Maven configuration for module support

Troubleshooting

Common Issues

  1. Module Not Found: Ensure the module is in your module path
  2. Internal API Access: Some JavaFX internal APIs are no longer accessible
  3. Lombok Issues: Ensure annotation processing is properly configured

Module Path Issues

If you encounter module path issues, verify:

# Check if the module is properly packaged
jar --describe-module --file target/jlmap-1.9.5.jar

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Ensure all tests pass
  5. Submit a pull request

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

Author

Mehdi Akbarian Rastaghi (@makbn)

Changelog

Version 1.9.5

TODO

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!

Previous Versions

See the GitHub Branches for previous version information.