Question

I use the following code to get Current Location from a Network provider in my application:

LocationManager mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean network_enabled = mgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(network_enabled){
Location location = mgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

But it gives a location approximately 300-700 meters away from my actual location.

This is expected from a Network provider. But the problem is:

with only this Network provider enabled, and no GPS, I openned Foursquare application where it shows my current location exactly where I am. Now when I come back to my application, it shows the accurate current location or say the same location which Foursquare showed.

Same thing happens with Google apps like Navigator, Maps etc..,

How can this be done? How are other apps able to get the exact location, based on just the Network provider?

Complete Code:

public class MyLocationActivity extends Activity implements LocationListener {
    private LocationManager mgr;
    private String best;
    Location location;
    public static double myLocationLatitude;
    public static double myLocationLongitude;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        best = mgr.getBestProvider(criteria, true);
        location = mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        dumpLocation(location);
    }

    public void onLocationChanged(Location location) {

        dumpLocation(location);
    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onPause() {

        super.onPause();
        mgr.removeUpdates(this);
    }

    @Override
    protected void onResume() {

        super.onResume();
        mgr.requestLocationUpdates(best, 15000, 10, this);
    }

    private void dumpLocation(Location l) {

        if (l != null){

            myLocationLatitude = l.getLatitude();
            myLocationLongitude = l.getLongitude();
        }
    }
}

Thank You

Was it helpful?

Solution

You are getting just the last known location. You should request location updates from the LocationManager. Use LocationManager.getLocationUpdates.

On your LocationListener on the onLocationChanged(Location location) method, you can check on the Location object, how accurate this location is, like this :

float accuracy=location.getAccuracy();

then, if this Location is accurate enough for you, you can stop receiving location updates from the LocationManager using removeUpdates() , and use the received Location. If the Location is not accurate enough, you can wait for a more precise Location, and stop the updates latter on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top