Wednesday, March 28, 2012

Embed Google Maps in JavaFX WebView

The Google Maps Javascript API lets you embed Google Maps in your own web pages. With WebView in JavaFX 2.0, you can easy embed Google Maps in your Java application.

Embed Google Maps in JavaFX WebView

Create a googlemaps.html to embed Google Maps in web page.
<!DOCTYPE html>
<title>Java-Buddy: Google Maps</title>
<script src=""></script>
<style>#mapcanvas { height: 360px; width: 100%}</style>

var map;
function loadmap(){

  var options = {
      zoom: 16,
      center: new google.maps.LatLng(51.507222, -0.1275),
      mapTypeId: google.maps.MapTypeId.SATELLITE
  map = new google.maps.Map(document.getElementById("mapcanvas"), options);


<body onload="loadmap()">
<h1>Java-Buddy: Google Maps</h1>
<div id="mapcanvas"></div>

Embed googlemaps.html in WebView in your Java application.
package javafx_googlemaps;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

* @web
public class JavaFX_GoogleMaps extends Application {

  private Scene scene;
  MyBrowser myBrowser;

   * @param args the command line arguments
  public static void main(String[] args) {

  public void start(Stage primaryStage) {
      myBrowser = new MyBrowser();
      scene = new Scene(myBrowser, 640, 480);

  class MyBrowser extends Region{
      HBox toolbar;
      WebView webView = new WebView();
      WebEngine webEngine = webView.getEngine();
      public MyBrowser(){
          final URL urlGoogleMaps = getClass().getResource("googlemaps.html");


- Embed OpenLayers with OpenStreetMap in JavaFX WebView


  1. Hello, I wonder if anyone has done more tests to verify that the webview is robust enough to withstand the generated javascript google maps api?

  2. hi, i want to know if you know how to call scripts in the googlemaps.hmtl file??? TY :D


    This url answer your question.


  4. now i want to save some variables from JAVASCRIPT TO JAVAFX, for example, when a user click on the google map, save that latitude and longitude inside of my JAVAFX Aplication. I know first that var go to javascript, so how save them to JAVAFX to manipulate it later on my aplication ?

    regards :D

  5. This is the best blog javafx i've ever seen... who is the writer of java-buddy?? thank you so much...

  6. Mouse operations on the map don't seem to work: clicking and dragging, double-click to zoom, and scroll wheel to zoom. I noticed that the mouse operations DO work on your OpenLayers example. The java code is almost identical, so I'm guessing the issue is in the javascript from Google. Do you know how to get the mouse actions working on the map?

    Thanks for the JavaFX examples!

  7. Its ur JavaFX version, somehow it doesnt work in 2.2. Get 2.1, it works there.

    Anyone having troubles with overlays disappearing??

    1. hello E7,

      I re-test it on JavaFX 2.2, and it still work as expected. Can you explain in more details?

    2. Yes E7, i had the same problem, but the reason it is still unknown for me, but i found a workaround it seems solved. Put this vm argument "-Dsun.java2d.ddoffscreen=false" to launch your java application.

  8. Buddy, the 2.2. gives you the android interface to control the map, in the 2.1 version it looks like in any other browser, so its all good for me.

    Thanks for the answer fop, i added the vm argument in eclipse but the issue persists. The overlays disappear when zooming in/out, at least just visually, they remain clickable. Somehow markes never disappear. Its quite a bug.

  9. I would like to use JavaFX 2.2.3 to embed Google Earth instead, is there a way to do this ??

  10. Is there any method to passing JavaFX array to javascript array?

  11. Hello, I would like to know how to get the coordinates by clicking on the map? and get the coordinates of where I click and address these coodenadas or place them in a swing textfield
    please help!