With JxBrowser you can embed and use Google Maps in your Java Desktop application. In this case JxBrowser represents kind of layer between your Java application and Google Maps. To embed Google Maps in your Java application you need to create map.html file that initializes and displays the map, create and embed Browser component, load the map.html file and communicate with loaded map using JxBrowser API and Google Maps JavaScript API.


The following steps describes in more details how to embed Google Maps, display some location, zoom in/out the map and set a new marker from Java code.


First you need to create HTML file where you embed Google Maps into HTML document. Follow the instruction in Google Maps Tutorial to find out how to embed Google Maps into HTML document.


The code of our map.html file is the following:
<!DOCTYPE html>
<html>
<head>
   <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
   <style type="text/css">
       html { height: 100% }
       body { height: 100%; margin: 0; padding: 0 }
       #map-canvas { height: 100% }
   </style>
   <script type="text/javascript"
           src="https://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=false"></script>
   <script type="text/javascript">
     var map;
     function initialize() {
       var mapOptions = {
         center: new google.maps.LatLng(48.209331, 16.381302),
         zoom: 4
       };
       map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
     }
     google.maps.event.addDomListener(window, 'load', initialize);

   </script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>

To use this file replace API_KEY with your Google API key. See the instruction about how to obtain API key. Now let’s create a simple Java application, configure it to use JxBrowser library and embed Browser component to display this map.html file: 

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;

public class GoogleMapsSample {
   public static void main(String[] args) {
       final Browser browser = new Browser();
       BrowserView browserView = new BrowserView(browser);

       JFrame frame = new JFrame(“map.html”);
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       frame.add(browserView, BorderLayout.CENTER);
       frame.setSize(900, 500);
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);

       browser.loadURL("C://map.html");
   }
}

 If we run this sample, we will get the following output:

We can communicate with the loaded map using its JavaScript API. To call map functions and access its properties we can use JxBrowser functionality that allows working with JavaScript of the loaded web page. We can execute any JavaScript on the loaded web page and get result of execution.


To zoom in/out the map we can use map.setZoom() function. This function can be invoked from Java side using JxBrowser API. For example:

JButton zoomInButton = new JButton("Zoom In");
zoomInButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
       if (zoomValue < MAX_ZOOM) {
           browser.executeJavaScript("map.setZoom(" + ++zoomValue + ")");
       }
   }
});

JButton zoomOutButton = new JButton("Zoom Out");
zoomOutButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
       if (zoomValue > MIN_ZOOM) {
           browser.executeJavaScript("map.setZoom(" + --zoomValue + ")");
       }
   }
});

To create and set a new marker on the map the google.maps.Marker object must be used. The following code demonstrates how to set marker from Java code on the map:

JButton setMarkerButton = new JButton("Set Marker");
setMarkerButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
       browser.executeJavaScript("var myLatlng = new google.maps.LatLng(48.4431727,23.0488126);\n" +
               "var marker = new google.maps.Marker({\n" +
               "    position: myLatlng,\n" +
               "    map: map,\n" +
               "    title: 'Hello World!'\n" +
               "});");
   }
});

Complete example source code: 

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GoogleMapsSample {

   public static final int MIN_ZOOM = 0;
   public static final int MAX_ZOOM = 21;

   /**
    * In map.html file default zoom value is set to 4.
    */
   private static int zoomValue = 4;

   public static void main(String[] args) {
       final Browser browser = new Browser();
       BrowserView browserView = new BrowserView(browser);

       JButton zoomInButton = new JButton("Zoom In");
       zoomInButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if (zoomValue < MAX_ZOOM) {
                   browser.executeJavaScript("map.setZoom(" + ++zoomValue + ")");
               }
           }
       });

       JButton zoomOutButton = new JButton("Zoom Out");
       zoomOutButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if (zoomValue > MIN_ZOOM) {
                   browser.executeJavaScript("map.setZoom(" + --zoomValue + ")");
               }
           }
       });

       JButton setMarkerButton = new JButton("Set Marker");
       setMarkerButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               browser.executeJavaScript("var myLatlng = new google.maps.LatLng(48.4431727,23.0488126);\n" +
                       "var marker = new google.maps.Marker({\n" +
                       "    position: myLatlng,\n" +
                       "    map: map,\n" +
                       "    title: 'Hello World!'\n" +
                       "});");
           }
       });

       JPanel toolBar = new JPanel();
       toolBar.add(zoomInButton);
       toolBar.add(zoomOutButton);
       toolBar.add(setMarkerButton);

       JFrame frame = new JFrame("map.html");
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       frame.add(toolBar, BorderLayout.SOUTH);
       frame.add(browserView, BorderLayout.CENTER);
       frame.setSize(900, 500);
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);

       browser.loadURL("C://map.html");
   }
}

 If run it and click Set Marker button, you will get the following output: