【问题标题】:Why am I getting the wrong GPS distance calculation in my Android app?为什么我在我的 Android 应用程序中得到错误的 GPS 距离计算?
【发布时间】:2014-08-05 01:35:55
【问题描述】:

在上班途中,我每 1 秒跟踪一次坐标。当我得到两点之间的距离并将其显示在 TextView 中时,我得到的数字比我应该得到的要高得多。例如,一条 9.5 英里的路线最终显示为 14,000 英里范围内的某个地方。以下是我的代码的副本:

GPS 更新处理程序/函数:

public void onGPSUpdate(Location location)
{
    //Get Milestone prefs
    SharedPreferences settings = getSharedPreferences("AppPrefs", 0);
    Integer milestone = settings.getInt("milestone", 5);

    //Get current Speed and Distance
    speed = location.getSpeed();

    Float newDistance;
    if(oldLocation == null){
        oldLocation = new Location("Point B");
        oldLocation = location;

        newDistance = 0f;
    } else {
        newDistance = getDistanceInMiles(location, oldLocation);
    }

    // Make sure the user is actually moving and that we have an old location
    // Add new distance to total
       if(roundDecimal(convertSpeed(speed),2) > 0 && oldLocation != null){
            distance = distance + newDistance;
       }

    // Format the distance to 1 decimal
    String formattedDistance = String.format("%.1f", distance);

    setText(R.id.miles_message, formattedDistance + " ");

}

计算里程函数:

public float getDistanceInMiles(Location p1, Location p2){
    float[] distance1 = new float[1];

    Double latFn = p1.getLatitude();
    Double lonFn = p1.getLatitude();
    Double newLat = p2.getLatitude();
    Double newLon = p2.getLatitude();

    Location.distanceBetween(latFn,lonFn,newLat,newLon, distance1); //in meters
    float distanceInMeters = distance1[0];
    float distanceInMiles = distanceInMeters * 0.00062137f;
    return distanceInMiles; //in miles
}

有什么建议吗?

【问题讨论】:

    标签: android gps location distance


    【解决方案1】:

    您的代码中有错误。您正在使用纬度作为每个点的经度和纬度。此外,还有一个内置的距离定位功能。试试:

    public float getDistanceInMiles(Location p1, Location p2){
        float distanceInMeters = p1.distanceTo(p2);
        float distanceInMiles = distanceInMeters * 0.00062137f;
        return distanceInMiles; //in miles
    }
    

    【讨论】:

      【解决方案2】:

      Gabe 在发现我的代码中的错误方面做得很好。我为每个坐标使用getLatitude(),而不是getLongitude()getLatitude()。我也切换了他的getDistanceInMiles函数:

      public float getDistanceInMiles(Location p1, Location p2){
          float distanceInMeters = p1.distanceTo(p2);
          float distanceInMiles = distanceInMeters * 0.00062137f;
          return distanceInMiles; //in miles
      }
      

      我发现的第二个错误是在我设置oldLocation 的第一个条件语句中。 oldLocation 仅在第一次运行时设置,导致 getdistanceInMiles() 函数每次计算从起始坐标到当前坐标的距离,而不是最后一个坐标和当前坐标之间的距离。

      以下是更正后的代码 sn-ps:

      public void onGPSUpdate(Location location)
      {
          //Get Milestone prefs
          SharedPreferences settings = getSharedPreferences("AppPrefs", 0);
          Integer milestone = settings.getInt("milestone", 5);
      
          //Get current Speed and Distance
          speed = location.getSpeed();
      
          Float newDistance;
          if(oldLocation == null){
              oldLocation = new Location("Point B");
              oldLocation = location;
      
              newDistance = 0f;
          } else {
              newDistance = getDistanceInMiles(location, oldLocation);
              oldLocation = location; //Second correction, make sure to set last coordinates to current coordinates
          }
      
          // Make sure the user is actually moving and that we have an old location
          // Add new distance to total
             if(roundDecimal(convertSpeed(speed),2) > 0 && oldLocation != null){
                  distance = distance + newDistance;
             }
      
              // Format the distance to 1 decimal
              String formattedDistance = String.format("%.1f", distance);
      
              setText(R.id.miles_message, formattedDistance + " ");
      
      }
      
      //First, simplified distance function to Gabe's suggestion
      public float getDistanceInMiles(Location p1, Location p2){
          float distanceInMeters = p1.distanceTo(p2);
          float distanceInMiles = distanceInMeters * 0.00062137f;
          return distanceInMiles; //in miles
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-19
        • 2014-02-03
        • 2021-05-19
        • 2017-05-27
        • 1970-01-01
        • 2023-03-27
        相关资源
        最近更新 更多