Gluon 位置服务显示坐标的时间太长
Gluon position service is taking too long to display the coordinates
我尝试了下面的代码,它是有效的,但是在启动应用程序后第一次点击按钮时,带有坐标的消息在点击按钮时正确显示,但是对于下一次点击,消息需要很长时间不幸的是,该显示消息了。
我的代码如下:
button.setOnAction(e->{
PositionService positionService = Services.get(PositionService.class).orElseThrow(() -> new RuntimeException("PositionService not available."));
positionService.positionProperty().addListener((obs, ov, nv) -> MobileApplication.getInstance().showMessage("Latest known GPS coordinates from device: " + nv.getLatitude() + ", " + nv.getLongitude()));
});
提前致谢。
假设您有一个使用 Gluon 插件为您的 IDE 创建的单一视图项目,并且您已经添加了所需的 location
服务和对 Android 清单的许可:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
在 BasicView
中你可以做例如:
public BasicView(String name) {
super(name);
Label label = new Label("GPS is not active");
Button button = new Button("Active GPS");
button.setGraphic(new Icon(MaterialDesignIcon.LOCATION_ON));
button.setOnAction(e -> {
Services.get(PositionService.class).ifPresent(service -> {
// if there is GPS service, disable button to avoid adding more
// listeners
button.setDisable(true);
label.setText("Waiting for GPS signal");
service.positionProperty().addListener((obs, ov, nv) ->
label.setText("Latest known GPS coordinates:\n" + nv.getLatitude() + ", " + nv.getLongitude()));
});
});
VBox controls = new VBox(15.0, label, button);
controls.setAlignment(Pos.CENTER);
setCenter(controls);
}
或者您可以在创建视图时初始化服务,而不需要激活它:
public BasicView(String name) {
super(name);
Label label = new Label("GPS is not active");
Services.get(PositionService.class).ifPresent(service -> {
label.setText("Waiting for GPS signal");
service.positionProperty().addListener((obs, ov, nv) ->
label.setText("Latest known GPS coordinates:\n" + nv.getLatitude() + ", " + nv.getLongitude()));
});
}
无论哪种方式,您都可以将应用程序部署到您的 Android/iOS 设备并进行测试。
编辑
可选,如果你想包含一个按钮,那么你可以点击它多次。
public BasicView(String name) {
super(name);
Label label = new Label("GPS is not active");
Button button = new Button("Get GPS coordinates");
button.setGraphic(new Icon(MaterialDesignIcon.LOCATION_ON));
button.setDisable(true);
Services.get(PositionService.class).ifPresent(service -> {
label.setText("Waiting for GPS signal");
service.positionProperty().addListener((obs, ov, nv) -> {
label.setText("Latest known GPS coordinates:\n" + nv.getLatitude() + ", " + nv.getLongitude());
});
// enable button and add listener to retrieve coordinates
button.setDisable(false);
button.setOnAction(e -> {
Position position = service.getPosition();
MobileApplication.getInstance().showMessage("Latest known GPS coordinates from device:\n" +
position.getLatitude() + ", " + position.getLongitude());
});
});
VBox controls = new VBox(15.0, label, button);
controls.setAlignment(Pos.CENTER);
setCenter(controls);
}
请注意,单击按钮时应使用 service.getPosition()
。
我尝试了下面的代码,它是有效的,但是在启动应用程序后第一次点击按钮时,带有坐标的消息在点击按钮时正确显示,但是对于下一次点击,消息需要很长时间不幸的是,该显示消息了。
我的代码如下:
button.setOnAction(e->{
PositionService positionService = Services.get(PositionService.class).orElseThrow(() -> new RuntimeException("PositionService not available."));
positionService.positionProperty().addListener((obs, ov, nv) -> MobileApplication.getInstance().showMessage("Latest known GPS coordinates from device: " + nv.getLatitude() + ", " + nv.getLongitude()));
});
提前致谢。
假设您有一个使用 Gluon 插件为您的 IDE 创建的单一视图项目,并且您已经添加了所需的 location
服务和对 Android 清单的许可:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
在 BasicView
中你可以做例如:
public BasicView(String name) {
super(name);
Label label = new Label("GPS is not active");
Button button = new Button("Active GPS");
button.setGraphic(new Icon(MaterialDesignIcon.LOCATION_ON));
button.setOnAction(e -> {
Services.get(PositionService.class).ifPresent(service -> {
// if there is GPS service, disable button to avoid adding more
// listeners
button.setDisable(true);
label.setText("Waiting for GPS signal");
service.positionProperty().addListener((obs, ov, nv) ->
label.setText("Latest known GPS coordinates:\n" + nv.getLatitude() + ", " + nv.getLongitude()));
});
});
VBox controls = new VBox(15.0, label, button);
controls.setAlignment(Pos.CENTER);
setCenter(controls);
}
或者您可以在创建视图时初始化服务,而不需要激活它:
public BasicView(String name) {
super(name);
Label label = new Label("GPS is not active");
Services.get(PositionService.class).ifPresent(service -> {
label.setText("Waiting for GPS signal");
service.positionProperty().addListener((obs, ov, nv) ->
label.setText("Latest known GPS coordinates:\n" + nv.getLatitude() + ", " + nv.getLongitude()));
});
}
无论哪种方式,您都可以将应用程序部署到您的 Android/iOS 设备并进行测试。
编辑
可选,如果你想包含一个按钮,那么你可以点击它多次。
public BasicView(String name) {
super(name);
Label label = new Label("GPS is not active");
Button button = new Button("Get GPS coordinates");
button.setGraphic(new Icon(MaterialDesignIcon.LOCATION_ON));
button.setDisable(true);
Services.get(PositionService.class).ifPresent(service -> {
label.setText("Waiting for GPS signal");
service.positionProperty().addListener((obs, ov, nv) -> {
label.setText("Latest known GPS coordinates:\n" + nv.getLatitude() + ", " + nv.getLongitude());
});
// enable button and add listener to retrieve coordinates
button.setDisable(false);
button.setOnAction(e -> {
Position position = service.getPosition();
MobileApplication.getInstance().showMessage("Latest known GPS coordinates from device:\n" +
position.getLatitude() + ", " + position.getLongitude());
});
});
VBox controls = new VBox(15.0, label, button);
controls.setAlignment(Pos.CENTER);
setCenter(controls);
}
请注意,单击按钮时应使用 service.getPosition()
。