Search code examples
javajavafxjavafx-8jxmapviewerjxmaps

JavaFX: Add MapView to VBox


I want to add a MapView from JXMaps to the VBox in JavaFX, i tried to add the MapView to the border panel and then added to the VBox, but i can´t find any solution to this, i hope you can help me, this is my code:

 public class ControlerMap
 {
     @FXML private BorderPane borderPanel;
     @FXML private Button butom;
     @FXML VBox cosa;

     @FXML public void initialize() 
     {
         MapView sample = new MapView();
         sample.setMaxHeight(394.0);
         sample.setPrefWidth(704.0);
         sample.setVisible(true);
         borderPanel = new BorderPane(sample);
         borderPanel.setVisible(true);
         borderPanel.setLayoutX(76.0);
         borderPanel.setLayoutY(134.0);
         borderPanel.setPrefHeight(200.0);
         borderPanel.setPrefWidth(467.0);
         cosa.getChildren().add(borderPanel);
     }
}

And here is my code from the .FXML file:

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" 
minWidth="-Infinity" prefHeight="496.0" prefWidth="757.0" 
xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="prueba.web.googleMap.ControlerMap">
<children>
      <Button fx:id="butom" layoutX="25.0" layoutY="14.0" 
     mnemonicParsing="false" text="Button" />
     <VBox fx:id="cosa" layoutX="32.0" layoutY="68.0" prefHeight="394.0" 
      prefWidth="704.0">
      <children>
         <BorderPane fx:id="borderPanel" prefHeight="400.0" 
             prefWidth="704.0" />
      </children>
     </VBox>
  </children>
</AnchorPane>

Solution

  • As I can see you created empty MapView.

    MapView sample = new MapView();
    

    Maps will be actually drawn only after initialization of its properties (at least zoom and center). You have to set it in following way:

    sample.setOnMapReadyHandler(new MapReadyHandler() {
           @Override
           public void onMapReady(MapStatus status) {
              if (status == MapStatus.MAP_STATUS_OK) {
                  final Map map = mapView.getMap();
                  map.setCenter(new LatLng(35.91466, 10.312499));
                  map.setZoom(2.0);
              }
           }
    });