user2244850
user2244850

Reputation: 53

why does my current location return different results #Android

every time I run my application on my Galaxy Note 10.1, I got different locations for my current location, although I've not moved a step!!

below is the GPS Class, and we use an object of it in different classes

public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location = null; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            /*if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network Enabled");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }*/
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS", "GPS is Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

/**
 * Stop using GPS listener Calling this function will stop using GPS in your
 * app
 * */
public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

/**
 * Function to get latitude
 * */
public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude() {
    if (location != null) {
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

/**
 * Function to check GPS/wifi enabled
 * 
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog On pressing Settings button will
 * lauch Settings Options
 * */
public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS settings");

    // Setting Dialog Message
    alertDialog
            .setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(
                            Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    mContext.startActivity(intent);
                }
            });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

    // Showing Alert Message
    alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

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

@Override
public IBinder onBind(Intent arg0) {
    return null;
} }

And here is the Manifest

uses-permission android:name="com.fltirha.faltirha.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />   

I need to get an accurate data and these are all the result locations I got for my current location:

21.536638661233727 , 39.23742879844281
21.536857937756533 , 39.237249447421995
21.536522367052235 , 39.237391639089886

Upvotes: 1

Views: 472

Answers (3)

user2246302
user2246302

Reputation: 396

If you want more accuracy in your results, I would suggest:

  1. Takee your measurements outside in clear view of the sky.

  2. Don't just take the first fix reported. In my experience, the "first fix is the worst fix" as far as accuracy goes. I would wait until receiving four or five position reports before deciding to use one.

  3. If you need several positions, don't stop the GPS receiver! Most GPS receivers will lose data if you stop them. Things like frequency offsets, precise time, etc. All these have to be determined again. If you need a fix in 2 seconds, just keep the receiver running.

Still, you will not get the same answer every time. That's the nature of GPS. It is about accuracy, not perfection. The best you can get in open sky from most commercial handsets is about 3 to 5 meters, most of the time.

Upvotes: 0

Stochastically
Stochastically

Reputation: 7846

GPS isn't 100% accurate. The distance between the first and second reading is only 30 metres, and you could easily have an error of that magnitude, especially if you're indoors.

Along with the lattitude and longitude, the GPS location provider also gives you an accuracy measure in metres. The accuracy represents 1 standard deviation of error, which means that about 2/3 of the time your true location is not more than the given number of metres away from the given location. But it also means that about 1/3 of the time your true location is further away. I think your next step should be to see whether the GPS accuracy readings are correct or not.

One approach which helps is to use a Kalman Filter on the readings from all providers. The advantage of this is that it incorporates the accuracy readings into its guess of your location, so low accuracy readings get ignored and high accuracy readings get much more weight. If you want to use a Kalman filter, I suggest starting with the code that I posted on "Smooth GPS data" last month.

Upvotes: 1

Leonidos
Leonidos

Reputation: 10518

It's ok, calculate distance between your points in meters and you'll see that the difference between them is less than about 50 meters. It's normal for GPS to have such accuracy, you never get exact same position even if you don't move at all.

Upvotes: 0

Related Questions