我需要在自行车移动时使用GPS在Android中计算距离。而我在仿真器中为aprx提供纬度和经度值,给出正确的值,但是通过使用设备的数据包数据并且运行应用程序时,值会随着任何移动而改变。这是代码,请建议。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
showCurrentLocation();
b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clearButton();
}
});
}
@SuppressWarnings("unused")
protected void showCurrentLocation() {
// Getting LocationManager object
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Creating an empty criteria object
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
provider = locationManager.getBestProvider(criteria, false);
if (provider != null && !provider.equals("")) {
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 1, this);
clat = location.getLatitude();
clon = location.getLongitude();
a[0] = clat;
a[1] = clon;
if (location != null)
onLocationChanged(location);
else
Toast.makeText(getBaseContext(), "Location can't be retrieved",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "No Provider Found",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onLocationChanged(Location location) {
// Getting reference to TextView tv_longitude
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Creating an empty criteria object
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
provider = locationManager.getBestProvider(criteria, false);
if (provider != null && !provider.equals("")) {
// Get the location from the given provider
Location location1 = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 1, this);
plat = location.getLatitude();
plon = location.getLongitude();
a[2] = plat;
a[3] = plon;
if(a[0]!=a[2]||a[1]!=a[3])
{
dis += getDistance(a);
}
a[0] = plat;
a[1] = plon;
Log.e("aa", String.valueOf(dis));
TextView tvDistance = (TextView) findViewById(R.id.textView1);
tvDistance.setText(String.valueOf(dis) + " km");
}
}
public double getDistance(double a[]) {
double earthRadius = 6371; //kilometers
double dLat = Math.toRadians(a[2] -a[0] );
double dLng = Math.toRadians(a[3] -a[1]);
double b = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(a[0])) * Math.cos(Math.toRadians(a[2])) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(b), Math.sqrt(1-b));
float dist = (float) (earthRadius * c);
return dist;
}
protected void clearButton(){
dis=0;
showCurrentLocation();
}
答案 0 :(得分:0)
你可以做一个步骤并且当位置不同时调用getDistance,但是当经过的时间与自行车速度(1s?)相关时。
private long timestamp = 0;
public double getDistance(double a[]) {
timestamp = System.currentTimeMillis();
(....)
}
public void onLocationChanged(Location location) {
(...)
if(System.currentTimeMillis() - timestamp > 1000){
dis += getDistance(a);
}