0

**i have used the gps coordinates for calculating the distance using location updates. but problem is that if i am standing at same place the coordinates changes and distance is changing with out moving anywhere. I want to change it only when I have moved min 10 mtrs.

## 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

}****
4

3 回答 3

0

我应该看到你正在编写的代码。但如果没有它,我可以说有一个你调用的函数,它每 X 毫秒为你提供一次位置更新。或者您可以获得最新更新的位置,我应该再次看到您的代码。

更新:

更改此行

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 0.0f, this);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 10.0f, this);

移动 10m 后您将收到更新。

第二个参数是以毫秒为单位的时间,第三个是最小距离。但这并不意味着您将在请求的时间或请求的距离准确地获得更新,而是关于它。

于 2013-01-24T09:14:02.547 回答
0

//在类上初始化的先前速度的公共或类变量。// 浮动上一个速度;

public void onLocationChanged(Location location) {
   // TODO Auto-generated method stub

   new_latitude=location.getLatitude();
   new_longitude=location.getLongitude();

   float speed = location.getSpeed();
   //You could also use location.hasSpeed(), which is a boolean, but it almost 
   //always returns True

   if(speed > DESIRED_LIMIT && previousSpeed > DESIRED_LIMIT){

      lat1=new_latitude;
      lon1=new_longitude;

   }

   previousSpeed = speed;
}

所以类似的东西。这种情况没有得到很好的优化或设计。但这就是您从位置对象获取 GPS 速度的方式。您可以调整速度限制或整个条件以获得最佳结果。或者添加更多的速度历史来监控。

于 2013-01-25T06:11:42.470 回答
-1

您可以使用来自 android 市场的位置欺骗应用程序,并指定欺骗位置的持续时间以及距离

于 2013-01-24T09:14:23.360 回答