Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get accurate current location from Network Provider

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

like image 790
Archie.bpgc Avatar asked Mar 26 '13 10:03

Archie.bpgc


People also ask

What is network location provider?

network –> (AGPS, CellID, WiFi MACID): Name of the network location provider. This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup. Requires either of the permissions android. permission.

Which interface do we use to get the location updates?

The LocationListener interface, which is part of the Android Locations API is used for receiving notifications from the LocationManager when the location has changed. The LocationManager class provides access to the systems location services.


1 Answers

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.

like image 197
Ovidiu Latcu Avatar answered Nov 07 '22 22:11

Ovidiu Latcu