问题
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.
回答1:
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.
回答2:
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);
来源:https://stackoverflow.com/questions/21734907/why-does-having-wifi-on-but-not-connected-help-network-location-when-using-loca