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