* *位置情報の更新を使用して距離を計算するために GPS 座標を使用しました。しかし問題は、同じ場所に立っていると座標が変わり、どこにも移動せずに距離が変わることです。10 メートル以上移動した場合にのみ変更したい。
## Activity ##
//Activity
public TextView dist;
public double lati1,lati2,longi1,longi2;
private boolean run1 = false;
private Handler handler1 = new Handler();
private Runnable task1 = new Runnable() {
public void run() {
// TODO Auto-generated method stub
if (run1) {
handler1.postDelayed(this,1000);
if(lati1==0.0&&longi1==0.0)
{
lati1=Service1.lat1;
longi1=Service1.lon1;
}
lati2=Service1.lat1;
longi2=Service1.lon1;
double R=63710;
double dLat=Math.toRadians(lati2-lati1);
double dLon=Math.toRadians(longi2-longi1);
double a=Math.sin(dLat/2)*Math.sin(dLat/2)+
Math.cos(Math.toRadians(lati1))*Math.cos(Math.toRadians(lati2))*
Math.sin(dLon/2)*Math.sin(dLon/2);
double c=2*Math.asin(Math.sqrt(a));
double d=R*c;
d=(double)(Math.round(d*100.00))/1000.00;
distance+=d;
String s=String.format("%.2f", distance);
dist.setText(s);
lati1 = lati2;
longi1 = longi2;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dist=(TextView)findViewById(R.id.distancetextView);
Intent i = new Intent(context, Service1.class);
startService(i);
}
## Service ##
//Service
private double new_latitude = 0.0;
private double new_longitude = 0.0;
public static double lat1=0.0,lon1=0.0;
LocationManager locationManager = null;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 0.0f, this);
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
locationManager.removeUpdates(this);
super.onDestroy();
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
new_latitude=location.getLatitude();
new_longitude=location.getLongitude();
lat1=new_latitude;
lon1=new_longitude;
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}****