海拔总是通过gps android获得零

时间:2014-05-19 09:31:13

标签: android location altitude

  

你好,这是我用来获取纬度,经度和海拔高度的代码。长度纬度正常但高度返回始终为零。我无法理解如何获得高度。请检查代码并告诉我。

if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                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(!location.hasAltitude())
                        {
                            altitude=location.getAltitude();    
                        }else
                        {
                            altitude=1.0;
                        }

                    }
                }
            }
            // 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();
                            if(!location.hasAltitude())
                            {
                                altitude=location.getAltitude();    
                            }else
                            {
                                altitude=1.0;
                            }
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

2 个答案:

答案 0 :(得分:2)

仅当getAltitude()返回hasAltitude()时(当没有高度数据可用且因此返回false时),您才会呼叫0.0

答案 1 :(得分:1)

取决于手机上安装的gps接收器。如果供应商在非标准的nmea实现上实现某种排序,则可能并非所有信息都可用。您可以通过sdk检查芯片组是否支持高度。 Here文档