GMapsFX :: Add Google Maps to your JavaFX application.

We have been considering adding a map component to our Freight Management application built on the NetBeans RCP and JavaFX, which would allow Lynden dispatchers to track its drivers throughout the city as well as highlight where trailers have been dropped off at customer locations and are ready for pickup.

Google Maps is a logical tool which could be utilized to accomplish this task. While there are examples out on the web for integrating Google Maps with JavaFX, these solutions require mingling JavaScript within the Java code in order to interact with a Google Map loaded within the application.

In an effort to remove the need to code JavaScript within JavaFX in order to use Google Maps, I have created a Java API ‘wrapper’ around the Google Maps JavaScript API and have dubbed this framework GMapsFX. This allows one to add a Google Map component to a JavaFX application and interact with it utilizing a pure Java API.

While at the present time only the most basic Google Map functionality has been ‘wrapped’ by the Java API, I am making this project open source in the hopes that if others find this library useful and require additional functionality, that it could be added and contributed back to the community.

The project can be found on GitHub at:

with the JavaDocs available at:

Below is an example of using GMapsFX to add a map component to a Scene, setting the location to Seattle, and then adding a Marker to the map.

package com.lynden.gmapsexampleapp;

import com.lynden.gmapsfx.GoogleMapView;
import com.lynden.gmapsfx.MapComponentInitializedListener;
import com.lynden.gmapsfx.javascript.object.GoogleMap;
import com.lynden.gmapsfx.javascript.object.LatLong;
import com.lynden.gmapsfx.javascript.object.MapOptions;
import com.lynden.gmapsfx.javascript.object.MapType;
import com.lynden.gmapsfx.javascript.object.Marker;
import com.lynden.gmapsfx.javascript.object.MarkerOptions;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainApp extends Application implements MapComponentInitializedListener {

GoogleMapView mapView;
GoogleMap map;

public void start(Stage stage) throws Exception {

    //Create the JavaFX component and set this as a listener so we know when 
    //the map has been initialized, at which point we can then begin manipulating it.
    mapView = new GoogleMapView();

    Scene scene = new Scene(mapView);

    stage.setTitle("JavaFX and Google Maps");

public void mapInitialized() {
    //Set the initial properties of the map.
    MapOptions mapOptions = new MapOptions(); LatLong(47.6097, -122.3331))

    map = mapView.createMap(mapOptions);

    //Add a marker to the map
    MarkerOptions markerOptions = new MarkerOptions();

    markerOptions.position( new LatLong(47.6, -122.3) )
                .title("My Marker");

    Marker marker = new Marker( markerOptions );



public static void main(String[] args) {



The code above produces the following result

enter image description here


twitter: @RobTerp

Written with StackEdit.

54 thoughts on “GMapsFX :: Add Google Maps to your JavaFX application.

  1. Pingback: JavaFX links of the week, May 5 // JavaFX News, Demos and Insight // FX Experience

  2. Pingback: Java desktop links of the week, May 5 « Jonathan Giles

    • Good question. I believe this is possible to do with the Google JavaScript API, so it would just be a matter of implementing the required JavaScript functions in the GMapsFX library to support this.

      I can add this to the enhancement list if you like.

  3. What do i have to keep in mind with the Google API restrictions from 2500 requests per day?
    How many requests are produced by the library? All in all a great library that has all that i need currently

    • Hi Clayn,
      The library is basically a WebView wrapper around the Javascript API, so it is not making any additional calls than if you were running the Google Maps API in a browser based application with Javascript.


  4. Provided code sample issues the following stack trace :
    netscape.javascript.JSException: ReferenceError: Can’t find variable: google
    at com.sun.webkit.dom.JSObject.fwkMakeException(
    at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
    at com.sun.webkit.WebPage.executeScript(
    at javafx.scene.web.WebEngine.executeScript(
    at com.lynden.gmapsfx.javascript.JavaFxWebEngine.executeScript(
    at com.lynden.gmapsfx.javascript.JavascriptRuntime.execute(
    at com.lynden.gmapsfx.javascript.JavascriptObject.(
    at com.lynden.gmapsfx.javascript.object.LatLong.(
    at application.Sample.mapInitialized(
    at com.lynden.gmapsfx.GoogleMapView.fireMapInitializedListeners(
    at com.lynden.gmapsfx.GoogleMapView$1.changed(
    at com.lynden.gmapsfx.GoogleMapView$1.changed(
    at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(
    at javafx.scene.web.WebEngine$LoadWorker.updateState(
    at javafx.scene.web.WebEngine$LoadWorker.dispatchLoadEvent(
    at javafx.scene.web.WebEngine$LoadWorker.access$1100(
    at javafx.scene.web.WebEngine$PageLoadListener.dispatchLoadEvent(
    at com.sun.webkit.WebPage.fireLoadEvent(
    at com.sun.webkit.WebPage.fwkFireLoadEvent(
    at Method)
    at Method)

    What’s the matter ?

      • The problem comes with the line 114 in the MainApp sample:
        LatLong center = new LatLong(47.606189, -122.335842);
        This line executes the following javascript :
        var LatLong0 = new google.maps.LatLng(47.606189,-122.335842)

        OS = Windows 7

    • Got this exception several times. Figured out it happens when i try to use something that uses the webeninge (in my cases for example “new LatLng()”) when the GoogleMapView wasnt initialized yet. So i think you should wait for such things until “mapInitialized” was called.
      Maybe this works for you

      • Yes, that is exactly what the example is doing? The LatLong objects are being initialized in the mapInitialized callback method. The strange thing is that I can’t seem to replicate this issue. I’m downloading Eclipse now, to see if I can replicate it there, but I’ve tried the example app on a few different machines with different OSs, but haven’t had any luck.

    • Hello, Guys

      I had this same issue, but i solved this in another away. I don’t use in FXML file. I use a AnchorPane and add my GoogleMapView object in this. For example:

      FXML File:


      private AnchorPane GmapAnchorPanel;
      private GoogleMapView mapView;


      Best regards,

  5. I try the sample code on this page (just changing MapType to MapTypeIdEnum) on Eclipse Luna (4.4.0M6) and JDK 1.8.0_05-b13.
    The same exception with version 1.1.0 and 1.1.1.

    • Are you able to run the example app bundled in the release build of 1.1.1?
      java -jar GMapsFX-1.1.1.jar

      The sample code doesn’t start manipulating the Map objects until the underlying map view is created so I’m not sure how the exception is being thrown.

      I’ve tried this out on a couple of different Win 7 and Ubuntu machines, and unfortunately can’t replicate this.

      • Hi! I have received the same error as Jean-Pascal (netscape.javascript.JSException: ReferenceError: Can’t find variable: google), i have tried it with GMapsFX-1.1.1.jar y con GMapsFX-1.1.0.jar but i haven’t get nothing.
        I’ll agree you alot if you’d say us how to solve it.
        Thank you very much.

  6. Well, I got it but without any proxy in my connection. Can rterp inform me if it exists any solutions to use this solution througth a proxy connection?
    Thank you very much!

    • You’ll need to set the following system properties, either in your class that launches the application, or on the command line.


      • Thank you very much rterp, but if my proxy have user and password, what do i have to add? I have been trying but i have not got anything. :S

      • Try adding the following system properties:
        System.getProperties().put(“http.proxyUser”, “someUserName”);
        System.getProperties().put(“http.proxyPassword”, “somePassword”);

      • In my environment I also need


        (usually, the same values as for the the http-case)

    • Unfortunately since the JavaFX WebView is pulling down the image tiles in real-time I don’t think there’s an easy way to cache the map.

      • Thats a “nice” answer. I thought the part that makes the hole thing impossible is the javascript part

  7. Im getting this exception on the provided source demo:

    netscape.javascript.JSException: RangeError: Maximum call stack size exceeded.
    at com.sun.webkit.dom.JSObject.fwkMakeException(
    at com.sun.webkit.dom.JSObject.fwkMakeException(
    at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
    at com.sun.webkit.WebPage.executeScript(
    at javafx.scene.web.WebEngine.executeScript(
    at com.lynden.gmapsfx.javascript.JavaFxWebEngine.executeScript(
    at com.lynden.gmapsfx.GoogleMapView.mapResized(
    at com.lynden.gmapsfx.GoogleMapView.lambda$createMap$19(
    at com.lynden.gmapsfx.GoogleMapView$$Lambda$6/33095720.handle(Unknown Source)
    at com.lynden.gmapsfx.javascript.event.EventHandlers.handleStateEvent(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at sun.reflect.misc.Trampoline.invoke(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at sun.reflect.misc.MethodUtil.invoke(
    at com.sun.webkit.Utilities$
    at Method)
    at com.sun.webkit.Utilities.fwkInvokeWithContext(
    at com.sun.webkit.Timer.twkFireTimerEvent(Native Method)
    at com.sun.webkit.Timer.fireTimerEvent(
    at com.sun.webkit.Timer.notifyTick(
    at javafx.scene.web.WebEngine$PulseTimer$2.pulse(
    at Method)
    at Method)

    I guess it is when trying to put the mark on the map cause it is not showing up

  8. I’m not be able to render images of the project in the icons MarkerOptions
    My code:

    markerOptions.icon( getClass().getClassLoader().getResource(“img/marker.png”).toString() );

    Can anyone?

    • I could not, I tried everything, absolute path, relative path. Only if I place image somewhere in the GMapsFX library – it works. The path should be relative to the maps.html file location.
      I could not find solution how to point application to icon next to my Jar or images/ folder for example

  9. Congratulations, nice project.

    I have a similar project using google maps with javafx and i´ve had a problem with the map, the map is too slow to use comparing with google map running in a browser, with zoom in/out, hybrid map, anything you need to do many times like zoom in/out. I donwloaded this project and realized it´s occured too, searching on web i found a post saying that it occurs because javafx webview doesn´t have cache like a browser, is it true ? is there any way to solve this problem ?

  10. Hello, i used the map it works perfectly, the only matter is that i have to put in in a tabpane in my project and i can’t know how to do it so please if you can help and thanks 😀

  11. The line with the error:


    public void handleStateEvent(String callbackKey) {
    if (handlers.containsKey(callbackKey) && handlers.get(callbackKey) instanceof StateEventHandler) {
    ((StateEventHandler) handlers.get(callbackKey)).handle();
    } else {
    System.err.println(“Error in handle: ” + callbackKey + ” for state handler “);

    The code: “((StateEventHandler) handlers.get(callbackKey)).handle();”

    return the following message:

    ” ((StateEventHandler) handlers.get(callbackKey)).handle() = >Exception occurred in target VM: RangeError: Maximum call stack size exceeded.” …



    • Hi Regula, I was able to do this in the example by putting the buttons in a GridPane and putting the GridPane on top of the Pane that contained the Map View component.


  12. Hi, Rob, how are you?

    I’m exploring your project and i hava a question: it’s possible to take a click event in a marker? For example:
    myMarker.addStateEventHandler(, () -> {
    System.out.println(“Latitude” + myMarker.getMarkerOptions().getLatLong().getLatitude().);

    Best Regards,

    • Very late but just incase you do read this

      map.addUIEventHandler(poly,, (JSObject obj) -> {
      LatLong ll = new LatLong((JSObject) obj.getMember(“latLng”));
      System.out.println(“You clicked the line at LatLong: lat: ” + ll.getLatitude() + ” lng: ” + ll.getLongitude());

      this is from his main app example, poly is a Polyline, I have been able to replicate it using a Marker in the place of the Polyline

      • hi, How are u ?
        I want to know if it’s possible to draw a polyline on GoogleMap ??
        Best Regards,

  13. Hi,
    Thanks for the great work!
    I have only one suggestion in regards to this tutorial:
    I think “MapType” should be changed to “MapTypeIdEnum” for it to work in the current version though.

  14. Hi,

    Congratulations, this is a great library.

    Some suggestion: Is it possible to add a closeclick-UI-Event (especially for the InfoWindow) ?

  15. Where do I put in my Google API key? When I run this, I get
    Caused by: java.lang.IllegalStateException: Couldn’t load map file ‘/html/maps.html’: NullPointerException
    at com.lynden.gmapsfx.GoogleMapView.(
    What goes in this file?

Leave a Reply