Android GPS Example?

Hi,

I’m referencing this library (and it’s related C++ calls) for OFAndroid (@arturo @tallavi)

PG 609 of this book mentions that to use the GPS I have to call ofRegisterGPSEvent() in my setup function. However, the ofRegisterGPSEvent() function as implemented needs some pointer to a Listener ofRegisterGPSEvent(Listener * listener)

What parameters would should I put in this function to call it in setup? Can anyone share a quick example of registering the listener in the app and doing some action when the event is triggered?

I’m new to Of/C++ so hope I’m not asking a stupid question here :wink:

1 Like

Hey raspy!

You need to have a method in your ofApp class that looks like this:

void locationChanged(ofxLocation& location) {

//TODO: handle location change

}

And you register by giving a pointer to your class:

ofRegisterGPSEvent(this);

Sorry I haven’t provided a full example. You are more than welcome to ask more questions.

BONUS: explanations on how to understand this stuff yourself. Look at the method in ofxAndroidGPS.h. It’s a template method that helps you register the event:

template<class Listener>
void ofRegisterGPSEvent(Listener * listener){
	ofAddListener(ofxAndroidGPS::locationChangedE,listener,&Listener::locationChanged);
}

It uses ofAddListener to register your object as a receiver to the ofEvent. ofEvents are used frequently. They are implementation of the publish/subscribe pattern. If you are unfamiliar with it, google it.

ofAddListener works like this:

ofAddListener([event to register], [target object to notify when the event is called], [target method]).

So this helper method just takes one argument, the object, and “assumes” that the class will have a locationChanged method. See how it maps “Listener” to the second parameter and “Listener::locationChanged” to the third parameter.

Hope it helps!

Thanks so much Tallavi. I tried this all (including the ofRegisterGPS event and by defining a method in ofApp called “locationChanged” but am getting this error when I do this:

Error:(19) undefined reference to 'ofApp::locationChanged(ofxLocation&)'

Any ideas?

It looks like the signature of the method is wrong. It needs to return void and have a single parameter of reference (not pointer) to ofxLocation. If you can’t solve the issue, I suggest you write here the signature of the method you created and the line you used to call ofRegisterGPS.

Hi. I’ve got the same issue :
no matching function for call to 'ofAddListener(ofEvent<ofxLocation>&, ofApp*&, void (ofApp::*)(ofxLocation))'

In my ofApp.h, I have added :
#include “ofxAndroidGPS.h”

and : void locationChanged(ofxLocation location);

In my ofApp.cpp :

void ofApp::setup() {
ofRegisterGPSEvent(this);
}

void ofApp::locationChanged(ofxLocation location) { }

Any idea ?

I’m using of 0.9.3 and eclipse 4.5

Thanks.

Hi there.

Thank you dearly for your work on ofxGPS.

It has allowed grabbing non-gimbal lock signals from quaternions.

I was also looking for an example, so here’s a working example for my ofxCompass addon, should be easy to adapt or something.

#pragma once

#include “ofMain.h”
#include “ofxAndroid.h”
#include “ofxCompass.h”

class ofApp : public ofxAndroidApp{

public:

  ofxCompass compass;
  ofPoint orientation;
  ofQuaternion quat;

  //method that will receive events from ofxEventsAddon
  void newOrientation(ofxCompass::CompassData & cd){
  	orientation.x = cd.ox;
  	orientation.y = cd.oy;
  	orientation.z = cd.oz;

  	quat.set(  cd.qx, cd.qy, cd.qz, cd.qw);
  }

  void setup(){
  	compass.startCompass();
  	// add the newFloat method to listen for eventsAddon newFloatEvent
  	ofAddListener(compass.newCompassDataEvent,this,&ofApp::newOrientation);

  }
  void update(){

  }
  void draw(){
  	string info = "compass orientation: " + ofToString(orientation);
  	info += "\ncompass quat: " + ofToString(quat);
  	ofDrawBitmapString(info, 10, 20);
  }

};