2
海我需要在自行车正在移动时使用GPS在android中计算距离。同时我给模拟器中的经纬度值提供正确的值,但通过使用设备的分组数据并运行应用程序,这些值不会发生任何变化。这是代码,请建议。计算在android中移动的距离(移动时)
@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();
}
嗨,您正在使用getLastKnownLocation()。有一段时间它会给出无效/错误的位置对,因为它会从最后更新的地方获取......并且更好的方法是使用googleplayservices库。它会帮助你获得移动速度和准确的位置。您可以使用谷歌距离api找到当前和指定位置之间的距离 – Sivakumar 2014-09-25 08:54:43
googleplayservices lib防止部署到诺基亚,亚马逊和其他不是谷歌Android设备。我想知道你所说的bug是否是g所要求的。 – Benoit 2014-09-25 09:03:26
@akhila我面临同样的问题。你有解决方案吗?.. – 2016-01-21 10:55:16