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

亡梦爱人 提交于 2019-12-04 19:58:52
Jambaaz

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!