我在android中使用gps帮助显示以下java文件的显示距离。它显示了吐司中的距离,但我想以km显示它,我该怎么办?
package com.dansaurabh.speedone;
import android.app.Service;
import android.content.Intent;
import android.location.GpsStatus.Listener;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class gpslocation extends Service {
public final static String key1 = "com.saurabh.speedone.mes1";
LocationManager locationManager;
Location currentLoc;
// Double par1,par2,par3,par4;
private double par1 = 0;
private double par2 = 0;
private double currentlong = 0;
private double currentlat = 0;
String distance;
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.d("LocationUpdateService", "onCreate is called");
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
/* ################ */
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
/* ################ */
Log.d("LocationUpdateService", "onStartCommand is called");
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ==
false) {
// put dialoug box of start gps service
// getNetworkLocation();
// add dilouge for start gps
Toast.makeText(this, "start GPS service",
Toast.LENGTH_LONG).show();
} else {
getGPSLocation();
}
return super.onStartCommand(intent, flags, startId);
}
/*
* private void getNetworkLocation(){ Log.d("LocationUpdateService",
* "getNetworkLocation is called"); currentLoc =
* locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
*
* String data = "Network : " + currentLoc.getLatitude() + " :: "
* +currentLoc.getLongitude(); // updateUI(data); } // above tells about
* last known location
*/
private void getGPSLocation() {
/*
* Log.d("LocationUpdateService", "getGPSLocation is called");
* currentLoc =
* locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
* if(currentLoc!=null){ String data = "getLastKnownLocation : GPS : " +
* currentLoc.getLatitude() + " :: " +currentLoc.getLongitude(); //
* updateUI(data);
*
*
* }else{ updateUI("No GPS Current Loc found"); }
*/
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new LocationListener() {
public void onStatusChanged(String provider, int
status,
Bundle extras) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
// updateUI(provider + " Enabled");
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
// updateUI(provider + " Disabled");
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
currentLoc = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (currentLoc != null) {
currentlat =
currentLoc.getLatitude();
currentlong =
currentLoc.getLongitude();
distance();
// in km
}
else {
Toast.makeText(gpslocation.this, "no location changed", Toast.LENGTH_LONG).show();
}
}
public void distance() {
// measure distance
double earthRadius = 3958.75;
double dLat = Math.toRadians(par1 -
currentlat);
double dLng = Math.toRadians(par2 -
currentlong);
double a = Math.sin(dLat / 2) *
Math.sin(dLat / 2)
+
Math.cos(Math.toRadians(currentlat))
*
Math.cos(Math.toRadians(par1))
* Math.sin(dLng / 2) *
Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a),
Math.sqrt(1 - a));
double dist = earthRadius * c;
Intent it = new Intent(gpslocation.this,
Run.class);
it.putExtra(key1, distance);
startActivity(it);
}
});
}
@Override
public void onDestroy() {
// updateUI("onDestroy is called");
Toast.makeText(this, "app closeing", Toast.LENGTH_LONG).show();
Intent it = new Intent(gpslocation.this, Run.class);
it.putExtra(key1, distance);
startActivity(it);
super.onDestroy();
}
private void updateUI(CharSequence dist) {
Log.i("LocationUpdateService,distance", "Data :: " + dist);
Toast.makeText(this, dist, Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
PLZ帮我提前谢谢
答案 0 :(得分:0)
如果我理解你的问题,那就简单了:
private void updateUI(CharSequence dist) {
Log.i("LocationUpdateService,distance", "Data :: " + dist);
Toast.makeText(this, dist.toString() + " km", Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
提供有意义的变量名称。什么是par1
和par2
?我敢打赌,从现在起乍一看你也不会知道。 看起来像你用它来计算距离(定义为0而不是再次设置),因此你正在计算到北极的距离(对不起,不要浪费太多时间分析应该简化的代码。)
在Android中,如果可以,请使用Location
对象,而不是原始double
坐标。
长距离(甚至是小距离)的计算是一件棘手的事情。在你之前,不要假装你能比别人做得更好。正如AlexWien在我面前的答案中所说的那样,找到一个合适的公式。或者,更好,使用内置的Location.distanceTo(Location),它以米的形式返回到另一个点的距离。对于自定义(? - 不确定Android API在这方面是多么可扩展,我敢打赌它是完全的),网上有libraries也可以使用。
如果这是我,我会提供我的内置引用Location
(例如,Location myDestination
),检索当前Location
(例如,Location currentLocation
) ,并使用currentLocation.distanceTo(myDestination)
。然后,我可能会转换并舍入这个值,以公里(或英里,对于你的盎格鲁 - 撒克逊人)提供一个不错的输出,使用Java中众所周知的方法来舍入数字以获得良好的输出。
很多更简单。 :)