All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dlsc.gmapsfx.ClusteredMainApp Maven / Gradle / Ivy

package com.dlsc.gmapsfx;

import com.dlsc.gmapsfx.javascript.event.UIEventType;
import com.dlsc.gmapsfx.javascript.object.*;
import com.dlsc.gmapsfx.service.directions.DirectionsRenderer;
import com.dlsc.gmapsfx.shapes.*;
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import netscape.javascript.JSObject;

import java.util.Locale;

/**
 * Example Application for creating and loading a GoogleMap into a JavaFX
 * application
 *
 * @author Rob Terpilowski
 */
public class ClusteredMainApp extends Application implements MapComponentInitializedListener {
    protected ClusteredGoogleMapView mapComponent;
    protected ClusteredGoogleMap map;
    protected DirectionsPane directions;

    private Button btnZoomIn;
    private Button btnZoomOut;
    private Label lblZoom;
    private Label lblCenter;
    private Label lblClick;
    private ComboBox mapTypeCombo;
	
	private MarkerOptions markerOptions2;
	private Marker myMarker2;
	private Button btnHideMarker;
	private Button btnDeleteMarker;

        
    @Override
    public void start(final Stage stage) throws Exception {
        mapComponent = new ClusteredGoogleMapView(null, Locale.getDefault().getLanguage(), null, true);
        mapComponent.addMapInitializedListener(this);
                
        BorderPane bp = new BorderPane();
        ToolBar tb = new ToolBar();

        btnZoomIn = new Button("Zoom In");
        btnZoomIn.setOnAction(e -> {
            map.zoomProperty().set(map.getZoom() + 1);
        });
        btnZoomIn.setDisable(true);

        btnZoomOut = new Button("Zoom Out");
        btnZoomOut.setOnAction(e -> {
            map.zoomProperty().set(map.getZoom() - 1);
        });
        btnZoomOut.setDisable(true);

        lblZoom = new Label();
        lblCenter = new Label();
        lblClick = new Label();
        
        mapTypeCombo = new ComboBox<>();
        mapTypeCombo.setOnAction( e -> {
           map.setMapType(mapTypeCombo.getSelectionModel().getSelectedItem() );
        });
        mapTypeCombo.setDisable(true);
        
        Button btnType = new Button("Map type");
        btnType.setOnAction(e -> {
            map.setMapType(MapTypeIdEnum.HYBRID);
        });
		
		btnHideMarker = new Button("Hide Marker");
		btnHideMarker.setOnAction(e -> {hideMarker();});
		
		btnDeleteMarker = new Button("Delete Marker");
		btnDeleteMarker.setOnAction(e -> {deleteMarker();});
		
        tb.getItems().addAll(btnZoomIn, btnZoomOut, mapTypeCombo,
                new Label("Zoom: "), lblZoom,
                new Label("Center: "), lblCenter,
                new Label("Click: "), lblClick,
				btnHideMarker, btnDeleteMarker);

        bp.setTop(tb);
        
        bp.setCenter(mapComponent);

        Scene scene = new Scene(bp);
        stage.setScene(scene);
        stage.show();
    }

    DirectionsRenderer renderer;
    
    @Override
    public void mapInitialized() {
        
        //System.out.println("MainApp.mapInitialised....");
        
        //Once the map has been loaded by the Webview, initialize the map details.
        LatLong center = new LatLong(47.606189, -122.335842);
        mapComponent.addMapReadyListener(() -> {
            // This call will fail unless the map is completely ready.
            checkCenter(center);
        });
        
        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.TERRAIN)
                .styleString("[{'featureType':'landscape','stylers':[{'saturation':-100},{'lightness':65},{'visibility':'on'}]},{'featureType':'poi','stylers':[{'saturation':-100},{'lightness':51},{'visibility':'simplified'}]},{'featureType':'road.highway','stylers':[{'saturation':-100},{'visibility':'simplified'}]},{\"featureType\":\"road.arterial\",\"stylers\":[{\"saturation\":-100},{\"lightness\":30},{\"visibility\":\"on\"}]},{\"featureType\":\"road.local\",\"stylers\":[{\"saturation\":-100},{\"lightness\":40},{\"visibility\":\"on\"}]},{\"featureType\":\"transit\",\"stylers\":[{\"saturation\":-100},{\"visibility\":\"simplified\"}]},{\"featureType\":\"administrative.province\",\"stylers\":[{\"visibility\":\"off\"}]},{\"featureType\":\"water\",\"elementType\":\"labels\",\"stylers\":[{\"visibility\":\"on\"},{\"lightness\":-25},{\"saturation\":-100}]},{\"featureType\":\"water\",\"elementType\":\"geometry\",\"stylers\":[{\"hue\":\"#ffff00\"},{\"lightness\":-25},{\"saturation\":-97}]}]");
        
