Is it possible to find out that my location came from gps or glonass?

Suppose a device supports both gps and glonass (hardware support).

Now that I get the location of the android.location API, is it possible to find out the equipment where the place was?


LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener());

class LocationListener implement LocationListener(){
    @Override
    public void onLocationChanged(Location location) {
        GPSStatus status=locationManager.getGpsStatus();
        //check the PRN of the satellites from the status
        //but how can be granted that the `status` object obtained here is exactly the same when it was used to calculate the location?
    }
}

If we use GPSStatus.Listeneras follows:

class LocationListener implement LocationListener,GPSStatus.Listener(){
    private GPSStatus status;
    @Override
    public void onLocationChanged(Location location) {
        if(status!=null){
            //status should be the GPSStatus before get this location
            //check the PRN of the satellites from the status
            //How it can be granted that the `onGpsStatusChanged` will be called before the `onLocationChanged` by the Android System?
        }

    }
   public void onGpsStatusChanged(int event) {
        status = locationManager.getGpsStatus(null);    
    }
}
+4
source share
3 answers

The hardware on which there was a place is the GPS / GLONASS chip on your phone. The chip receives signals from satellites orbiting the earth.

GPS , . , , GpsStatusListener, LocationManager.getGpsStatus(). getSatellites() GpsStatus getPrn() GpsSatellite . 65 88, usedInFix() true, .

. http://developer.sonymobile.com/knowledge-base/technologies/glonass/

+6

@DavidWasser, . @DavidWasser.

LocationManager:

private LocationManager locationManager; 

:

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

implements LocationListener, GpsStatus.Listener:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
        new GPSLocationListener());

GPSLocationListenerClass:

private class GPSLocationListener implements LocationListener,
        GpsStatus.Listener {
    private boolean isGPSFix;

    public void onGpsStatusChanged(int event) {
        checkGlonassFeature(); // the method which checks if locations from GLONASS or GPS
        switch (event) {
        case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
            if (mLastLocation != null)
                isGPSFix = (SystemClock.elapsedRealtime() - lastGPStime) < 3000;
            if (isGPSFix) { // A fix has been acquired.
                Toast toast = Toast.makeText(GPSLocationService.this, "GPS has a fix.", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            } else { // The fix has been lost.
                Toast toast = Toast.makeText(GPSLocationService.this, "GPS DOES NOT have a fix.", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
            break;
        case GpsStatus.GPS_EVENT_FIRST_FIX:
            Toast toast = Toast.makeText(GPSLocationService.this, "GPS got first fix.", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            isGPSFix = true;
            break;
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        //  Do works here
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        String statusDescription = "unknown";
        switch (status) {
        case LocationProvider.OUT_OF_SERVICE:
            statusDescription = "OUT_OF_SERVICE";
            break;
        case LocationProvider.AVAILABLE:
            statusDescription = "AVAILABLE";
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            statusDescription = "TEMPORARILY_UNAVAILABLE";
            break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast toast = Toast.makeText(GPSLocationService.this, "GPS feature is active", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast toast = Toast.makeText(GPSLocationService.this, "GPS feature is passive", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}

, , :

public void checkGlonassFeature() {
    boolean isGPSFromGlonass = false;
    final GpsStatus gs = this.locationManager.getGpsStatus(null);
    final Iterable<GpsSatellite> it = gs.getSatellites();
    for (GpsSatellite sat : it) {
        if(sat.usedInFix()){
            if(sat.getPrn() > 65 && sat.getPrn() < 88)
                isGPSFromGlonass = true;
            else
                isGPSFromGlonass = false;
        }
        else
            isGPSFromGlonass = false;
    }
    if(isGPSFromGlonass){
        Toast toast = Toast.makeText(getBaseContext(), "Location from GLONASS", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
    else{
        Toast toast = Toast.makeText(getBaseContext(), "Location from GPS", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}

, . .

+1

, , Glonass, GPS, , . , . GPS , .

, , , - , , , . 1 32 GPS, 65 88 - . , , - , gps.h, , , GPS. , GPS, , , GPS. , . , , .

0
source

Source: https://habr.com/ru/post/1569027/


All Articles