nishitpatel
nishitpatel

Reputation: 652

How to get the current accurate GPS Coordinates using GPS_PROVIDER in android?

I am developing an android app based on GPS location navigation for finding user current location. I am using GPS_PROVIDER I dont want to use any other provider like PASSIVE_PROVIDER or NETWORK_PROVIDER currently my code is working and it gives me the coordinates accurately but when I exit from application and again start the application it gives me slightly different coordinates from the old ones while I am on same place and same position.
To find GPS coordinates I used following code.

LocationManager mlocManager = (LocationManager) 
        getSystemService(Context.LOCATION_SERVICE);
MyLocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, mlocListener);

and my Listener class is

public class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location loc) {
        loc.getLatitude();
        loc.getLongitude();
        String Text = "My current location is: " + "Latitud = "
            + loc.getLatitude() + "Longitud = " + loc.getLongitude();
        TextView txtgps = (TextView) findViewById(R.id.text_GPS_Coordinates);
        txtgps.setText(Text);
        Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT)
                .show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(getApplicationContext(), "Disable",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(getApplicationContext(), "Enable",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
}

I define permission in AndroidManifest is :

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Please tell me the method which gives me the accurate and reliable coordinates and if possible, please explain your code.

Upvotes: 1

Views: 20243

Answers (2)

Jambaaz
Jambaaz

Reputation: 2848

You might expect that the most recent location fix is the most accurate. However, because the accuracy of a location fix varies, the most recent fix is not always the best. You should include logic for choosing location fixes based on several criteria. The criteria also varies depending on the use-cases of the application and field testing.

Here are a few steps you can take to validate the accuracy of a location fix:

  • Check if the location retrieved is significantly newer than the previous estimate.
  • Check if the accuracy claimed by the location is better or worse than the previous estimate.
  • Check which provider the new location is from and determine if you trust it more.

Apart from this, your GPS might give you last cached location of either wifi or tower . This is a known bug and to overcome this, you can write your custom algo to ignore this.

Upvotes: 2

user1954492
user1954492

Reputation: 495

The method you are using to get the locations are always accurate. Even if your location is changed a little bit, it will show different co-ordinates..

Suppose if you want to get only one co-ordinate (that is, when the 1st time you open your application it will remain the same to end) you may use this.

public class MyLocationListener implements LocationListener
{
int f=1;
public void onLocationChanged(Location loc) {
    // TODO Auto-generated method stub
   if(f==1)
   {
    loc.getLatitude();
    loc.getLongitude();
    f=2
   }

    String Text = "My current location is: " +
    "Latitud = " + loc.getLatitude() +
    "Longitud = " + loc.getLongitude();

    TextView txtgps = (TextView) findViewById(R.id.text_GPS_Coordinates);

    txtgps.setText(Text);

    Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();

}

Now it will get only one location co-ordinate (i.e 1st co-ordinate).

Note that it gets the location only one time when your application runs. It will be changed when your application is opened next time. I you want to find the location during movements don't use this method.

Upvotes: 1

Related Questions