How can i find accurate currenct location using only GPS

387 views Asked by At

I am new to Android. I am using the following code but sometimes on Location changed is not called and Location object returns null... Or sometimes it is getting nearest value only but i need accurate same as OLA cabs tracking.

My Code as Follows

MyLocationListener mll=new MyLocationListener();
LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, 
                        0, 
                        0,
                        mll
                        );
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) 
        Log.e("TAG","LocationDetected ");
1

There are 1 answers

2
tahsinRupam On

Location can be got by mainly two types of providers:

  • Network Provider: You can get location from anywhere but accuracy can be awful.
  • GPS Provider: You will get accurate location but you have to go outside to get location otherwise you will get null.

So, you can use both according to accuracy, availability and efficiency by using Criteria . Here is a snippet of my project code-

    criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    //get the best provider depending on the criteria
    provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);

For faster yet accurate location tracking, you may use latest Google Play API which is recommended by Google. Here is the best example I got regarding Google Play/Fused API - http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/