0

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.

Walker
  • 307
  • 4
  • 15

3 Answers3

3

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());
Joeri Sykora
  • 514
  • 3
  • 8
  • Sir, thank you for the answer, but Gluon cant seem to find the class PositionService and Position, please refer the updated question above. – Walker Apr 07 '16 at 08:19
  • Hi, why is it variable position is returning null? I already turned on my location in high accuracy. Any idea Sir? – Walker Apr 08 '16 at 03:19
  • 1
    When loading the PositionService the first time, it will try to set the current position to the last known position, by using the following method: http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String). This might return `null`. You can instead add a ChangeListener on the position property: `positionService.positionProperty().addListener((obs, oldPos, newPos) -> System.out.println("New GPS position: " + newPos.getLatitude() + "," + newPos.getLongitude()));`. This listener will be called each time the position changes. – Joeri Sykora Apr 08 '16 at 19:33
1

This can be achieved via the Gluon Mobile library. http://gluonhq.com/products/mobile/

mipa
  • 10,369
  • 2
  • 16
  • 35
1

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';
}
Community
  • 1
  • 1
Eugene Ryzhikov
  • 17,131
  • 3
  • 38
  • 60
  • hi, where to place this code? Can you tell me where to place this codes in build.gradle? – Walker Apr 08 '16 at 02:21
  • your javafxports application should have build.gradle file in the root folder. This file may already have a dependency section, just add those 4 entries to it. You should also read javafxports documentation at http://docs.gluonhq.com/javafxports/#_build_gradle – Eugene Ryzhikov Apr 08 '16 at 03:04