Search code examples
androidgeolocation

Latitude and Longitude are changing even I stay in the same place


 @Override
public void onLocationChanged(Location location) {
    Log.d(TAG, "Firing onLocationChanged..............................................");
    mUpdatedLocation = location;
    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
    updateUI();
}

private void updateUI() {
    Log.d(TAG, "UI update initiated .............");
    if (null != mUpdatedLocation && mUpdatedLocation.getAccuracy()>10) {
        String lat = String.valueOf(mUpdatedLocation.getLatitude());
        String lng = String.valueOf(mUpdatedLocation.getLongitude());
        tvUpdatedlocation.setText("\n Updated At Time: " + mLastUpdateTime + "\n" +
                "Latitude: " + lat + "\n" +
                "Longitude: " + lng + "\n" +
                "Accuracy: " + mUpdatedLocation.getAccuracy() + "\n" +
                "Distance: " + mUpdatedLocation.distanceTo(mLastLocation) + "m\n" +
                "Provider: " + mUpdatedLocation.getProvider());
    } else {
        Log.d(TAG, "location is null ...............");
    }
}

I want to compare two Location's and find the distance between them in meter.

Destination location(LOC A) is static, LOC B is moving. But the lat and longitude of LOC B is changing frequesntly.

Location changes even I am in the same position.

Any help will be appreciated...


Solution

  • Because location isn't magic. It works on wireless signals, GPS signals, etc. These change, they have noise, they have intereference. The calculations aren't exact. Notice the accuracy- an X meters of accuracy just means there's a 66% chance you're within X meters of that location. So yes, your location will constantly change. You have to decide how much change you're willing to ignore before deciding you've really moved.

    Also note that you'll once in a while get a reading that's just really off. For example, if you run GPS all night you'll get a few readings that show you a mile off. You have to filter out these spikes as well, or deal with the fact they appeared to teleport.