【问题标题】:How do I initialize Google Maps API key when using GMapsFX and FXML?使用 GMapsFX 和 FXML 时如何初始化 Google Maps API 密钥?
【发布时间】:2020-07-19 09:11:36
【问题描述】:

我正在尝试建立一个JavaFX 项目,我使用GMapsFX。我已经让它在不使用 FXML 的情况下工作,我刚刚在 start() 方法中初始化了 GoogleMapView 变量。因此我也假设我的 API 密钥不是问题。但是,当使用 FXML 时,我不确定在哪里初始化它。我得到的只是:“仅供开发人员使用”或空白页。是否可能是因为 FXML 在 FXML 控制器中的 initialize() 方法之前加载?

这是我的控制器:

public class MapSceneController implements Initializable, MapComponentInitializedListener {

    //I have tried to initialize it here
    @FXML
    private GoogleMapView mapView = new GoogleMapView(Locale.getDefault().getLanguage(), "AIzaSyAaCPNXgEw5zlJTHLr0MYOmvxOcQ50vLdw");

    @FXML
    private TextField addressTextField;

    private GoogleMap map;

    private GeocodingService geocodingService;

    private StringProperty address = new SimpleStringProperty();

    @Override
    public void mapInitialized() {
        //And here
        //mapView = new GoogleMapView(Locale.getDefault().getLanguage(), "AIzaSyAaCPNXgEw5zlJTHLr0MYOmvxOcQ50vLdw");
        geocodingService = new GeocodingService();
        MapOptions mapOptions = new MapOptions();

        mapOptions.center(new LatLong(47.6097, -122.3331))
                .mapType(MapTypeIdEnum.ROADMAP)
                .overviewMapControl(false)
                .panControl(false)
                .rotateControl(false)
                .scaleControl(false)
                .streetViewControl(false)
                .zoomControl(false)
                .zoom(12);

        map = mapView.createMap(mapOptions);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //And here
        //mapView = new GoogleMapView(Locale.getDefault().getLanguage(), "AIzaSyAaCPNXgEw5zlJTHLr0MYOmvxOcQ50vLdw");
        mapView.addMapInitializedListener(this);
        address.bind(addressTextField.textProperty());
    }

    @FXML
    public void addressTextFieldAction(ActionEvent event) {
        geocodingService.geocode(address.get(), (GeocodingResult[] results, GeocoderStatus status) -> {

            LatLong latLong = null;

            if( status == GeocoderStatus.ZERO_RESULTS) {
                Alert alert = new Alert(Alert.AlertType.ERROR, "No matching address found");
                alert.show();
                return;
            } else if( results.length > 1 ) {
                Alert alert = new Alert(Alert.AlertType.WARNING, "Multiple results found, showing the first one.");
                alert.show();
                latLong = new LatLong(results[0].getGeometry().getLocation().getLatitude(), results[0].getGeometry().getLocation().getLongitude());
            } else {
                latLong = new LatLong(results[0].getGeometry().getLocation().getLatitude(), results[0].getGeometry().getLocation().getLongitude());
            }

            map.setCenter(latLong);

        });
    }
}

这是我的 FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

    <?import com.lynden.gmapsfx.GoogleMapView?>
    <AnchorPane id="AnchorPane" prefHeight="600" prefWidth="1066" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65" fx:controller="sample.JavaFX.MapScene.MapSceneController">
        <children>
            <GoogleMapView fx:id="mapView" prefHeight="600.0" prefWidth="1066.0" AnchorPane.bottomAnchor="0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0" AnchorPane.topAnchor="0.0"/>
            <TextField fx:id="addressTextField" onAction="#addressTextFieldAction" prefHeight="27.0" prefWidth="274.0" promptText="Address" AnchorPane.leftAnchor="10" AnchorPane.topAnchor="10" />
        </children>
    </AnchorPane>

提前致谢! :)

【问题讨论】:

  • 为什么使用 JavaFX 8.x 版?
  • 哈哈,你好。我刚刚从本指南中复制了 FXML:rterp.wordpress.com/2016/08/01/…。这可能是问题吗?
  • TBH,我对 Java 几乎一无所知。仍然在查看您之前的问题时,我读了几次,JavaFX 的更新版本可以解决很多问题。第 8 版来自 2014 年...
  • 我使用的是 JavaFX 版本 11,只是因为我复制了指南中的代码,它在 FXML 文件中说版本 8.x。还是谢谢你:)

标签: java google-maps javafx google-maps-api-3 fxml


【解决方案1】:

您的代码中有几个问题。我不知道 GMapsFX 的 API,但如果构造函数 GoogleMapView 是设置密钥的唯一方法,那么我会提出以下建议。在您的 FXML 文件中,仅定义您想要放置地图视图的容器。然后在初始化方法(在 FXML 部分实例化后自动调用)中手动创建地图视图并将其添加到容器中。删除所有其他初始化,因为它们不起作用。

【讨论】:

  • 谢谢!有效。当你解释它时,这样做真的很有意义。你说我的代码有几个问题,是不是比你上面解释的还要多?没有 JavaFX 经验,所以想知道将来要避免什么:)
  • 您在 FXML 中实例化了地图视图,并在控制器中再次覆盖了它。这也不会独立于 API 密钥的问题而起作用。但你现在可能已经解决了这个问题。
猜你喜欢
  • 2019-04-08
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多