1

In my application I want latitude and longitude value by GPS and/ or Network. My code work but some time it give accurate value some time it not give the accurate value, some time it give the value which is 10 or 20 meter far from the accurate place. Code:

mlocListener = new MyLocationListener(getApplicationContext());
    /* Use the LocationManager class to obtain GPS locations */
    LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                10000, 0, mlocListener);
    } else if (mlocManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        mlocManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 10000, 0, mlocListener);
    }

    Location gpsLocation = mlocManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location netLocation = mlocManager
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    if (gpsLocation != null) {
        mlocListener.latitude = gpsLocation.getLatitude();
        mlocListener.longitude = gpsLocation.getLongitude();

        double lat = (double) (gpsLocation.getLatitude());
        double lng = (double) (gpsLocation.getLongitude());


        Toast.makeText(this, "Location attributes using GPS Provider",
                Toast.LENGTH_LONG).show();

    } else if (netLocation != null) {
        mlocListener.latitude = netLocation.getLatitude();
        mlocListener.longitude = netLocation.getLongitude();

        double lat = (double) (netLocation.getLatitude());
        double lng = (double) (netLocation.getLongitude());



        Toast.makeText(this, "Location attributes using NETWORK Provider",
                Toast.LENGTH_SHORT).show();

    } 
Vinit ...
  • 1,409
  • 10
  • 37
  • 66

3 Answers3

2

Use loc.getAccuracy() method to check the accuracy level of location you received. If the value is less then 10(or less than that) then you can consider it , otherwise wait for location Lister to fetch another location.

getLastKnownLocation is your last known location, dont use just getAccuracy also check the time.

Better dont use getLastKnownLocation if you need only accurate location.

abbas.aniefa
  • 2,855
  • 21
  • 30
0

Usually GPS is accurate upto 3m but if its 10 to 20 meters, it is something possible with GPS.

A standard GPS receiver for civil use offers an accuracy down to a few meters. In praxis the number and geometry of the received satellites influences the accuracy considerably, and in daily use, accuracies of about 20 m can be expected.

GPS Accuracy

Also if you are getting your current location from network provider, you may expect even more inaccurate locations.

Habib
  • 219,104
  • 29
  • 407
  • 436
0

If you are looking forward to get maximum accuracy, I will say that you dismiss network provider because it is influenced by other factors related to phone operator etc.

locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) 

This get your location using your phone internal GPSreceiver (time costly) but it gives you the maximum possible accuracy.

amelvin
  • 8,919
  • 4
  • 38
  • 59
Hectico76
  • 9
  • 2