Refresh / Update Location more frequently

edited April 2014 in Android Mode

Hi, I'm making a gps app using android mode, currently tested on my xperia z. Everything is working fine, except the refresh/update rate is slow, it takes about 12sec-14sec to update my location once. I haven't try anything yet, please help me if you guys know about this thanks

my code :

import android.content.Context;
import android.location.*;
import android.os.Bundle;
LocationManager manager;
GPSLocationListener gps;
float latitude;
float longitude;
float accuracy;
String provider;


class GPSLocationListener implements LocationListener
{
  void onLocationChanged( Location _loc )
  {
    latitude = (float) _loc.getLatitude();
    longitude = (float) _loc.getLongitude();
    accuracy = (float) _loc.getAccuracy();
    provider = _loc.getProvider();
  }
  void onProviderDisabled( String _provider )
  {
    provider = "";
  }
  void onProviderEnabled( String _provider )
  {
    provider = _provider;
  }
  void onStatusChanged( String _provider, int status, Bundle xtras )
  {
  }
}
void setup()
{
  orientation( PORTRAIT );
  latitude = 0;
  longitude = 0;
  accuracy = 0;
  provider = "";

  textSize(50);
}


void draw()
{
  background( 5, 10, 85 );
  fill( 250, 255, 13 );
  noStroke();
  translate( width/2, height/2 );
  String msg = "Latitude: " + latitude + "\n";
  msg += "Longitude: " + longitude + "\n";
  msg += "Accuracy: " + accuracy + "\n";
  msg += "Provider: " + provider;
  textAlign( CENTER, CENTER );
  text( msg, 0, 0 );
}


void onResume()
{
  super.onResume();
  gps = new GPSLocationListener();
  manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
  manager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 
  0, 0, gps );
}
void onPause()
{
  super.onPause();
}

Answers

  • edited April 2014

    Try this class:

    import android.app.AlertDialog;
    import android.app.Service;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.IBinder;
    import android.provider.Settings;
    import android.util.Log;
    
    public class GPSTracker extends Service implements LocationListener {
    
      private final Context mContext;
    
      // flag for GPS status
      boolean isGPSEnabled = false;
    
      // flag for network status
      boolean isNetworkEnabled = false;
    
      // TIPO DI PROVIDER TROVATO
      String PROVIDERgps = "";
    
      // flag for GPS status
      boolean canGetLocation = false;
    
      Location location; // location
      double latitude; // latitude
      double longitude; // longitude
    
        // The minimum distance to change Updates in meters
      private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
    
      // The minimum time between updates in milliseconds
      private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
    
      // Declaring a Location Manager
      protected LocationManager locationManager;
    
      public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
      }
    
      public Location getLocation() {
        try {
          locationManager = (LocationManager) mContext
            .getSystemService(LOCATION_SERVICE);
    
          // getting GPS status
          isGPSEnabled = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    
          // getting network status
          isNetworkEnabled = locationManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
          if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
            PROVIDERgps = "null";
          } 
          else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
              //
              PROVIDERgps = "Network";
              //
              locationManager.requestLocationUpdates(
              LocationManager.NETWORK_PROVIDER, 
              MIN_TIME_BW_UPDATES, 
              MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
              Log.d("Network", "Network");
              if (locationManager != null) {
                location = locationManager
                  .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if (location != null) {
                  latitude = location.getLatitude();
                  longitude = location.getLongitude();
                }
              }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
              //
              PROVIDERgps = "GPS";
              //
              if (location == null) {
                locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 
                MIN_TIME_BW_UPDATES, 
                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("GPS Enabled", "GPS Enabled");
                if (locationManager != null) {
                  location = locationManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                  if (location != null) {
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                  }
                }
              }
            }
          }
        } 
        catch (Exception e) {
          e.printStackTrace();
        }
    
        return location;
      }
    
      public String getPROVIDERgps() {
        return PROVIDERgps;
      }
    
    
    
      // Stop using GPS listener
      // Calling this function will stop using GPS in your app
    
      public void stopUsingGPS() {
        if (locationManager != null) {
          locationManager.removeUpdates(GPSTracker.this);
        }
      }
    
    
      // Function to get latitude
    
      public double getLatitude() {
        if (location != null) {
          latitude = location.getLatitude();
        }
    
        // return latitude
        return latitude;
      }
    
    
      // Function to get longitude
    
        public double getLongitude() {
        if (location != null) {
          longitude = location.getLongitude();
        }
    
        // return longitude
        return longitude;
      }
    
    
      // Function to check GPS/wifi enabled
      // @return boolean
    
      public boolean canGetLocation() {
        return this.canGetLocation;
      }
    
    
      // Function to show settings alert dialog
      // On pressing Settings button will lauch Settings Options
    
      public void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
    
        // Setting Dialog Title
        alertDialog.setTitle("Impostazioni GPS");
    
        // Setting Dialog Message
        alertDialog.setMessage("Il GPS non è abilitato. Vuoi andare al menù impostazioni GPS?");
    
        // On pressing Settings button
        alertDialog.setPositiveButton("Impostazioni", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
          }
        }
        );
    
        // on pressing cancel button
        alertDialog.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
          }
        }
        );
    
        // Showing Alert Message
        alertDialog.show();
      }
    
      @Override
        public void onLocationChanged(Location location) {
      }
    
      @Override
        public void onProviderDisabled(String provider) {
      }
    
      @Override
        public void onProviderEnabled(String provider) {
      }
    
      @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
      }
    
      @Override
        public IBinder onBind(Intent arg0) {
        return null;
      }
    }
    
  • i've tried but it doesn't run and with many errors, i'm just a beginner so i didn't know how to correct the error.

  • Try this method for using that class

    import android.widget.Toast;                       // TOAST notifications
    
    boolean GPS = false;
    String GPSprovider = "null";
    double mapCenterLat = 0;
    double mapCenterLon = 0;
    double OLDmapCenterLat = 0;
    double OLDmapCenterLon = 0;
    
        void UPDATEgps() {
          GPSTracker gps = new GPSTracker(this);
    
          // check if GPS enabled     
          if (gps.canGetLocation()) {
    
            if ( gps.getPROVIDERgps().equals("null") == true ) GPS = false;
            else GPS = true;
    
            GPSprovider = gps.getPROVIDERgps();
    
            OLDmapCenterLat = mapCenterLat ;
            OLDmapCenterLon = mapCenterLon;
    
            mapCenterLat = gps.getLatitude();
            mapCenterLon = gps.getLongitude();
          }
          else {
            // can't get location
            // GPS or Network is not enabled
            // Ask user to enable GPS/network in settings
            gps.showSettingsAlert();
          }
          if ( OLDmapCenterLat != mapCenterLat || OLDmapCenterLon != mapCenterLon ) {
    Toast.makeText(this, "Lat: " + mapCenterLat + "\nLon: " + mapCenterLon, 3000).show();
    
              TOASTandroid("Posizione aggiornata tramite "+GPSprovider+"\n\nLat: " + mapCenterLat + "\nLon: " + mapCenterLon, 3000);
          }
        }
    
Sign In or Register to comment.