JavaFX:将MapView添加到VBox

时间:2017-11-01 06:20:38

标签: java javafx javafx-8 jxmapviewer jxmaps

我想将JXMaps中的MapView添加到JavaFX中的VBox,我试图将MapView添加到边框面板然后添加到VBox,但我找不到任何解决方案,我希望你能帮忙我,这是我的代码:

 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);
     }
}

这是我在.FXML文件中的代码:     

<?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>

1 个答案:

答案 0 :(得分:1)

我可以看到你创建了空MapView。

MapView sample = new MapView();

只有在初始化其属性(至少缩放和居中)后才会实际绘制地图。您必须按以下方式设置它:

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);
          }
       }
});