我在 android 中借助 gps 编写了以下用于显示距离的 java 文件。它在 toast 中显示距离,但我想以 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;
}
}
请帮助我提前谢谢