Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does having Wifi on but not connected help Network location, when using LocationManager?

This is possibly off-topic for SO, if so I apologise (and gladly accept the flag for closure), but I'm having issues figuring out why when WIFI is on, but not connected to any access point (on my android device), it vastly improves the accuracy of network provider when using LocationManager. Without it on, the general network location result is about 1 mile away from my current position. Also, most of the time the lat/lng values returned are different, so it's not even that it requested once and just cached the result in lastKnownLocation()

Obviously I'm using both GPS and Network providers to get a decent fix on end-user location (when either isn't available), and using the timestamp to figure out which is the latest.

I've searched google and have come up with various answers like: "It just does" and "Its magic" - Which are pretty useless to be honest. I don't want an in-depth description of the inner workings, just a slightly lower level than: "It just does", or "that's how it works".

requested code

// first get location from network provider //
            if(isNetworkEnabled)
            {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, 
                        MIN_TIME_FOR_UPDATES, 
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, 
                        this);
                Logging.Debug("GPSTracker", "Network");
                if(locationManager != null)
                {
                    netLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if(netLocation != null)
                    {
                        latitude = netLocation.getLatitude();
                        longitude = netLocation.getLongitude();
                        speed = netLocation.getSpeed();
                    }
                }
            }

            // if gps is enabled get lat/lng using that as well //
            if(isGPSEnabled)
            {
                if(gpsLocation == null)
                {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER, 
                            MIN_TIME_FOR_UPDATES, 
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, 
                            this);
                    Logging.Debug("GPSTracker", "GPS Enabled");

                    if(locationManager != null)
                    {
                        gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if(gpsLocation != null)
                        {
                            latitude = gpsLocation.getLatitude();
                            longitude = gpsLocation.getLongitude();
                            speed = gpsLocation.getSpeed();                             
                        }
                    }
                }
            }

Any insight is greatly appreciated. Thanks.

like image 434
fantasitcalbeast Avatar asked Feb 12 '14 17:02

fantasitcalbeast


People also ask

How do I stop my location from turning on WIFI?

Settings → Google → Security → Find My Device → Allow remote lock and erase: OFF.

How location manager works in android?

This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to be notified when the device enters the proximity of a given geographical location.


2 Answers

Android, Apple and other companies have access to a huge data base where they can look up Wlan-Device-Id (the Mac-Adress of your WLAN Router) to lat/lon coordinates.
So when your phone detects a "known" WLAN, it can either:
- look up a local cache of Wlan-id to coordinate, or if you have internet connection active,
- it can connect and query the coordinate for the Wlan id via that data base.

Remains the question where they have the coordinate of your WLAN-id?
Probably the first time when you have GPS active while connected to your WLAN.

SkyHook is such a company that provides that data.

like image 120
AlexWien Avatar answered Nov 15 '22 00:11

AlexWien


From the LocationManager API:

public static final String NETWORK_PROVIDER

Added in API level 1 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.

Constant Value: "network"

Even though you are not connected to any access points, your phone can still detect them and use this location info when calling:

netLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
like image 36
Sionnach733 Avatar answered Nov 15 '22 01:11

Sionnach733