Git Product home page Git Product logo

java_leaflet's Introduction

Java Leaflet

Java wrapper for Leaflet, JavaScript library for mobile-friendly interactive maps.

  • Current version: v1.9.4
  • Previous version: v1..6.0

Project Source Code: https://github.com/makbn/java_leaflet

Java-Leaflet Test FOSSA Status

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.

Getting start

Since you are working on JavaFX application you know you need to have the JavaFX runtime. Read more about installing JavaFx:

To run the Leaflet example class you need to set the module path as VM options:

--module-path [PATH_TO_JAVA_FX_LIB_FOLDER]
        --add-exports javafx.web/com.sun.javafx.webkit=ALL-UNNAMED
        --add-modules=javafx.graphics,javafx.web

There is an issue with JavaFX v17.X javafx.web engine and OSM tiles, I tried JavaFX v19.0.2.1 and it works fine!

First, you need to initialize an instance of JLMapView:

final JLMapView map = JLMapView
        .builder()
        .mapType(JLProperties.MapType.OSM_MAPNIK)
        .showZoomController(true)
        .startCoordinate(JLLatLng.builder()
            .lat(51.044)
            .lng(114.07)
            .build())
        .build();

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 map
  • map.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.

Map functions

Some map view functionalities are available in map layer like setView or setMapListener as a callback for map events:

  • Change the current coordinate of map center:
map.setView(JLLatLng.builder()
        .lng(10)
        .lat(10)
        .build());
  • Add a listener for map events:
map.setMapListener(new OnJLMapViewListener() {
        @Override
        public void mapLoadedSuccessfully(JLMapView mapView) {
            
        }

        @Override
        public void mapFailed() {
            log.error("map failed!");
        }

        @Override
        public void onAction(Event event) {
            if (event instanceof MoveEvent moveEvent) {
                log.info("move event: " + moveEvent.action() + " c:" + moveEvent.center()
                        + " \t bounds:" + moveEvent.bounds() + "\t z:" + moveEvent.zoomLevel());
            } else if (event instanceof ClickEvent clickEvent) {
                log.info("click event: " + clickEvent.center());
                map.getUiLayer().addPopup(clickEvent.center(), "New Click Event!", JLOptions.builder()
                        .closeButton(false)
                        .autoClose(false).build());
            } else if (event instanceof ZoomEvent zoomEvent) {
                log.info("zoom event: " + zoomEvent.zoomLevel());
            }
        }
}

Read more about map events from OnJLMapViewListener.Action.

UI Layer

Layer for adding/removing markers and popup. you can access UI layer from map.getUiLayer(). As an example:

map.getUiLayer()
    .addMarker(JLLatLng.builder()
                        .lat(35.63)
                        .lng(51.45)
                        .build(), "Tehran", true)
    .setOnActionListener(getListener());

Controlling map's zoom level:

// map zoom functionalities
map.getControlLayer().setZoom(5);
map.getControlLayer().zoomIn(2);
map.getControlLayer().zoomOut(1);

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

Vector layer

Represents the Vector layer for Leaflet map. Poly lines, Polygons, and shapes are available in this layer.

map.getVectorLayer()
        .addCircleMarker(JLLatLng.builder()
            .lat(35.63)
            .lng(40.45)
            .build()
        );

GeoJson layer

Represents the GeoJson layer for Leaflet map and defines methods for adding and managing GeoJSON data layers in a Leaflet map.

JLGeoJsonObject geoJsonObject = map.getGeoJsonLayer()
                        .addFromUrl("https://pkgstore.datahub.io/examples/geojson-tutorial/example/data/db696b3bf628d9a273ca9907adcea5c9/example.geojson");

You can add GeoJson data from three different sources:

  • From a file using map.getGeoJsonLayer().addFromFile([FILE])
  • From a URL using map.getGeoJsonLayer().addFromUrl([URL])
  • From a GeoJson content map.getGeoJsonLayer().addFromContent([CONTENT])

Styling

You can pass JLOptions to each method for changing the default style!

 map.getVectorLayer()
        .addCircle(JLLatLng.builder()
            .lat(35.63)
            .lng(51.45)
            .build(), 30000,
            
            JLOptions.builder()
                .color(Color.BLACK)
                .build()
        );

For the map itself, you can choose between themes available in JLProperties.MapType class. The JLProperties.MapType.OSM_MAPNIK is available to be used without any access key but for the rest of them, you need to define your own map using JLProperties.MapType and passing proper list of key-values containing all the necessary access keys.

JLProperties.MapType myMapType = new JLProperties.MapType("HEREv3.terrainDay",
            Set.of(new JLMapOption.Parameter("apiKey", "<insert apiKey here>")));
    
    JLMapView map = JLMapView
            .builder()
            .mapType(myMapType)
            .showZoomController(true)
            .startCoordinate(JLLatLng.builder()
                    .lat(51.044)
                    .lng(114.07)
                    .build())
            .build();

Read more:

TODO

  • Adding GeoJson Support
  • Adding better support for Map providers
  • Adding SVG support
  • Adding animation support
  • Separating JS and HTML
  • Publishing package on GitHub

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!

License

FOSSA Status

java_leaflet's People

Contributors

dependabot[bot] avatar fossabot avatar makbn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

java_leaflet's Issues

Not able to build the library

Hello,
For some reason I can by no means use the library in a Maven JavaFX Intellij-IDEA project.
Tried both building the jar and importing it as a module, and installing it via maven.
Regarding the last option, the package is not included in maven-central, and installation from jitpack.io produces an error.
With importing the jar directly and after setting the module-path in maven-javafx-plug, I still can't use the functionality provided by jlmap (the project compiles, but in runtime an error is produced):

Exception in Application start method
java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
	at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
	at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
	at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
	at java.base/java.lang.Thread.run(Thread.java:1589)
Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.sg.prism.web.NGWebView (in unnamed module @0x8ce7cb7) cannot access class com.sun.javafx.sg.prism.NGGroup (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.sg.prism to unnamed module @0x8ce7cb7
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1013)
	at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
	at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	at [email protected]/io.github.makbn.jlmap.JLMapView.<init>(JLMapView.java:57)
	at com.example.rf/com.example.rf.HelloApplication.start(HelloApplication.java:21)
	at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
	at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
	at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
	at [email protected]/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
	at [email protected]/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
	at [email protected]/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:316)
	... 1 more
