Tracks
Since version 1.4.0 it is possible to display coordinate-lines – or tracks – on the map. The following screenshot shows the demo application with two tracks in different colors and with a different line-thickness:
These objects are created in the demo application by reading two files in csv format containing the coordinates:
// load two coordinate lines trackMagenta = loadCoordinateLine(getClass().getResource("/M1.csv")).orElse(new CoordinateLine ()).setColor(Color.MAGENTA); trackCyan = loadCoordinateLine(getClass().getResource("/M2.csv")).orElse(new CoordinateLine ()).setColor(Color.CYAN).setWidth(7);
The method that is called reads the csv file, splits the lines in two, creates Coordinate Objects and adds them to a list to finally pass this list to the CoordinateLine
constructor.
private Optional<CoordinateLine> loadCoordinateLine(URL url) { try ( Stream<String> lines = new BufferedReader( new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)).lines() ) { return Optional.ofNullable(new CoordinateLine( lines.map(line -> line.split(";")).filter(array -> array.length == 2) .map(values -> new Coordinate(Double.valueOf(values[0]), Double.valueOf(values[1]))) .collect(Collectors.toList()))); } catch (IOException | NumberFormatException e) { logger.error("load {}", url, e); } return Optional.empty(); }
CoordinateLine objects have a visibility property that in the demo application is bound to the checkboxes. On change of these properties the demo application zooms the extent so that both tracks are in view.
Polygons
Since version 2.2.0 (1.21.0 for the JDK8 version), CoordinateLines can be set to be closed, so that they are polygons with a fill color. After the draw a polygon checkbox is checked in the left pane, the demo app uses the clicks in the map to build a filled polygon:
When trying this out, after the first click you will see no change, after the second a line and only after the third click a polygon.
The code in the handler for the MAP_CLICKED event just gets the coordinates from the previous polygon, adds the new one and then recreates the polygon (Coordinate lines cannot be modified once their are created in the map:
/** * shows a new polygon with the coordinate from the added. * * @param event * event with coordinates */ private void handlePolygonClick(MapViewEvent event) { final List<Coordinate> coordinates = new ArrayList<>(); if (polygonLine != null) { polygonLine.getCoordinateStream().forEach(coordinates::add); mapView.removeCoordinateLine(polygonLine); polygonLine = null; } coordinates.add(event.getCoordinate()); polygonLine = new CoordinateLine(coordinates) .setColor(Color.DODGERBLUE) .setFillColor(Color.web("lawngreen", 0.4)) .setClosed(true); mapView.addCoordinateLine(polygonLine); polygonLine.setVisible(true); }
Like with markers and labels, coordinate lines and polygons are independent from the map style, so you change change the style while displaying them:
Circles
since version 1.33.0/2.15.0 it is now possible add circles to the map as well. The API is very similar to the Coordinate line API.
Miscellaneous options
In this section on the left side of the demo application there are the following options:
animationDuration. This is a value in milliseconds that is used for animation when the map’s center or zoom is modified using the setCenter(c)
, setZoom(z)
or setExtend(e)
calls. It does not change the animation that is used by the OpenLayers map component when the controls in the map are used for zooming. Setting the value to zero switches the animation off.
Bing Maps API Key. Enter your API key for Bing Maps and then the map style can be changed to one of the Bing Map styles.
constrain to Germany: when selected, the map cannot be panned or zoomed to show areas outside of the rectangle that is defined by the points that are furthest north, south, east and west of Germany
…continued on the next page
Hello, thank you for your wonderful work, first! I was having trouble with running this extension in IntelliJ. I am stuck after cloning the file. I wonder where I can run the code required to open the application. Do I copy the code and run it to the terminal? (I’m currently using a macbook). Or can I run the code in IntelliJ, if so, where do I put this code?
I am not maintaining this project any longer, but you can find this information in the readme file in the repository.
Hi. can any way openstreetmap use offline in JavaFx Desktop app? Do you can do example for this?
No, there is only the support to cache the data so that the tiles that were downloaded will be reused
Hello..
I did tests with the demo and it works very well, now I try to make a modification to get the Here Maps tiles and from what I see this is done in the methods
private WMSParam wmsParam = new WMSParam () and private XYZParam xyzParams = new XYZParam () inside Conttoller.java
but I can’t get them, it keeps showing me the OSM ones, can you guide me on how to do it ???
I don’t know if WMS or XYZ params are the right ones for Here maps, but besides setting the parameters, you need to switch the map style. The demo application has this in these lines: https://github.com/sothawo/mapjfx-demo/blob/main/src/main/java/com/sothawo/mapjfxdemo/Controller.java#L362-L389
Hallo, many thanks for the work and time you spend for this project and make it open source!
It is exactly what I am looking for to integrate it in my project. But I have an issue with the markers. Once I have set them, they are a kind of hovering over the map and are not fixed to their coordinate. So when I scroll out, the marker is maybe in Africa, only scrolling into the maximum moves it to the right position. Maybe you have an solution for it.
Thank you in advanced.
I found the problem in scaling my display as a result of my high resolution monitor and that this and that the problem is already being discussed.
Sorry for my hasty question
Hi , many thanks for this excellent project.
I have a Question : can i change the url to openMapTile local server this is the format : http://localhost:8080/styles/osm-bright/{z}/{x}/{y}.png
Set up an XYZ source for the map, see https://github.com/sothawo/mapjfx-demo/blob/main/src/main/java/com/sothawo/mapjfxdemo/Controller.java#L255-L258 and https://github.com/sothawo/mapjfx-demo/blob/main/src/main/java/com/sothawo/mapjfxdemo/Controller.java#L378
Thanks it worked 🙂
Hi, awesome project!
I just downloaded the demo and it works perfectly fine. I then tried to integrate this map into my fx application without using FXML – Just getting a plain, basic instance of the map view with something like:
MapView mv = new MapView();
mv.initialize();
StackPane root = new StackPane();
root.getChildren().add(mv);
Unfortunately this and all other things I have tried ended up with just a grey screen and the zoom controls visible.
I then tried to copy the demo code with FXML into my project and excuting / changing it, which worked fine and as expected.
How can I use this without FXML? I have never used FXML before and am slightly struggeling with it.
If it is relevant: Java 11.0.4, Eclipse, Windows 10.
Cheers and once again, awesome project!
the mapjfx project itslef has a small test application which I use when devloping (https://github.com/sothawo/mapjfx/blob/master/src/main/java/com/sothawo/mapjfx/app/TestApp.java#L286-L299). This is s pure code setup, no fxml
I should log in before answering
make sure that your MapView has a center set and an extent / a zoom level (https://github.com/sothawo/mapjfx/blob/master/src/main/java/com/sothawo/mapjfx/app/TestApp.java#L255-L280)
Setting a center did the trick – As your comment in the code said: “Map is only displayed, when an initial coordinate is set”
Thanks for the super quick reply! 🙂
Hi, i am new on JavaFX and mapjfx. I was testing your lib by doing my own test application. I was succedeed in show a map, set center, zoom, change MapType and also include a marker with a label. But I have a little issue with marker, I added just one marker to test and when I change the zoom, the marker just desapears… Am I doing something wrong?? Is that a bug?? There is a kind of “UpdateMapView” method that I can call to refresh the mapView? Thankyou… you did a greatjob…
is there a kind of…*** Sorry bad english!
When you create a Marker object you need to make sure, to keep a reference to it in your java code, for example by having it as a property in some class or by putting it into a list of Markers. If you don’t, then the Java collector will remove the Marker object from the JVM, and then it will also be removed from the map.
Thank You, it works now. As you indicated, I create a global List, add all my markers on this list and call “mapView.addMarker(pt)” from this list. Now they don’t disappears anymore. Thank you again!!!
Hi
I was trying to look into the mapjfx cache issues.
I tried to build a previous cached demo version, 1.7.2, from the maven repo.
But maven keeps failing on a missing configuration (source) file.
Can you give some directions?
Cheers,
Antonio
what is the exact error?
Is it possible to use this library in an offline application? We currently have a swing based application which renders a map and various layers using OpenMap and all our data is stored locally. Plus OpenMap supports a bunch of mapping standards like VMAP and DTED. Does your library support something like those or would we have to write something?
as written here (https://www.sothawo.com/projects/mapjfx-demo/6/) there is an offline caching mode which caches the tiles during an online session, so they are available later even when offline.
**But**: this will not work from probably Java 11 on, as I need to use some reflection to get this working which will not be allowed in Java versions to come
Very interesting and useful code!!!
Can you explain how can i use “stamen watercolor” type map ?
Thank you in advanced.
in the demo app by selecting ‘map style’ on the left and then the ‘Stamen Watercolor” radio button.
But as this is a OpenStreetmap map as wellm you might run into the problems with the mpa tiles not properly loading; see the blog posts and jira issues for more details.
Hello,
The map is not displaying for me. All I can see are zoom buttons, but none of the map layers.
I’m running Java 8 on Windows 10 with 1.15. Any ideas?
Hello Chris,
which version of Java? JDK or JRE? At the moment when running this on 1.8.0_162 on OSX the loading of the map tiles was quite slow, but now it’s working here.
There is an open issue (https://github.com/sothawo/mapjfx/issues/29) where I am investigating a similar problem, but I have no solution for that. Is this only happening for the OSM map? The WMS Gis Landsat server seems not to be available anymore, but can you try the WMS world food programme map and zoom out to level 3? This should give you a grey map with some darker grey boundaries especially in Africa and the middle east.
You can sign up for a Bing Maps API key, that does not cost anything for up to 125.000 calls a year. At least that what give you the possibility to check if this map type is working.
P.J.,
I’m running 1.8.0_161. I’ve tried all the map layers and none of them display for me.
I also tried switching to Java 10, but still didn’t work. Has this been solved yet?
Thanks
Also, I tried both JRE and JDK.
Here is an image of what I’m looking at: https://ibb.co/dTgzHS
Hi Chris, thanks for the info.
Alas as far as I found out, this is a problem which is caused by something in the underlying WebKit implementation (see the analysis in https://github.com/sothawo/mapjfx/issues/29 and my test with a plain example from the OpenLayers webiste https://www.sothawo.com/2018/02/mapjfx-problems/).
I just tried with the new 1.8_172 and there with the mapjfx-demo app I have only parts loading.
So at the moment I do not know how to fix this.
Np, thank you for looking in to this.
Hello,
is it posible to use map in JFXPanel of Swing application, i see only zoom in/out after calling initialize(). But no map?
I have set mapType to OSM.
Thanks for answer
Does the mapjfx-demo application work on your system? I am just asking to make sure that there is no problem in the combination of OS, Java version with mapjfx component. Please check this.
Hello,
is it possible to add markers and labels at runtime?
I have tried it, but only the markers defined in the constructor are visible.
Greetings,
Tursic
Sure it’s possible. You have to take care that you keep a reference of the created markers, otherwise they are immediately garbage-collected.
If for example you add the following property to the Controller class:
private Map<String, Marker> markersCreatedOnClick = new HashMap<>();
and add this code in the event handler initializer:
mapView.addEventHandler(MapViewEvent.MAP_CLICKED, event -> {
final Marker marker = Marker.createProvided(Marker.Provided.ORANGE)
.setPosition(event.getCoordinate())
.setVisible(true);
mapView.addMarker(marker);
markersCreatedOnClick.put(marker.getId(), marker);
});
mapView.addEventHandler(MarkerEvent.MARKER_CLICKED, event -> {
event.consume();
Marker marker = markersCreatedOnClick.remove(event.getMarker().getId());
if (null != marker) {
mapView.removeMarker(marker);
}
});
then with every click you will add a new marker and clicking on the marker will remove it again.
Thank you very much.
On my development-PC it works now fine (except from the missing map when started in the IDE, but that would be ok), but unfortunately on every other PC I tested, the map does not appear at all.
Could it be the WMSParam-Urls?
It looks like the map is loaded somehow, because the markers appear and I can move and zoom, only the map itself is missing, there is only a grey background.
Now i found out:
The problem appears, when ‘jdk’ is used. When using ‘jre’, it works.
Hello everyone I had the same issue when the map was not showing in the demo app even though cache was disabled.
The fix was that I adjusted the Java version to instead using an old JRE 1.8.0_66 to use a new JDK 1.8.0_121.
Then the map was shown correctly.
For some reason the logger.trace is not shown im my debug-output.
And when I step into ‘mapView.initialize()’, only the Logger.finer – Method is called.
I can run the demo, can see the markers,etc, but the map does not appear.
Can anyone help me?
Hello,
on which System? Which map type? Do you run software like vpn etc?
The demo has enabled the offline cache for the embedded browser, can you change the line 271 in Controller.java so that the cache is not used, either by putting a comment in front or by changing it to
offlineCache.setActive(false);
?When starting the program from the console/terminal, do you see some errors in the output?
greetings
Peter
Hello,
thank you for the answer.
The system is Windows7.
Now,I have tested the demo on another PC, and here I can see the OSM-Map, when starting from console.
But in Netbeans-IDE, the map still does not appear.
Setting offlineCache to false takes no effect.
Greetings,
Tursic
any output in the console that shows some errors?
No errors, just this:
10:34:44.789 [JavaFX Application Thread] INFO com.sothawo.mapjfx.demo.DemoApp – starting DemoApp
10:34:44.791 [JavaFX Application Thread] DEBUG com.sothawo.mapjfx.demo.DemoApp – loading fxml file /fxml/DemoApp.fxml
10:34:45.039 [JavaFX Application Thread] DEBUG com.sothawo.mapjfx.demo.Controller – map type toggled to RadioButton[id=radioMsOSM, styleClass=radio-button]’OSM’
10:34:45.532 [JavaFX Application Thread] DEBUG com.sothawo.mapjfx.demo.Controller – initialization finished
10:34:45.858 [JavaFX Application Thread] DEBUG com.sothawo.mapjfx.demo.DemoApp – application start method finished.
10:34:46.081 [JavaFX Application Thread] DEBUG com.sothawo.mapjfx.demo.Controller – setting center and enabling controls…
On the other PC (with Windows 10) the map still doesn’t appear, even if started from console.
Maybe a performance-problem? The rendering of the map is very slow anyway.
strange. When I start the program on Windows the from a console with the command
mvn clean package && cd target/mapjfx-demo && ./bin/mapjfx-demo)
I get the following output:04:55:53.545 INFO [JavaFX Application Thread] com.sothawo.mapjfx.demo.DemoApp - starting DemoApp
04:55:53.545 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.demo.DemoApp - loading fxml file /fxml/DemoApp.fxml
04:55:53.792 TRACE [JavaFX Application Thread] com.sothawo.mapjfx.demo.Controller - begin initialize
04:55:53.797 TRACE [JavaFX Application Thread] com.sothawo.mapjfx.demo.Controller - location buttons done
04:55:53.821 TRACE [JavaFX Application Thread] com.sothawo.mapjfx.demo.Controller - options and labels done
04:55:53.823 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.demo.Controller - map type toggled to RadioButton[id=radioMsOSM, styleClass=radio-button]'OSM'
04:55:53.826 TRACE [JavaFX Application Thread] com.sothawo.mapjfx.demo.Controller - map handlers initialized
04:55:53.869 TRACE [JavaFX Application Thread] com.sothawo.mapjfx.demo.Controller - marker checks done
04:55:53.879 TRACE [JavaFX Application Thread] com.sothawo.mapjfx.demo.Controller - tracks loaded
04:55:53.879 TRACE [JavaFX Application Thread] com.sothawo.mapjfx.demo.Controller - tracks checks done
04:55:53.880 TRACE [JavaFX Application Thread] com.sothawo.mapjfx.demo.Controller - start map initialization
04:55:53.880 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - initializing...
04:55:53.881 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - loading from jar:file:/C:/Users/peter/Entwicklung/mapjfx-demo/target/mapjfx-demo/lib/mapjfx-1.12.2.jar!/mapview.html
04:55:53.885 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - loading from jar:file:/C:/Users/peter/Entwicklung/mapjfx-demo/target/mapjfx-demo/lib/mapjfx-1.12.2.jar!/ol/4.0.1/ol.css
04:55:53.886 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - loading from jar:file:/C:/Users/peter/Entwicklung/mapjfx-demo/target/mapjfx-demo/lib/mapjfx-1.12.2.jar!/polyfill.js
04:55:53.886 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - loading from jar:file:/C:/Users/peter/Entwicklung/mapjfx-demo/target/mapjfx-demo/lib/mapjfx-1.12.2.jar!/ol/4.0.1/ol.js
04:55:53.908 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - loading from jar:file:/C:/Users/peter/Entwicklung/mapjfx-demo/target/mapjfx-demo/lib/mapjfx-1.12.2.jar!/mapview.css
04:55:53.909 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - loading custom mapview css from jar:file:/C:/Users/peter/Entwicklung/mapjfx-demo/target/mapjfx-demo/lib/mapjfx-demo-1.12.1.jar!/custom_mapview.css
04:55:53.910 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - loading from jar:file:/C:/Users/peter/Entwicklung/mapjfx-demo/target/mapjfx-demo/lib/mapjfx-1.12.2.jar!/functions.js
04:55:53.911 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - loading from jar:file:/C:/Users/peter/Entwicklung/mapjfx-demo/target/mapjfx-demo/lib/mapjfx-1.12.2.jar!/MapObject.js
04:55:53.911 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - loading from jar:file:/C:/Users/peter/Entwicklung/mapjfx-demo/target/mapjfx-demo/lib/mapjfx-1.12.2.jar!/WMSParams.js
04:55:53.911 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - loading from jar:file:/C:/Users/peter/Entwicklung/mapjfx-demo/target/mapjfx-demo/lib/mapjfx-1.12.2.jar!/coordinateline.js
04:55:53.911 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - loading from jar:file:/C:/Users/peter/Entwicklung/mapjfx-demo/target/mapjfx-demo/lib/mapjfx-1.12.2.jar!/mapview.js
04:55:53.985 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - WebView created
04:55:53.986 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - Java Version: 1.8.0_121-b13
04:55:53.986 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - JavaFX Version: 8.0.121-b13
04:55:53.987 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - OS: Windows 10, 10.0, amd64
04:55:53.987 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/602.1 (KHTML, like Gecko) JavaFX/8.0 Safari/602.1
04:55:53.988 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - load html into WebEngine
04:55:54.007 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - WebEngine loader state READY -> SCHEDULED
04:55:54.008 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - WebEngine loader state SCHEDULED -> RUNNING
04:55:54.010 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.demo.Controller - initialization finished
04:55:54.010 TRACE [JavaFX Application Thread] com.sothawo.mapjfx.demo.DemoApp - stage loaded
04:55:54.059 TRACE [JavaFX Application Thread] com.sothawo.mapjfx.demo.DemoApp - scene created
04:55:54.060 TRACE [JavaFX Application Thread] com.sothawo.mapjfx.demo.DemoApp - showing scene
04:55:54.500 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.demo.DemoApp - application start method finished.
04:55:54.732 DEBUG [JavaFX Application Thread] com.sothawo.mapjfx.MapView - WebEngine loader state RUNNING -> SUCCEEDED
04:55:54.735 TRACE [JavaFX Application Thread] com.sothawo.mapjfx.demo.Controller - map intialized
That’s a lot more before the Map is shown. Can you use a debugger to step through the
Controller.initialize()
method?Pingback: mapjfx 1.7.0 with the ability for caching map data | sothawo
You are just awesome! Thank you!
comment for the first user of javafx scene builder
user can download scene builder,
http://gluonhq.com/open-source/scene-builder/
and who can’t open the DemoApp.fxml
check the under document, pp. 35 Import JAR
https://docs.oracle.com/javase/8/scene-builder-2/JSBID.pdf
After importing mapjfx-1.5.0.jar file in scene builder, user can open this DemoApp.fxml
hope It helps for someone.
Thank you agian
Peter-Josef Meisch 🙂
Download link for the last Oracle SceneBuilder: http://www.oracle.com/technetwork/java/javafxscenebuilder-1x-archive-2199384.html
Don’t know why this is, but to have the MapView available in the Scene Builder, I had to import mapjfx-demo (1.9.0) …
1.9.0 is pretty old (February 2017).

I just downloaded SceneBuilder 8.5.0 from https://gluonhq.com/products/scene-builder/ (the Java 8 version), added the actual Java8 version of mapjfx (1.24.3), and there it works
Do the same thing as in the tutorial below just in part where he searches for GMapsFX instead write mapjfx-demo. And you will import MapView.
https://rterp.wordpress.com/2016/10/03/how-to-import-the-gmapsfx-component-into-scenebuilder/