1

The problem I believe is well known.

So what is my problem?

I am writing an app that is using the GPS provider to find the location of the user and calculate the distance from the user to the end point. This works correctly. However a problem occurs:

The problem in details:

If you go around the city with my app it will give you some numbers for your distance but ... the distance is aways changing. No matter if I move towards my destination the distance either goes higher or goes lower (the value of the distance calculated is "jumping" around from high to low and vise versa) the the previous number of the distance but logically it should be getting lower. I believe this is because the GPS signal is sometimes lost or weak and it cant calculate the distance correctly.

What I need help with?

I want to know is there a way to filter the coordinates received from the GPS so I can get more accurate numbers for distance so when I move towards my end point the distance is calculated correctly(as possible not necessary to be 100% correct) and not go up and down the scales like crazy.

How do I get the coordinates and calculate the distance:

public void onLocationChanged(Location location) 
{
    txtLat = (TextView) findViewById(R.id.currentCoordinatesView);
    clat = location.getLatitude();
    clong = location.getLongitude();

    Location location1 = new Location("Start");
    location1.setLatitude(clat);
    location1.setLongitude(clong);

    Location locationB = new Location("Finish");

    locationB.setLatitude(endLatitude); //endpoint coordinates
    locationB.setLongitude(endLongitude);

    distance = location1.distanceTo(locationB); //calculate the distance

    TextView TextDistance = (TextView)findViewById(R.id.TextDistance);
    TextDistance.setText(new DecimalFormat("##,###,###.##").format(distance)+" m");

    CurLat = location.getLatitude();
    CurLong = location.getLongitude();  
user3182266
  • 1,270
  • 4
  • 23
  • 49

1 Answers1

4

The below answer can be further improved by saving the last, say, 10 location objects and do calculations based on those. If for instance the 10 last locations suggests that the user is moving towards the target at 1m/s, then a new location suggesting a jump of 5 meters is very likely inaccurate and should be ignored.

To filter some of the GPS updated which are way off you could simple do something like this (3 represent how accurate the position should be in respect to your actual position and may be adjusted):

    public void onLocationChanged(Location location) 
    {
        if (location.hasAccuracy() && location.getAccuracy() < 3) {
            txtLat = (TextView) findViewById(R.id.currentCoordinatesView);
            clat = location.getLatitude();
            clong = location.getLongitude();

            Location location1 = new Location("Start");
            location1.setLatitude(clat);
            location1.setLongitude(clong);

            Location locationB = new Location("Finish");

            locationB.setLatitude(endLatitude); //endpoint coordinates
            locationB.setLongitude(endLongitude);

            distance = location1.distanceTo(locationB); //calculate the distance

            TextView TextDistance = (TextView)findViewById(R.id.TextDistance);
            TextDistance.setText(new DecimalFormat("##,###,###.##").format(distance)+" m");

            CurLat = location.getLatitude();
            CurLong = location.getLongitude(); 
        }
    }

Here is the definition of the accuracy measure from: Location getAccuracy()

Get the estimated accuracy of this location, in meters. We define accuracy as the radius of 68% confidence. In other words, if you draw a circle centered at this location's latitude and longitude, and with a radius equal to the accuracy, then there is a 68% probability that the true location is inside the circle.

In statistical terms, it is assumed that location errors are random with a normal distribution, so the 68% confidence circle represents one standard deviation. Note that in practice, location errors do not always follow such a simple distribution.

This accuracy estimation is only concerned with horizontal accuracy, and does not indicate the accuracy of bearing, velocity or altitude if those are included in this Location.

If this location does not have an accuracy, then 0.0 is returned. All locations generated by the LocationManager include an accuracy.

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • Hi thank you for your answer but could you explain to me why location.hasAccuracy && location.getAccuracy() must be < 3.0? And can you show me how to store and calculate the location as you suggested in your answere – user3182266 Feb 25 '14 at 10:09
  • And with your suggestion OnLocation() changed is never called :/ – user3182266 Feb 25 '14 at 10:24
  • 3.0 is just an example, as stated it may be adjusted. I do not have an actual example of that, but it would just be storing the valid/filtered location objects in a list. Then on each location change iterate the list and calculate the average speed (location.getSpeed()) or something else which can help you determine whether the new location is valid. But try the accuracy first, no need to complicate things if that is proving enough improvement. – cYrixmorten Feb 25 '14 at 10:25
  • Are you sure that you are actually getting GPS values then? Could be locations based on network instead.. might explain the bad accuracy too. – cYrixmorten Feb 25 '14 at 10:26
  • To be absolutely sure that you are actually using GPS, you could use my implementation to only accept location changes when the GPS has gotten a first-fix, see: http://stackoverflow.com/questions/19365035/location-servise-gps-force-closed/19366773#19366773 – cYrixmorten Feb 25 '14 at 10:28
  • Yes I am getting GPS values since I get the distance but the accuracy I thing it has something to do with the number 3.0 I will try out and tell you the result – user3182266 Feb 25 '14 at 10:29
  • Yes the problem was with 3.0 ... however the distance still changes and constantly "jumps" it is not still but this time it doesn't jump above the given value I putted 100. Usually it jumps over 100 so I am guessing that when I say if (location.hasAccuracy() && location.getAccuracy() < 100) it will only get coordinates which bring accuracy to the distance of 100m am I right? – user3182266 Feb 25 '14 at 10:39
  • With an 68% confidence, yes, that is the idea. Though GPS locations should never jump more than maximum 10 - 20m, something else must be wrong I think, or are you referring to the distance between the positions? – cYrixmorten Feb 25 '14 at 10:42
  • distance between the positions – user3182266 Feb 25 '14 at 10:48
  • Ok, then try perhaps (location.hasAccuracy() && location.getAccuracy() < 5) instead. The accuracy knows nothing about the distance and is only concerned with the quality of the new incoming location, so 100m is too high but perhaps 3m is too low. Is the same as saying 'I am now within a circle of 100m' which is not worth much as it should always be true. – cYrixmorten Feb 25 '14 at 10:56
  • @AlexWien can you elaborate? The answer has been accepted, besides if you have a better answer feel free to post it. Plus, I state in the answer that it may need to be adjusted. – cYrixmorten Feb 25 '14 at 15:05
  • accuray is the estimated horicontal accuracy measured in meters. GPS rarley achieves a value of 3. You have to choose a value much higher (>=10m). Again whether accepted or not, did this code ever run on a real device? I doubt. – AlexWien Feb 25 '14 at 15:19
  • @AlexWien I never said that it had. Did you read the conversation I had with user3182266 in this comment section? That I too doubt, cause if you did you would find that we tried raising the accuracy to 5m and seem that it worked. Is a bit odd reasoning to downvote in my opinion. – cYrixmorten Feb 25 '14 at 15:24
  • I downvoted because you gave non working tipps. 5 is still to low. only in best sitaution you will get a value of 5. You don't know the answer to his problem, you are guessing. there for the downvote. – AlexWien Feb 25 '14 at 19:22
  • I am not guessing, I am suggesting. And my suggestion is perfectly valid, yes, maybe it is low, hence the note on need for adjustment, though it is not impossible to get ~2m accuracy. Furthermore it was not the only suggestion I came with. But whatever, cannot tell you what to do, only that I do not agree nor understand this as deemed 'not being helpful', which is what downvoting means. – cYrixmorten Feb 25 '14 at 20:22