Thursday Jun 05, 2014

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.

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
« June 2014 »
SunMonTueWedThuFriSat
14
15
19
28
29
     
       
Today