[英]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似乎找不到一个名为PositionService和Position的类。
使用Gluon Mobile,可以使用PositionService跟踪设备的当前位置。 示例代码段:
PositionService positionService = PlatformFactory.getPlatform().getPositionService();
Position position = positionService.getPosition();
System.out.println("Current GPS position: " + position.getLatitude() + "," + position.getLongitude());
这可以通过Gluon Mobile库来实现。 http://gluonhq.com/products/mobile/
要使用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';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.