        //[{\"featureType\":\"landscape\",\"stylers\":[{\"saturation\":-100},{\"lightness\":65},{\"visibility\":\"on\"}]},{\"featureType\":\"poi\",\"stylers\":[{\"saturation\":-100},{\"lightness\":51},{\"visibility\":\"simplified\"}]},{\"featureType\":\"road.highway\",\"stylers\":[{\"saturation\":-100},{\"visibility\":\"simplified\"}]},{\"featureType\":\"road.arterial\",\"stylers\":[{\"saturation\":-100},{\"lightness\":30},{\"visibility\":\"on\"}]},{\"featureType\":\"road.local\",\"stylers\":[{\"saturation\":-100},{\"lightness\":40},{\"visibility\":\"on\"}]},{\"featureType\":\"transit\",\"stylers\":[{\"saturation\":-100},{\"visibility\":\"simplified\"}]},{\"featureType\":\"administrative.province\",\"stylers\":[{\"visibility\":\"off\"}]},{\"featureType\":\"water\",\"elementType\":\"labels\",\"stylers\":[{\"visibility\":\"on\"},{\"lightness\":-25},{\"saturation\":-100}]},{\"featureType\":\"water\",\"elementType\":\"geometry\",\"stylers\":[{\"hue\":\"#ffff00\"},{\"lightness\":-25},{\"saturation\":-97}]}]
        map = mapComponent.createMap(options,false);
        directions = mapComponent.getDirec();
        
        map.setHeading(123.2);
//        System.out.println("Heading is: " + map.getHeading() );

        MarkerOptions markerOptions = new MarkerOptions();
        LatLong markerLatLong = new LatLong(47.606189, -122.335842);
        markerOptions.position(markerLatLong)
                .title("My new Marker")
                .icon("mymarker.png")
                .animation(Animation.DROP)
                .visible(true);

        final Marker myMarker = new Marker(markerOptions);

        markerOptions2 = new MarkerOptions();
        LatLong markerLatLong2 = new LatLong(47.906189, -122.335842);
        markerOptions2.position(markerLatLong2)
                .title("My new Marker")
                .visible(true);

        myMarker2 = new Marker(markerOptions2);

        map.addClusterableMarker(myMarker);
        map.addClusterableMarker(myMarker2);

