requestLocationUpdates for count traveled meters

时间:2017-04-19 09:00:20

标签: java android android-gps

更新: 我的真正的类看起来像这样,它的工作原理(它从onLocationChange方法向我提供变量)。但是我无法在MainActivity中找到这个变量 - 它总是把我扔掉0.0 - 这个类有可能在某个时候销毁这个变量吗?

public class GPStracker implements LocationListener {

    Context context;
    double plat;
    double plon;
    double clat;
    double clon;

    public double dis;

    public GPStracker(Context c) {
        context = c;
    }

    public Location getlocation() {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(context, "Uprawnienia nie przyznane", Toast.LENGTH_SHORT).show();
        }

        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean isGPSenabled;
        isGPSenabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if (isGPSenabled) {
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
            Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            return location;
        } else {
            Toast.makeText(context, "Proszę włączyć GPS", Toast.LENGTH_LONG).show();
        }
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {
        clat = location.getLatitude();
        clon = location.getLongitude();
        if (clat != plat || clon != plon) {
            dis += getDistance(plat, plon, clat, clon);
            plat = clat;
            plon = clon;

            setDis(dis);
        }
        Toast.makeText(context, String.valueOf(dis), Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

    public double getDistance(double lat1, double lon1, double lat2, double lon2) {
        double latA = Math.toRadians(lat1);
        double lonA = Math.toRadians(lon1);
        double latB = Math.toRadians(lat2);
        double lonB = Math.toRadians(lon2);
        double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB - lonA)) +
                (Math.sin(latA) * Math.sin(latB));
        double ang = Math.acos(cosAng);
        double dist = ang * 6371;
        return dist;
    }

    public double getDis() {
        return dis;
    }

    public void setDis(double dis) {
        this.dis = dis;
    }
}

我想问(也许这是一个愚蠢的问题),但我正在尝试使用gps提供商计算旅行的数量。现在我正在以这种方式使用requestLocationUpdates:

int i;      
      requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
and 
    public void onLocationChanged(Location location) {
i++
}

所以根据谷歌参考,它应该自动更新我的int我每一个新的米。那就对了?或许我认为错了?多数民众赞成看起来很容易计算旅行的数量,但我不确定它是否会起作用以及它是否准确......有什么建议吗?

2 个答案:

答案 0 :(得分:0)

你应该用例子: loc1.distanceTo(loc2);

答案 1 :(得分:0)

使用以下代码:

requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
每1秒调用{p> requestLocationUpdates,覆盖1米距离。因此,您将在每次通话中获得更新位置。您可以计算先前位置与新位置之间的距离,并将其添加到distance,如下所示,以获得总行驶距离。

您可以比较试试以下代码:

 Location previousLoc = null;
 float distance = 0;
 boolean firstTime = true;
 publc void onLocationChanged(Location location) {
      if(firstTime){
          previousLoc = location;
          firstTime = false;
      }
      distance += previousLoc.distanceTo(location);
      previousLoc = location;
 }

希望这有帮助。