Able to fetch current location but its not accurate

1.1k views Asked by At

This question may a repetition but i am not satisfied with others ,that is why asking again .i have created a simple app to show current location and displayed it on map.But its not accurate.I tested my app within a building and is fetching the nearby road as my current location,But other apps like Myteksi,Grab teksi is showing my company name as current location and its accurate.i dont know why its so.Please help.Code for fetching current location is giving below

protected void gotoCurrentLocation() {
    Location currentLocation = mLocationClient.getLastLocation();
    if (currentLocation == null) {
        Log.d("currentLocation-->>>", "null");
        Toast.makeText(this, "Current location isn't available",
                Toast.LENGTH_SHORT).show();
    } else {
        LatLng ll = new LatLng(currentLocation.getLatitude(),
                currentLocation.getLongitude());
        Log.d("lattitude", currentLocation.getLatitude()+"");
        Log.d("longitude", currentLocation.getLongitude()+"");

        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll,
                DEFAULTZOOM);
        map.animateCamera(update);
        /*String address= service.GetAddress(currentLocation.getLatitude(),
                currentLocation.getLongitude());
        Log.d("address", address);*/

    }
}

please comment if any other codes are needed.

3

There are 3 answers

1
T3KBAU5 On

You might be getting stale location data, as you're using LocationClient.getLastKnownLocation(), which returns the last cached location, not the current one. You could try requesting your own location updates using a LocationListener. To do this, you need to create your own LocationListener and override the default onLocationChanged(Location) behavior. For example:

final LocationListener ll = new LocationListener(){

    int i=0;

    @Override
    public void onLocationChanged(Location loc) {

        if(i < 5 && loc.getAccuracy() > 8){
            i ++;
            return;
        }
        double lat = loc.getLatitude();
        double lon = loc.getLongitude();
        double acc = loc.getAccuracy();
    }
};

You then need to register the listener using an instance of LocationManager:

final LocationManager lm  = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

This example will register the listener for whenever the GPS location changes. The actual listener code above waits for at least 5 location changes to help ensure that the final location is accurate (the longer time waited, the more time it will have had to lock-on to satellites), then gets the latitude, longitude, and accuracy from the Location object. There are many other ways that you can setup your listener, but that's the way that I did it in one of my apps. If you Google something like "android location updates", you should find some other good tutorials for it. Good luck!

0
Ilya Gazman On

This is because fetching location is a very complicated task to do. And the native implantation of LocationClient might not be the most accurate.

Consider using one or all the 13 sensors(Table 1) in your device to improve that.

  • TYPE_ACCELEROMETER Hardware Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), including the force of gravity. Motion detection (shake, tilt, etc.).
  • TYPE_AMBIENT_TEMPERATURE Hardware Measures the ambient room temperature in degrees Celsius (°C). See note below. Monitoring air temperatures.
  • TYPE_GRAVITY Software or Hardware Measures the force of gravity in m/s2 that is applied to a device on all three physical axes (x, y, z). Motion detection (shake, tilt, etc.).
  • TYPE_GYROSCOPE Hardware Measures a device's rate of rotation in rad/s around each of the three physical axes (x, y, and z). Rotation detection (spin, turn, etc.). TYPE_LIGHT Hardware Measures the ambient light level (illumination) in lx. Controlling screen brightness.
  • TYPE_LINEAR_ACCELERATION Software or Hardware Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), excluding the force of gravity. Monitoring acceleration along a single axis.
  • TYPE_MAGNETIC_FIELD Hardware Measures the ambient geomagnetic field for all three physical axes (x, y, z) in μT. Creating a compass.
  • TYPE_ORIENTATION Software Measures degrees of rotation that a device makes around all three physical axes (x, y, z). As of API level 3 you can obtain the inclination matrix and rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the getRotationMatrix() method. Determining device position.
  • TYPE_PRESSURE Hardware Measures the ambient air pressure in hPa or mbar. Monitoring air pressure changes.
  • TYPE_PROXIMITY Hardware Measures the proximity of an object in cm relative to the view screen of a device. This sensor is typically used to determine whether a handset is being held up to a person's ear. Phone position during a call.
  • TYPE_RELATIVE_HUMIDITY Hardware Measures the relative ambient humidity in percent (%). Monitoring dewpoint, absolute, and relative humidity.
  • TYPE_ROTATION_VECTOR Software or Hardware Measures the orientation of a device by providing the three elements of the device's rotation vector. Motion detection and rotation detection.
  • TYPE_TEMPERATURE Hardware Measures the temperature of the device in degrees Celsius (°C). This sensor implementation varies across devices and this sensor was replaced with the
  • TYPE_AMBIENT_TEMPERATURE sensor in API Level 14 Monitoring temperatures
0
Milad Faridnia On

As you said you test the app in indoor location. And you know in indoor locations GPS sensor will not work, According to google's docs:

Although GPS is most accurate, it only works outdoors.

So your location might come from Network Provider using wi-fi or cell-id, which is not enough accurate.

Android's Network Location Provider determines user location using cell tower and Wi-Fi signals, providing location information in a way that works indoors and outdoors.

and you must be aware of that:(when using getLastLocation() )

To get the current location, create a location client, connect it to Location Services, and then call its getLastLocation() method. The return value is the best, most recent location, based on the permissions your app requested and the currently-enabled location sensors.

BUT:

The current location is only maintained while a location client is connected to Location Service. Assuming that no other apps are connected to Location Services, if you disconnect the client and then sometime later call getLastLocation(), the result may be out of date.

and also please take a look at this to learn more about Maintaining a current best estimate:

http://developer.android.com/guide/topics/location/strategies.html#BestEstimate

I hope this information helps. ;)