X

Geertjan's Blog

JavaFX, Google Maps, and NetBeans Platform

Geertjan Wielenga
Product Manager

Thanks to a great new article by Rob Terpilowski, and other work and research he describes in that article, it's now trivial to introduce a map component to a NetBeans Platform application. Making use of the GMapsFX library, as described in Rob's article, which provides a JavaFX API for Google Maps, you can very quickly knock this application together. Click to enlarge the image.

Here's all the code (from Rob's article):

@TopComponent.Description(
preferredID = "MapTopComponent",
persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "org.map.MapTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
displayName = "#CTL_MapWindowAction",
preferredID = "MapTopComponent"
)
@NbBundle.Messages({
"CTL_MapWindowAction=Map",
"CTL_MapTopComponent=Map Window",
"HINT_MapTopComponent=This is a Map window"
})
public class MapWindow extends TopComponent implements MapComponentInitializedListener {
protected GoogleMapView mapComponent;
protected GoogleMap map;
private static final double latitude = 52.3667;
private static final double longitude = 4.9000;
public MapWindow() {
setName(Bundle.CTL_MapTopComponent());
setToolTipText(Bundle.HINT_MapTopComponent());
setLayout(new BorderLayout());
JFXPanel panel = new JFXPanel();
Platform.setImplicitExit(false);
Platform.runLater(() -> {
mapComponent = new GoogleMapView();
mapComponent.addMapInializedListener(this);
BorderPane root = new BorderPane(mapComponent);
Scene scene = new Scene(root);
panel.setScene(scene);
});
add(panel, BorderLayout.CENTER);
}
@Override
public void mapInitialized() {
//Once the map has been loaded by the Webview, initialize the map details.
LatLong center = new LatLong(latitude, longitude);
MapOptions options = new MapOptions();
options.center(center)
.mapMarker(true)
.zoom(9)
.overviewMapControl(false)
.panControl(false)
.rotateControl(false)
.scaleControl(false)
.streetViewControl(false)
.zoomControl(false)
.mapType(MapTypeIdEnum.ROADMAP);
map = mapComponent.createMap(options);
//Add a couple of markers to the map.
MarkerOptions markerOptions = new MarkerOptions();
LatLong markerLatLong = new LatLong(latitude, longitude);
markerOptions.position(markerLatLong)
.title("My new Marker")
.animation(Animation.DROP)
.visible(true);
Marker myMarker = new Marker(markerOptions);
MarkerOptions markerOptions2 = new MarkerOptions();
LatLong markerLatLong2 = new LatLong(latitude, longitude);
markerOptions2.position(markerLatLong2)
.title("My new Marker")
.visible(true);
Marker myMarker2 = new Marker(markerOptions2);
map.addMarker(myMarker);
map.addMarker(myMarker2);
//Add an info window to the Map.
InfoWindowOptions infoOptions = new InfoWindowOptions();
infoOptions.content("<h2>Center of the Universe</h2>")
.position(center);
InfoWindow window = new InfoWindow(infoOptions);
window.open(map, myMarker);
}
}

Awesome work Rob, will be useful for many developers out there.

Join the discussion

Comments ( 3 )
  • Rob Terpilowski Friday, June 6, 2014

    Thanks Geertjan,

    Did you suppress the TopComponent tab by using a custom TabDisplayerUI class which you had detailed in a previous post:

    https://blogs.oracle.com/geertjan/entry/farewell_to_space_consuming_weird

    Or is there a more straightforward way to do it in version 8 of the RCP?


  • Geertjan Sunday, June 8, 2014

    No straightforward way yet, unfortunately. Just used the TabDisplayerUI approach.


  • JimR Tuesday, November 18, 2014

    How do I add this new Map module to the Palette for the GUI builder in Netbeans 8?


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.