【问题标题】:getLatitude() , getLongitude() returns wrong latitude and longitude valuesgetLatitude() , getLongitude() 返回错误的纬度和经度值
【发布时间】:2012-12-30 18:45:35
【问题描述】:

这是我用来查找当前位置的代码,但它没有准确地指向位置,它返回的位置为 9.920473,78.102423 而不是 9.909076, 78.100758

我不知道我哪里出错了,请帮助我提出建议。

   try {
        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;
            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();
                        }
                        Log.e("latitude", ""+latitude);
                        Log.e("longitude", ""+longitude);
                    }
                }
                // 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);
                        if (locationManager != null) {
                            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();                      
                                longitude = location.getLongitude();
                            }
                            Log.e("latitude", ""+latitude);
                            Log.e("longitude", ""+longitude);
                        }
                    }
                }
            }
        } 

【问题讨论】:

  • 放大够了吗?假设您的 gps 返回不正确的值是遥不可及的。
  • 请注意,您正在记录网络和 gps 位置 - 也许您应该在日志中指出用户正在查看的位置?另外,为什么要对两者都进行测试?如果是 gps,你不关心网络,否则如果网络 - 你的粒度不会很好。
  • 放大够了吗?抱歉,我无法得到它@Siddharth,你能解释一下吗? . .
  • 如果缩小,将无法准确定位并识别返回的gps是否正确。对吧?
  • 回答标题:不,它没有。你解释错了

标签: android map geolocation


【解决方案1】:

您首先使用的是网络提供商,这不是很准确。如果您从 Network 获得位置修复,您的代码将不会使用 GPS 位置。在这种情况下,可以预期位置会有如此大的变化。如果您需要准确的位置,请使用 GPS Provider。

【讨论】:

  • 但是我的 GPS 代码在 locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) 中返回 null;我找不到哪里出错了。
  • 在这种情况下,最初使用来自 NETWORK_PROVIDER 的最后一个已知位置,但稍后当您的应用从 GPS_PROVIDER 接收到更准确的位置时,请改用它。
【解决方案2】:

试试这个代码:

              public void onLocationChanged(Location location) {
          if (location != null) {
            GeoPoint point = new GeoPoint(
                (int) (location.getLatitude() * 1E6), 
                (int) (location.getLongitude() * 1E6));

            Toast.makeText(getBaseContext(), 
                "Latitude: " + location.getLatitude() + 
                " Longitude: " + location.getLongitude(), 
                Toast.LENGTH_SHORT).show();

}

【讨论】:

  • 它会做什么?
猜你喜欢
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
  • 1970-01-01
相关资源
最近更新 更多