是否有可能知道我的位置来自gps或glonass?

时间:2014-12-26 09:04:09

标签: android location

假设设备支持gps和glonass(在硬件级别支持)。

现在,当我通过android.location API获取位置时,是否可以知道位置来自哪里的硬件?


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?
    }
}

如果我们像这样使用GPSStatus.Listener

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);    
    }
}

3 个答案:

答案 0 :(得分:6)

位置来自的硬件是手机中的GPS / GLONASS芯片。芯片从绕地球轨道运行的卫星接收信号。

GPS和GLONASS卫星在支持此功能的设备上组合使用。要确定是否使用GLONASS卫星确定位置,您可以注册GpsStatusListener或致电LocationManager.getGpsStatus()。在getSatellites()对象上调用GpsStatus,并在返回列表中的每个getPrn()对象上调用GpsSatellite。如果值介于65和88之间且usedInFix()方法返回true,则使用GLONASS卫星计算最后一个位置。

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

答案 1 :(得分:1)

正如@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();
    }
}

请注意,它只是上述答案的补充。 祝你好运。

答案 2 :(得分:0)

通常,支持Glonass和GPS的大多数设备都会将来自两个星座的测量值组合起来以形成一个位置。你没有得到一个,你通常得到两个。 GPS与Glonass星座的混合将取决于您的位置以及可见的卫星。

正如Batuhan所示,告诉我们使用哪些卫星的方法是走完报告的卫星列表,找到修复中使用的卫星,然后查看它们的ID号。 ID值为1到32用于GPS卫星,65到88用于Glonass。要记住的一件事是,标准的android有一个名为gps.h的脑死文件头文件,它不了解像Glonass这样的其他星座,它只喜欢GPS。许多制造商在解析从GPS驱动程序中提取数据时做了一个简单的调整来解决这个问题,但需要在App处理器端和GPS驱动程序代码中完成。你不能只在一侧调整这些东西,而不需要在另一侧进行匹配。希望您的设备能够进行此更改,以便您可以查看来自所有星座的卫星ID号。