JavaFX, Google Maps, and NetBeans Platform

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.

Comments:

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?

Posted by Rob Terpilowski on June 06, 2014 at 09:58 AM PDT #

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

Posted by Geertjan on June 08, 2014 at 01:55 AM PDT #

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

Posted by JimR on November 18, 2014 at 07:22 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« May 2015
SunMonTueWedThuFriSat
     
2
3
8
9
10
19
22
23
24
25
26
27
28
29
30
31
      
Today