-1

I creating an app to GPS tracking.

Unfortunetlly the app doesn't show actual location (lat,long). It always shows lat:50.059644 long:19.9206669. But I want to get and show the actual location of user(me).

It's my main activity:

Button btnShowLocation;
GPSTracker gps;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about);

    btnShowLocation = (Button) findViewById(R.id.btn1);
    btnShowLocation.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {        
            gps = new GPSTracker(About.this);

            if(gps.canGetLocation()){                   
                double latitude = gps.getLatitude();
                double longitude = gps.getLongitude();
                Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();    
            }else{

                gps.showSettingsAlert();
            }

        }
    });
}

And this is my GPS Tracker:

public class GPSTracker extends Service implements LocationListener{
     private final Context mContext;

        boolean isGPSEnabled = false;                
        boolean canGetLocation = false;      
        Location location;

        private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters                   
        private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute                 
        protected LocationManager locationManager;

        public GPSTracker(Context context) {
            this.mContext = context;
            getLocation();
        }

    public Location getLocation() {
        try{
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);            
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);


            if(!isGPSEnabled) {
                System.out.println("disabled");
                //no network provider is enabled
            } else {
                this.canGetLocation = true;

                if(isGPSEnabled){
                    System.out.println("gps wlaczony");
                    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) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        } catch(Exception e){
            e.printStackTrace();
        }
        return location;
    }
Koin Arab
  • 1,045
  • 3
  • 19
  • 35

1 Answers1

1

You request location updates here:

locationManager.requestLocationUpdates

but don't wait for the updates to come.
You have to register your requests and provide a listener to the onLocationChanged method. That method will be called when you have a new location.
Other than that, in order for the process to work you can't initialize GPSTracker every time the Btn is clicked. Do it once and wait for updates.
Try this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about);

    btnShowLocation = (Button) findViewById(R.id.btn1);
    btnShowLocation.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {        
            gps = new GPSTracker(About.this);
        }
    });
}

public class GPSTracker extends Service implements LocationListener{
    private final Context mContext;
    boolean isGPSEnabled = false;                
    boolean canGetLocation = false;      
    Location location;

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters                   
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute                 
    protected LocationManager locationManager;

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);            
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);


        if(!isGPSEnabled) {
            System.out.println("disabled");
            //no network provider is enabled
        } else {
            this.canGetLocation = true;

            if(isGPSEnabled){
                System.out.println("gps wlaczony");
                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) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show()

    }
}
Ohad Zadok
  • 3,452
  • 1
  • 22
  • 26