James Patrick
James Patrick

Reputation: 263

onlocationChanged is called even if I am sitting at the same place

I have used the code below and everything is working fine except that onLocationChanged is called even if I am sitting at the same location .

I thought it should be called only when I am moving right ?

I only want to get the location after I have moved a certain distance.

Please help me out. Thanks in advance.

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    locationMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
    this);


}

    @Override
       public void onLocationChanged(Location location) {
           Toast.makeText(this, "Working!", Toast.LENGTH_SHORT).show();
                    if (location != null) {
                            double lat = location.getLatitude();
                            double lng = location.getLongitude();

                            String Text = "Latitud = " + lat + "\nLongitud = " +
     lng;

      Toast.makeText(getBaseContext(),Text,Toast.LENGTH_SHORT).show();
                    }
           }

Upvotes: 3

Views: 3655

Answers (2)

Franci Penov
Franci Penov

Reputation: 75991

Network location is not as precise as you would think it is. Therefore the result returned by the sensors can fluctuate. This is even more true when you consider that GPS signal weakens if you don't have direct line of sight with the satellites, and the number of visible satellites also has effect on the precision. This gets even worse when you start using the network provider, where the position is calculated by triangulation of the signal strength of cell towers, and the number and SSIDs of visible wireless network. Since these can fluctuate quite a lot, the precision suffers greatly. There are bunch of averaging algorithms, and heuristics employed to minimize such fluctuations, but ultimately nothing can stabilize it to be as good as you expect it to be.

A simple averaging and variation filtering can help you. Adding a correction based on the device accelerometer can also help a lot, but it will make your code more complex.

Upvotes: 5

Jade Byfield
Jade Byfield

Reputation: 4816

You're requesting location updates at the shortest possible intervals/distances

locationMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);

This is what the documentation says about those parameters

" The location update interval can be controlled using the minTime parameter. The elapsed time between location updates will never be less than minTime, although it can be more depending on the Location Provider implementation and the update interval requested by other applications. "

The minDistance parameter can also be used to control the frequency of location updates. If it is greater than 0 then the location provider will only send your application an update when the location has changed by at least minDistance meters, AND at least minTime milliseconds have passed. However it is more difficult for location providers to save power using the minDistance parameter, so minTime should be the primary tool to conserving battery life.

I personally use a minTime of 10 seconds and 10 meters for my app

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000,
                10, locationListener);

Upvotes: 9

Related Questions