Search code examples
javajsongoogle-mapsgoogle-maps-api-3javafx

Google Maps API routing for a Java desktop application


I want to have a map integrated into my Java desktop application where I can set an origin and destination and show the route on the map.

I have signed up to Google Maps API and have received a key, this provided me with the json data of the route but that is not what I need. How would I apply this data to a map in my Java application? It does not have to be Googles map, as long as it is a map with the route.

I have tried using the Google Maps Embed API, but this must only be used in an iFrame. Instead of the map loading the following text is displayed "The Google Maps Embed API must be used in an iframe.". However if I wrap this in an iFrame and open in a browser I get the desired results.

Any advise would be useful.

Code below

 @Override
public void initialize(URL location, ResourceBundle resources) {
    WebEngine browser = mapView.getEngine();
    mapView.getEngine().load(mapPathBuilder("London","Luton"));
}

public String mapPathBuilder(String from, String to){
    String url = "https://www.google.com/maps/embed/v1/directions?key=";
    String googleAPI = "****************";
    String origin = "&origin=";
    String destination = "&destination=";
    String result = url+googleAPI+origin+from+destination+to;
    return result;
}

Solution

  • A solution may be to use this JavaFX component

    https://github.com/dlsc-software-consulting-gmbh/GMapsFX

    instead of directly working with JavaScript.