Thales' cellular IoT products business is now part of Telit Cinterion, find out more.

You are here

Java Location API LocationListener | Telit Cinterion IoT Developer Community

November 26, 2018 - 2:29pm, 3904 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