Java Location API LocationListener | Telit Cinterion IoT Developer Community
November 26, 2018 - 2:29pm, 3514 views
Hi everyone,
I'm developing a Java program on Cinterion PDS8 module and I'm trying to get GPS information by usig JSR179 Location API. I want to provide location updates using LocationListener.
This is my code:
public class LocationTracker implements LocationListener {
Location locationUpdated;
public LocationTracker(){
}
public void locationUpdated(LocationProvider provider, Location location){
if ( location!=null && location.isValid()){
Coordinates coord = location.getQualifiedCoordinates();
System.out.println("new latitude: " + coord.getLatitude());
System.out.println("new longitude: " + coord.getLongitude());
System.out.println("new time: " + location.getTimestamp());
System.out.println("new speed: " + location.getSpeed());
locationUpdated = location;
}
public void providerStateChanged(LocationProvider provider, int newState){
}
public Location getNewLoc(){
return locationUpdated;
}
}
and this is the startApp method in the main program:
protected void startApp() throws MIDletStateChangeException {
try{
Location loc;
Location tracker tracker;
int i = 0;
Criteria cr = new Criteria();
cr.setCostAllowed(false);
cr.setHorizontalAccuracy(10);
cr.setVerticalAccuracy(10);
provider = LocationProvider.getInstance(cr);
tracker = new LocationTracker();
provider.setLocationListener(tracker,10,5,5);
while(i < N_ITER){
Thread.sleep(60000);
loc = tracker.getNewLoc();
i++;
}
}catch(LocationException e){
e.printStackTrace();
}catch(InterruptedException e){
e.printStackTrace();
}
The LocationProvider triggers the locationUpdated method whenever the location changes. What I'm expected is to see the print on video of the informations about the new location every time the provider call locationUpdated (in my code every 10 seconds) but it returns the same Location it retrieves for first.
What am I doing wrong?
Thanks,
Best regards
Alessio
Hello,
I haven't tried to test it but I think that you have configured quite short values for interval, timeout and maxAge - I suppose that the location may not be updated every 10 seconds. Please test with default values - set -1 for each parameter and check how frequently the callback method will be called. Is the device moving?
Regards,
Bartłomiej
Thanks for your reply,
I tested the code with the default values (and another high values for interval, timeout and maxAge) but the program's behavior is the same. The device are not moving but at least I expect to see different time values from the new location. Seems that once the fix is done the provider switch off the GPS engine and doesn't get any new Location...
Regards,
Alessio
And how about if you try to call provider.getLocation(-1) in your code for instance instead of tracker.getNewLoc() - does it change then or is it still the same?
It works but I have a variable delay in getting the answer. I'm trying to use the Listener to avoid such delay. My impression is that getLocation() always turns on and off the GPS at each call thus waiting for the fix every time.
Actually I have to associate some data to a location and time with the highest possible accurancy so I can't tolerate a delay higher than one or two seconds.
Regards,
Alessio
Hello,
I've done some research and I think that I know what could cause this problem. For the correct GPS functioning it is important to have the current time set on the module - it should be UTC time. It is not done automatically - please check with AT+CCLK command, set the time and test.
Regards,
Bartłomiej
Thank you Bartłomiej for the help and your interest in the problem but unfortunatly it doesn't work yet. I setted the time on the module with the current time but it continues to give me the same Location every time it calls the calback method. At this point I don't know what could be the problem...the way the provider get the updated location is not very clear for me. As I told you in my first comment seems that once it retrives the first location it switch off the GPS engine and then give me the same location without updating. I think I will have to try to get the location in another approach, for example through the AT^SGPSC command’s by parsing the NMEA output string.
Regards,
Alessio
I have tested it with EHS8 because I didn't have PDS8 and I was able to observe returning the same data each time. But after setting the current UTC time (not local tile) the data returned by the listener was fresh each time. I have used your settings 10,5,5 for LocationProvider. Anyway I think that you can experiment with these values. Maybe your antenna is located far from the open sky and is receiving a weak signal. It would be also a good idea read NMEA sentences and analyze the parameters.
Regards,
Bartłomiej
I had wrongly setted the clock with the local time. I have tested today setting the current UTC time and with a better signal finally it works. Thank you very much for the help.
Reagards,
Alessio