I have a Javafx application which is to be deployed in Android. My app needs to get the latitude and longitude but I dont know how to implement them. Can someone give me a code sample on how to do it in JavaFX?
UPDATED:
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 cant seem to find a class call PositionService and Position.
With Gluon Mobile, the current position of a device can be tracked with the PositionService . A sample code snippet:
PositionService positionService = PlatformFactory.getPlatform().getPositionService();
Position position = positionService.getPosition();
System.out.println("Current GPS position: " + position.getLatitude() + "," + position.getLongitude());
This can be achieved via the Gluon Mobile library. http://gluonhq.com/products/mobile/
To use PositionService
, mentioned by Joeri , you have to add a dependency to Gluon open source charm-down
library. Here is a sample configuration for your 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';
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.