        InfoWindowOptions infoOptions = new InfoWindowOptions();
        infoOptions.content("

Here's an info window

with some info

") .position(center); InfoWindow window = new InfoWindow(infoOptions); window.open(map, myMarker); map.fitBounds(new LatLongBounds(new LatLong(30, 120), center)); // System.out.println("Bounds : " + map.getBounds()); lblCenter.setText(map.getCenter().toString()); map.centerProperty().addListener((ObservableValue obs, LatLong o, LatLong n) -> { lblCenter.setText(n.toString()); }); lblZoom.setText(Integer.toString(map.getZoom())); map.zoomProperty().addListener((ObservableValue obs, Number o, Number n) -> { lblZoom.setText(n.toString()); }); // map.addStateEventHandler(MapStateEventType.center_changed, () -> { // System.out.println("center_changed: " + map.getCenter()); // }); // map.addStateEventHandler(MapStateEventType.tilesloaded, () -> { // System.out.println("We got a tilesloaded event on the map"); // }); map.addUIEventHandler(UIEventType.click, (JSObject obj) -> { LatLong ll = new LatLong((JSObject) obj.getMember("latLng")); //System.out.println("LatLong: lat: " + ll.getLatitude() + " lng: " + ll.getLongitude()); lblClick.setText(ll.toString()); }); btnZoomIn.setDisable(false); btnZoomOut.setDisable(false); mapTypeCombo.setDisable(false); mapTypeCombo.getItems().addAll( MapTypeIdEnum.ALL ); LatLong[] ary = new LatLong[]{markerLatLong, markerLatLong2}; MVCArray mvc = new MVCArray(ary); PolylineOptions polyOpts = new PolylineOptions() .path(mvc) .strokeColor("red") .strokeWeight(2); Polyline poly = new Polyline(polyOpts); map.addMapShape(poly); map.addUIEventHandler(poly, UIEventType.click, (JSObject obj) -> { LatLong ll = new LatLong((JSObject) obj.getMember("latLng")); // System.out.println("You clicked the line at LatLong: lat: " + ll.getLatitude() + " lng: " + ll.getLongitude()); }); LatLong poly1 = new LatLong(47.429945, -122.84363); LatLong poly2 = new LatLong(47.361153, -123.03040); LatLong poly3 = new LatLong(47.387193, -123.11554); LatLong poly4 = new LatLong(47.585789, -122.96722); LatLong[] pAry = new LatLong[]{poly1, poly2, poly3, poly4}; MVCArray pmvc = new MVCArray(pAry); PolygonOptions polygOpts = new PolygonOptions() .paths(pmvc) .strokeColor("blue") .strokeWeight(2) .editable(false) .fillColor("lightBlue") .fillOpacity(0.5); Polygon pg = new Polygon(polygOpts); map.addMapShape(pg); map.addUIEventHandler(pg, UIEventType.click, (JSObject obj) -> { //polygOpts.editable(true); pg.setEditable(!pg.getEditable()); }); LatLong centreC = new LatLong(47.545481, -121.87384); CircleOptions cOpts = new CircleOptions() .center(centreC) .radius(5000) .strokeColor("green") .strokeWeight(2) .fillColor("orange") .fillOpacity(0.3); Circle c = new Circle(cOpts); map.addMapShape(c); map.addUIEventHandler(c, UIEventType.click, (JSObject obj) -> { c.setEditable(!c.getEditable()); }); LatLongBounds llb = new LatLongBounds(new LatLong(47.533893, -122.89856), new LatLong(47.580694, -122.80312)); RectangleOptions rOpts = new RectangleOptions() .bounds(llb) .strokeColor("black") .strokeWeight(2) .fillColor("null"); Rectangle rt = new Rectangle(rOpts); map.addMapShape(rt); LatLong arcC = new LatLong(47.227029, -121.81641); double startBearing = 0; double endBearing = 30; double radius = 30000; MVCArray path = ArcBuilder.buildArcPoints(arcC, startBearing, endBearing, radius); path.push(arcC); Polygon arc = new Polygon(new PolygonOptions() .paths(path) .strokeColor("blue") .fillColor("lightBlue") .fillOpacity(0.3) .strokeWeight(2) .editable(false)); map.addMapShape(arc); map.addUIEventHandler(arc, UIEventType.click, (JSObject obj) -> { arc.setEditable(!arc.getEditable()); }); } private void hideMarker() { // System.out.println("deleteMarker"); boolean visible = myMarker2.getVisible(); //System.out.println("Marker was visible? " + visible); myMarker2.setVisible(! visible); // markerOptions2.visible(Boolean.FALSE); // myMarker2.setOptions(markerOptions2); // System.out.println("deleteMarker - made invisible?"); } private void deleteMarker() { //System.out.println("Marker was removed?"); map.removeMarker(myMarker2); } private void checkCenter(LatLong center) { // System.out.println("Testing fromLatLngToPoint using: " + center); // Point2D p = map.fromLatLngToPoint(center); // System.out.println("Testing fromLatLngToPoint result: " + p); // System.out.println("Testing fromLatLngToPoint expected: " + mapComponent.getWidth()/2 + ", " + mapComponent.getHeight()/2); } /** * The main() method is ignored in correctly deployed JavaFX application. * main() serves only as fallback in case the application can not be * launched through deployment artifacts, e.g., in IDEs with limited FX * support. NetBeans ignores main(). * * @param args the command line arguments */ public static void main(String[] args) { System.setProperty("java.net.useSystemProxies", "true"); launch(args); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy