2

i wanted to get the gps position of an user while playing a game (libgdx). I found this tutorial for creating an GPSTracker for android phones. my problem is, that this is written for Android Activities/Tasks, but not for libgdx. without libgdx it works perfectly, but i don't know how to use it with libgdx and there isn't any opportunity for using gps with libgdx, yet.

does somebody know if it's possible to use GPS through libgdx and how? it would be very nice.

vtni
  • 950
  • 3
  • 14
  • 43

3 Answers3

3

You may be able to create your GPSTracker from the Android project wrapper and pass it to the Game instance.

This is what I did to test.-

GPSTracker gps = new GPSTracker(MainActivity.this);
MyGame game = new MyGame(gps.getLatitude(),  gps.getLongitude());

GPSTracker is returning well my current coordinates.

ssantos
  • 16,001
  • 7
  • 50
  • 70
  • 1
    i tried this, but my problem was, that the `GPSTracker` class needs a value of the type `Context`. in the android project, i committed `MainActivity.this`, but when using libgdx, the contentView isn't on MainActivity... – vtni Apr 14 '13 at 14:54
  • Mmh just tried and it seems it's working and returning right coordinates (I'm editing my answer). Are you getting any exception maybe? – ssantos Apr 14 '13 at 15:20
  • of course, that would work but i want to initialize the GPSTracker in a deep subclass of the game. of course, i could commit it to every subclass, but then it would be saved in each of these. isn't there a better option? – vtni Apr 14 '13 at 22:36
  • For those situations, I usually have a `singleton class`, like `MyGameUtils`. You could have one to store the GPSTracker instance, that way any class in your game should have access to it. – ssantos Apr 15 '13 at 00:49
  • i tried this, but i think there must be an error: http://pastebin.com/K8vWUi32 when i test it on my phone, the app crashs. – vtni Apr 15 '13 at 11:31
  • 1
    ok sorry, it works with the singleton class, but only when gps is enabled. when it's not, there isn't a message to turn it on (but it should) and it crashs.. – vtni Apr 15 '13 at 16:36
  • Mmh... what about leaving `GPSTracker` instance in your `MainActivity`, and creating a basic class (i.e. `GPSInfo`) with the fields of interest (such as `latitude` and `longitude`). You would pass an instance of this new class instead of `GPSTracker`'s, and keep its reference also in your Main, which you could update inside the callback methods of `GPSTracker`. – ssantos Apr 15 '13 at 19:08
  • i think the problem is, that the Dialog can't be called. Maybe it's because the ContentView is on the libgdx Activity and not on the MainActivity anymore? – vtni Apr 16 '13 at 12:07
  • 1
    i think i have solved it: i get the view of libgdx with `initializeForView()` in the MainActivity, see this: http://code.google.com/p/libgdx-users/wiki/NativeUIConfirmDialog – vtni Apr 16 '13 at 16:18
0

You can get context object from Applicaion object(look this: Static way to get 'Context' on Android?) , then use the connect to construct the GPSTracker class.

Community
  • 1
  • 1
  • there is one problem: the MainActivity Class is in the android project, so i can't call `MainActivity.getAppContext()` from any other class (in my case it's the renderer class). – vtni Apr 14 '13 at 20:49
  • Yes. So you can get context from a static methon from Application Class, The Application object will be maintain when any component of you app is running. – Lin Xiangyu Apr 15 '13 at 01:43
0

To pass the context to the GPSTracker, try it in your AndroidLauncher.java in the onCreate Method with "getApplicationContext()"

AndroidLauncher.context = getApplicationContext();
this.gps = new GPSTracker(this.context);

AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new MyGdxGame(gps.getLatitude(),  gps.getLongitude()), config);
Suisse
  • 3,467
  • 5
  • 36
  • 59