仅使用 WIFI 无法在室内环境中获取正确位置
Posted
技术标签:
【中文标题】仅使用 WIFI 无法在室内环境中获取正确位置【英文标题】:Unable to get the correct location within indoor environment using with only WIFI 【发布时间】:2015-03-30 10:56:42 【问题描述】:我编写了一个程序,它提供用户在建筑物中的位置,而不是实际位置。我知道 Gps 在建筑中不能提供高精度的结果。我的代码如下:
public class Internet extends Service implements LocationListener
Context context;
LocationManager locationManager;
public Internet(Context context)
this.context = context;
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Location findMeInternet()
Boolean isInternet = false;
Location location;
isInternet = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isInternet)
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 3, 1, this);
if (locationManager != null)
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
return location;
return null;
//other override methods with empty bodies .
【问题讨论】:
你有什么问题? 我的问题是位置不是实际位置! 【参考方案1】:此代码或许可以帮助您。它的作用是,如果可用,它将获取 GPS 坐标。如果 GPS 坐标不可用,它将使用网络坐标来解决。
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public Location getLocation()
try
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled)
// no GPS Provider and no network provider is enabled
else
// Either GPS provider or network provider is enabled
// First get location from Network Provider
if (isNetworkEnabled)
locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null)
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
latitude = location.getLatitude();
longitude = location.getLongitude();
this.canGetLocation = true;
// End of IF network enabled
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled)
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null)
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
latitude = location.getLatitude();
longitude = location.getLongitude();
this.canGetLocation = true;
// End of if GPS Enabled
// End of Either GPS provider or network provider is enabled
catch (Exception e)
e.printStackTrace();
// If GPS is enabled, the GPS coordinates will be returned in location.
// If GPS is not enabled, then the network coordinates will be returned.
// If both are enabled, then GPS co orindate will be returned because GPS coordinates have more accuracy than network coordinates.
return location;
【讨论】:
如前所述:***.com/a/23824578/3492139.. 注意:GPS 提供商更准确。如果 GPS Provider 不是 avbl,则可以使用网络提供商。【参考方案2】:尝试 GPS 提供程序而不是网络提供程序,
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, this);
if (locationManager != null)
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
latitude = location.getLatitude();
longitude = location.getLongitude();
确保设备 GPS 已启用。 (如果有人需要投票,请同时说明原因)
【讨论】:
您确定 Gps 在建筑物中返回正确的位置吗?? GPS 比网络更可靠,参考这个,blog.codecentric.de/en/2014/05/…以上是关于仅使用 WIFI 无法在室内环境中获取正确位置的主要内容,如果未能解决你的问题,请参考以下文章
Android:如果手机通过 WiFi 连接,则无法从网络获取地理位置
FusedLocationProviderClient 无法通过 3G 或 4G 网络获取 GPS 位置,除非先连接到 WIFI 网络