Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中找到以前的位置来测量我的行程_Android - Fatal编程技术网

如何在android中找到以前的位置来测量我的行程

如何在android中找到以前的位置来测量我的行程,android,Android,如何在Android中获取以前的位置?我正在尝试制作一个每30秒更新一个位置的应用程序,我想通过查找前一个位置和当前位置之间的距离来测量我的行程。然而,我只能得到当前位置。这是我的密码 public void onCreate(Bundle savedInstanceState) { //This is for the UI super.onCreate(savedInstanceState); setContentView(R.layout.main); showD

如何在Android中获取以前的位置?我正在尝试制作一个每30秒更新一个位置的应用程序,我想通过查找前一个位置和当前位置之间的距离来测量我的行程。然而,我只能得到当前位置。这是我的密码

public void onCreate(Bundle savedInstanceState)
{   //This is for the UI
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    showData = (TextView)findViewById(R.id.showText);
    start = (Button)findViewById(R.id.startButton);
    stop = (Button)findViewById(R.id.stopButton);

    start.setOnClickListener(globalListener);
    stop.setOnClickListener(globalListener);
   //This is for the Location
   locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,10,10,this);
}
public void onLocationChanged(Location location)
{

    distance = location.distanceTo(previousLocation);
    meter = distance + meter;
    showData.setText(Double.toString(meter));
}

public void onStatusChanged(String provider, int status, Bundle extras) {
    //To change body of implemented methods use File | Settings | File Templates.
}

public void onProviderEnabled(String provider) {
    //To change body of implemented methods use File | Settings | File Templates.
}

public void onProviderDisabled(String provider) {
    //To change body of implemented methods use File | Settings | File Templates.
}

为什么不存储当前位置,然后在30秒后读取此存储值。
通过这样做,你可以从新值中减去它,得到你想要的结果

定义两个变量

Location oldLocation;
Location newLocation;
然后你可以在一个循环中这样做:

oldLocation = newLocation;
newLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//Here you can start calculating
确保在进入循环之前设置newLocation


这就是你一直在寻找的吗?

Android系统只保存它最后得到的位置。它不存储任何以前的位置。你必须自己实现这一点。每次获得位置更新时,请保存位置。下次您请求并获取新位置时,将有旧位置与之进行比较。

lastknownlocation将在系统收到新位置更新后立即更新。它也会被任何其他请求位置更新的应用程序更新,而不仅仅是他自己的。因此,从本质上讲,lastknownlocation可能只有几秒钟的时间(而不是30秒)。当然,您可以随时查看您获得位置更新的时间,以查看是否感兴趣。是的,谢谢。这就是我要找的。我需要打圈吗?因为我很喜欢你在OnLocationChanged()方法上说的话。它正在工作,但我觉得我得到的数字有点偏离了它应该是的。好吧,如果你使用OnLocationChangedListener,那么你就不需要使用循环。你应该试一下。如果我帮了忙,别忘了接受答案,同时检查我的问题——也许你能帮上忙;)看起来和我上面写的一样是的,但是你的答案有代码片段:)。值得注意的是,getLastKnownLocation的问题在于,如果在过去30秒内没有得到任何gps更新,最后一个已知位置仍然会返回一个过时的位置。你得小心点。使用onLocationChanged完全消除了循环的需要。每次新的locationChanged更新出现时,用户可以简单地检查保存的更新和最新更新之间的时间差,如果时间差<30秒,只需拒绝位置更新。有效时间(>30秒)的下一次更新现在将设置为旧位置。