Android 仅使用WIFI无法在室内环境中获取正确位置
我编写了一个程序,提供用户在建筑物中的位置,但不提供实际位置。我知道Gps不能在建筑物内提供高精度的结果。我的代码如下:Android 仅使用WIFI无法在室内环境中获取正确位置,android,gps,geolocation,Android,Gps,Geolocation,我编写了一个程序,提供用户在建筑物中的位置,但不提供实际位置。我知道Gps不能在建筑物内提供高精度的结果。我的代码如下: public class Internet extends Service implements LocationListener { Context context; LocationManager locationManager; public Internet(Context context) { this.context = context; lo
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 .
}
请尝试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坐标不可用,它将使用网络坐标进行结算
// 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;
}
你的问题是什么?我的问题是位置而不是实际位置!您确定Gps在建筑物中返回正确位置吗?Gps比网络更可靠,请参考此,如前面的回答:。。注意:GPS提供商更准确。如果GPS提供商不是avbl,则可以使用网络提供商。