deucalion0
deucalion0

Reputation: 2440

Why is my location constantly changing, is my code incorrect?

I am working toward building a distance traveled part of my application but I am having issues with my location. In the location listener I am outputting the Lat and Long coordinates in a toast and I can see the coordinates constantly changing, now I know they will never be perfectly the same each time but when I store the first registered location in the listener and then check the distance between that and the constantly updated location the distance jumps around and can be as high as 25 meters. Here is my listener code:

public class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location location) {

        if(flag ==0){

            locationC = location;
            flag = 1;
        }

        //Constantly updated location
        locationD = location;

        //walking distance is distance from first initialized location to current location
        walking = locationC.distanceTo(locationD);

        String s =String.valueOf(location.getLatitude()); 
        String ss =String.valueOf(location.getLongitude()); 

         Toast.makeText(getApplicationContext(),"  "+s+" "+ss+" " ,Toast.LENGTH_SHORT).show(); 
         Toast.makeText(getApplicationContext(),"Walking distance is "+walking ,Toast.LENGTH_SHORT).show(); 

    }

    public void onStatusChanged(String s, int i, Bundle b) {
    }

    public void onProviderDisabled(String s) {
    }

    public void onProviderEnabled(String s) {
    }
}

here is the rest of my location code which is run in the onCreate method:

// Creating the location manger and towers etc
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria crit = new Criteria();
    towers = lm.getBestProvider(crit, false);
    location = lm.getLastKnownLocation(towers);


    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATE, MINIMUM_DISTANCECHANGE_FOR_UPDATE,  new MyLocationListener());

    if (location != null) {
        glat = location.getLatitude();
        glon = location.getLongitude();
    }

I researched the V2 maps which I am using to see if there was a more accurate way to get the location and I found that googleMap.getLocation(); was broken, and indeed it caused my application to fail. I am not sure if I have set up my listener correctly, or any of the other location code, I feel that the location should not be jumping around like that.

I will say that I am using GPS to get the location under a moderately cloudy sky, the device tells me location is set by GPS. Can anyone advise on a better pragmatically way to get a finer location?

Upvotes: 1

Views: 740

Answers (3)

Nahuel Barrios
Nahuel Barrios

Reputation: 1910

Which is the value of your constant MINIMUM_DISTANCECHANGE_FOR_UPDATE?

I've assigned 50 meters to it and it's ok for my app. I also assigned 2 minutes to MINIMUM_TIME_BETWEEN_UPDATE.

Source code of my app | See method subscribeToLocationUpdates

Upvotes: 3

m0skit0
m0skit0

Reputation: 25873

25m deviation is totally normal under civilian GPS precision.

See here for more details about how GPS works, specially:

The cause for poor accuracy is not always obvious but is usually attributable to one of the following source of error:

- Multipath / signal corruption
- Low number of satellites / poor satellite geometry
- Erratic Ionospheric activity

Upvotes: 3

lenik
lenik

Reputation: 23508

With the current GPS technology it's absolutely normal for lat/lon to jump around within a 20-50 meters.

Upvotes: 3

Related Questions