在 JavafxPorts 中集成 GPS

Integrate GPS in JavafxPorts

我有一个要在 Android 中部署的 Javafx 应用程序。我的应用程序需要获取经纬度,但我不知道如何实现它们。有人可以给我一个代码示例,说明如何在 JavaFX 中执行此操作吗?

更新:

package com.gpstracker;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class GPSTracker extends Application {

    @Override
    public void start(Stage stage) {
        TextField t1 = new TextField();

        TextField t2 = new TextField();

        Button button = new Button("Show Location");
        button.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                PositionService positionService = PlatformFactory.getPlatform().getPositionService();
                Position position = positionService.getPosition();
            }
        });

        VBox root = new VBox(20);
        root.setPadding(new Insets(20));
        root.getChildren().addAll(t1, t2, button);

        Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
        Scene scene = new Scene(root, visualBounds.getWidth(), visualBounds.getHeight());

        stage.setScene(scene);
        stage.show();
    }

}

Gloun 似乎找不到 class 调用 PositionService 和 Position。

这可以通过 Gluon Mobile 库实现。 http://gluonhq.com/products/mobile/

使用 Gluon Mobile,可以使用 PositionService 跟踪设备的当前位置。示例代码片段:

PositionService positionService = PlatformFactory.getPlatform().getPositionService();
Position position = positionService.getPosition();
System.out.println("Current GPS position: " + position.getLatitude() + "," + position.getLongitude());

要使用 Joeri 提到的 PositionService,您必须向 Gluon 开源 charm-down 库添加依赖项。这是 build.gradle:

的示例配置
dependencies {
    compile 'com.gluonhq:charm-down-common:2.0.0';
    desktopRuntime 'com.gluonhq:charm-down-desktop:2.0.0';
    androidRuntime 'com.gluonhq:charm-down-android:2.0.0';
    iosRuntime 'com.gluonhq:charm-down-ios:2.0.0';
}