2

我正在使用 GPS 服务在我的应用程序中获取纬度和经度。当信号可用时,我能够获得纬度和经度。一旦我得到修复,如果我必须去一个 GPS 无法获得位置修复的地方,它仍然会显示最后一个位置。我想获取当前位置而不是其他位置。我尝试使用 onLocationChanged() 和 onGpsStatusChanged() 来获取 GPS 状态,但它没有给我结果。

@Override
public void onGpsStatusChanged(int arg0) {
    // TODO Auto-generated method stub
    if (event == GpsStatus.GPS_EVENT_STARTED) {
        Log.d("zmenaGPS" , "GPS event started ");
        GpsStatus gs = mlocManager.getGpsStatus(null);
        Toast.makeText(getApplicationContext(),String.valueOf(gs),Toast.LENGTH_LONG).show();

    } else if (event == GpsStatus.GPS_EVENT_STOPPED) {
        Log.d("zmenaGPS" , "GPS event stopped ");
        GpsStatus gs = mlocManager.getGpsStatus(null);
        Log.d("zmenaGPSgps status" , String.valueOf(gs));
        Toast.makeText(getApplicationContext(),String.valueOf(gs),Toast.LENGTH_LONG).show();

    } else if (event == GpsStatus.GPS_EVENT_FIRST_FIX) {
        Log.d("zmenaGPS" , "GPS fixace ");
        Toast.makeText(getApplicationContext(),"First Fix",Toast.LENGTH_LONG).show();
    } else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
        Log.d("zmenaGPS" , "GPS EVET NECO ");
    } 

}
4

3 回答 3

1

改用 NETWORK_PROVIDER 来获取更新的位置。像这样..

public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(Context.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;
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
//                  locationManager.requestLocationUpdates(
//                          LocationManager.NETWORK_PROVIDER,
//                          MIN_TIME_BW_UPDATES,
//                          MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    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, 0, 0, locationListenerGps);
//                      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();
                            }
                        }
                    }
                }
            }
//          if (!isGPSEnabled) {
//              showSettingsAlert();
//          } 
        } catch (Exception e){
            e.printStackTrace();
        }

        return location;
    }

希望这可以帮助

于 2013-05-06T06:55:14.527 回答
1

听起来您想获取当前位置,并且您想知道当前位置何时未知,因此您不要尝试使用旧位置。在这种情况下,您可以尝试获取当前位置,然后测试它是否旧。

long fresh=60000; // 1 minute in milliseconds
if(System.currentTimeMillis()-location.getTime()>fresh)
    // Do something useful with location
于 2017-11-09T13:16:28.910 回答
-1

If you could not get the location using GPS, you may need to get the location using NETWORK PROVIDER and compare with you last location fix to know which one is better and use accordingly.

于 2013-05-06T06:53:04.933 回答