仅使用WIFI无法在室内环境中获得正确的位置

时间:2015-03-30 10:56:42

标签: android gps geolocation

我写了一个程序,它提供了用户在建筑物中的位置而不是实际位置。我知道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 .

}

2 个答案:

答案 0 :(得分:0)

尝试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。 (如果有人需要进行投票,请同时提及原因)

答案 1 :(得分:0)

此代码可能对您有所帮助。它的作用是,如果可用,它将采用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;
}