如何在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.
}
答案 0 :(得分:1)
为什么不存储当前位置,然后在30秒后读取该存储值 通过这样做,您可以从新值中减去它并获得您正在寻找的结果。
定义两个变量
Location oldLocation;
Location newLocation;
然后你可以做一个像这样的循环:
oldLocation = newLocation;
newLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//Here you can start calculating
确保在进入循环之前设置newLocation。
这是你在找什么?
答案 1 :(得分:0)
Android系统仅保留存储的最后位置。它不存储任何以前的位置。你必须自己实现它。每次获得位置更新时,请保存位置。下次您申请并获得新位置时,您将拥有旧位置以与之进行比较。