Android位置失败

时间:2015-03-23 21:40:55

标签: android gps

在Android中跟踪设备的纬度/经度的最佳方法是什么。我尝试了很多方法,其中没有一种方法可以使用。

我的设备运行的是Android 4.1.2。

在我目前的代码中,有时候GPS会很好地运行,但有时它不会工作(给我0和经度0)。

这是我目前的代码:

locationManager = mLocationManager;
    locationListener = new LocationListener() {

        /**
         * Fired when the location from the sensor changes.
         *
         * @param location Location object.
         */
        @Override
        public void onLocationChanged(Location location) {
            sLatitude = String.valueOf(location.getLatitude());
            sLongitude = String.valueOf(location.getLongitude());
            sLatLon = sLatitude.concat(",").concat(sLongitude);
            Log.d("TOMTOM LOCATION", location.toString());
        }

        /**
         * Fired when the provider's status changes.
         *
         * @param provider  The provider that has changed status.
         * @param status    The new status of the provider.
         * @param extras    Any extra data.
         */
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // Do nothing.
        }

        /**
         * Fired when the provider has been enabled.
         *
         * @param provider  Provider that has been enabled.
         */
        @Override
        public void onProviderEnabled(String provider) {
            // Do nothing.
        }

        /**
         * Fired when the provider has been disabled.
         *
         * @param provider  Provider that has been disabled.
         */
        @Override
        public void onProviderDisabled(String provider) {
            // Do nothing.
        }
    };

    Log.d("TOMTOM", locationManager.getProviders(true).toString());

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        Log.d("TOMTOM", "GPS enabled");
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (location != null) {
            Log.d("TOMTOM", "Request GPS updates");
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        } else {
            Log.d("TOMTOM", "Request network updates");
            location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            if (location != null) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
            } else {
                Log.d("TOMTOM", "Fail");
            }
        }
    } else {
        Log.d("TOMTOM", "GPS disabled");
        Log.d("TOMTOM", "Request network updates");
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (location != null) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        } else {
            Log.d("TOMTOM", "Fail");
        }
    }

任何想法为什么有时我会获得GPS更新(记录为Request GPS updates),有时它会完全失败(记录为Fail)?

2 个答案:

答案 0 :(得分:1)

自从我在Android上使用GPS之后已经有一年左右了,所以如果我不完全正确,请耐心等待。也许有人可以纠正我。

GPS系统不是即时的。您无法启动您的应用程序,并希望立即拥有一个位置。我相信你到日志的原因告诉你它只是Fail,因为Android并不总是有一个最后的已知位置。因此,您可以进入GPS既不可用的情况(例如在建筑物中),您也不具备有意义的最后已知位置。在这种情况下,它将只是null。

答案 1 :(得分:0)

您可以将读数之间的最短时间设置得非常小,甚至为零:

// The minimum distance to change Updates in meters
final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 0 meters

// The minimum time between updates in milliseconds
final long MIN_TIME_BW_UPDATES = 0; // 0 minute 

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 network provider is enabled
} else {
     this.canGetLocation = true;
    // 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();

                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                //if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
        }
   }