Exception running application com.example.rf.HelloApplication

Configuration options (maven plugin)

<options>
    <option>--add-exports javafx.web/com.sun.javafx.webkit=ALL-UNNAMED</option>
    <option>--add-modules=javafx.graphics,javafx.web</option>
</options>

Dependencies I tried to add

<dependency>
            <groupId>io.github.makbn</groupId>
            <artifactId>jlmap</artifactId>
            <version>1.9.4</version>
</dependency>
        
<dependency>
            <groupId>com.github.makbn</groupId>
            <artifactId>java_leaflet</artifactId>
            <version>1.9.4</version>
</dependency>

Desktop (please complete the following information):
Linux
Intellij-IDEA

What is a recommended way to install the library?

Thank you!

Release build file

Is there any way to find a jar build for this library?

I have several issues with maven pom.xml file because of which i could not build it for myself.

I'd appreciate it if you'd post release jar file.

Is the 1.9.4 JAR lib compatible with JDK 1.8 ?

I am trying to use your JLMAP .jar library as is without recompilation from the JAR files I got from the github page

I use netbeans IDE and compiling with JDK 1.8 and I get the infamous error at runtime :

warning: [options] bootstrap class path not set in conjunction with -source 1.7
D:\TestMapsInJavaFXPanel\src\testmapsinjavafxpanel\NewClass.java:8: error: cannot access JLMapView
import io.github.makbn.jlmap.JLMapView;
bad class file: D:\TestMapsInJavaFXPanel\lib\jlmap-1.9.4-jar-with-dependencies.jar(io/github/makbn/jlmap/JLMapView.class)
class file has wrong version 61.0, should be 52.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
BUILD FAILED (total time: 0 seconds)

It seems your Jar need JDK 1.7 isnt it ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.