I’ve been trying to create a Java desktop app using Java, JavaScript and JavaFX and one part of the app should be able to display a location on the map using the location of the venue taken from the database (street + city, no postal code) and once I run the app it seems to show the location correctly for a second or two and then it completely disapears like it’s shown in the images below.
Before
After
I used Maps JavaScript API and Geocoding API. I’m trying to get the app to consistently show the location without it disappearing without it running into an issue. This is the method that I’m currently using to display the map:
public void showMap(Venue venue) {
WebEngine webEngine = mapView.getEngine();
Location location = venue.getLocation();
String address = location.toString();
String encodedAddress = URLEncoder.encode(address, StandardCharsets.UTF_8);
String mapHTML = "<html>" +
"<head>" +
"<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY"></script>" +
"<script>" +
"function initMap() {" +
" var geocoder = new google.maps.Geocoder();" +
" geocoder.geocode({'address': '" + encodedAddress + "'}, function(results, status) {" +
" if (status === 'OK') {" +
" var mapOptions = {" +
" zoom: 15," +
" center: results[0].geometry.location," +
" mapTypeId: google.maps.MapTypeId.ROADMAP" +
" };" +
" var map = new google.maps.Map(document.getElementById('map'), mapOptions);" +
" new google.maps.Marker({" +
" position: results[0].geometry.location," +
" map: map" +
" });" +
" } else {" +
" alert('Geocode was not successful for the following reason: ' + status);" +
" }" +
" });" +
"}" +
"</script>" +
"</head>" +
"<body onload='initMap()'>" +
"<div id='map' style='width:100%;height:100%;'></div>" +
"</body>" +
"</html>";
webEngine.loadContent(mapHTML);
}
I used WebView on JavaFX to add the map as well. Anybody knows what could I add to the code to fix this. Should I maybe add another class that would do the geocoding for the given location or is there something wrong with the displayed code. Anything helps
2
Answers
There are several questions on this site describing Google Maps not working in JavaFX
WebView
. It seems the Google Maps API does some fairly sophisticated browser checking which causes it to fail running in that context.Another solution is to use the Gluon Maps Project, which provides a "native" (i.e. not relying on
WebView
and HTML/Javascript embedding) JavaFX component displaying Open Street Maps.Gluon maps requires specifying the latitude and longitude of the location to display, so you will need a separate geocoding service if you want to search addresses. Nominatim from Open Street Maps seems to work well enough.
Here’s a Geocoding implementation based on Nominatim:
Geocoder.java
And a simple demo app displaying the address you displayed in Google maps (or close to it), along with a location marker, using Gluon maps:
For completeness, the
pom.xml
:and
module-info.java
:I had the same issue a while ago and my (maybe only temporary) solution was to add the api version 3.55 to the script call
src="http://maps.google.com/maps/api/js?v=3.55&key=API_KEY&libraries=maps,geometry,visualization,marker"
As everythings works when using mozilla I think it is a problem with the WebView ar the WebView Support. I am looking forward to a permanent solution, because I don’t know how long this version will be available.
Any advice is welcome…