Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 检查车辆位置是否改变_Android_Gps - Fatal编程技术网

Android 检查车辆位置是否改变

Android 检查车辆位置是否改变,android,gps,Android,Gps,我的应用程序是GPS跟踪应用程序。 . 应用程序在后台运行,每10秒检查一次位置是否发生变化。如果位置更改,应用程序将发送带有新位置的短信。如果不是,它将继续检查。 我的问题是如何在检测到当前位置的情况下使用最后一个位置进行检查。 如何使用(如果条件)查看最后一个位置是否仍然没有更改。 我想检查值是否相同(相同位置),以了解设备是否移动到另一个位置或仍在该位置 在update()中 主要活动 public class MainActivity extends Activity { private

我的应用程序是GPS跟踪应用程序。
.
应用程序在后台运行,每10秒检查一次位置是否发生变化。如果位置更改,应用程序将发送带有新位置的短信。如果不是,它将继续检查。
我的问题是如何在检测到当前位置的情况下使用最后一个位置进行检查。
如何使用(如果条件)查看最后一个位置是否仍然没有更改。
我想检查值是否相同(相同位置),以了解设备是否移动到另一个位置或仍在该位置 在update()中

主要活动

public class MainActivity extends Activity {
private Thread thread;
Button btnShowLocation;
double latitude,longitude,Ch1,Ch2;
final Handler handler = new Handler();
// GPSTracker class
GPSTracker gps;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Search();
    Ch1=gps.getLatitude();
    Ch2=gps.getLongitude();
    update();
}
private void update() {
(new Thread(new Runnable()
{

    @Override
    public void run()
    {
        while (!Thread.interrupted())
            try
            {
                Thread.sleep(20000);
                runOnUiThread(new Runnable() // start actions in UI thread
                {
                    double a=gps.getLatitude();
                    double  b=gps.getLongitude();
                    @Override
                    public void run()
                    {
                         if ( a==Ch1 && b==Ch2){
                            Toast.makeText(getApplicationContext(), "No Changes " ,Toast.LENGTH_LONG).show();
                        }
                        else
                            Search(); // this action have to be in UI thread
                    }
                });
            }
            catch (InterruptedException e)
            {
                // ooops
            }
    }
})).start(); // the while thread will start in BG thread

}
private void Search() {
    // TODO Auto-generated method stub

    // create class object
    gps = new GPSTracker(MainActivity.this);


    // check if GPS enabled     
    if(gps.canGetLocation()){

         latitude = gps.getLatitude();
         longitude = gps.getLongitude();

        String sms="http://maps.google.com/maps?q=" + latitude + "," + longitude + "&iwloc=A";
        String number = "012345";

        SmsManager smsManager =     SmsManager.getDefault();
        smsManager.sendTextMessage(number, null, sms, null, null);

        Toast.makeText(getApplicationContext(), "Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();

    }

    else{

        gps.showSettingsAlert();
    }

}

}

Location.distance-between
,谢谢,但我不想计算两个位置之间的距离。。我想检查值是否相同(相同位置)。要知道移动设备是否被移动到另一个位置,还是在该位置,如果前面的位置和当前位置之间的距离小于10米,你可以认为用户在同一个位置。它工作得很好,它帮助我学习一些新的东西,谢谢。
public class MainActivity extends Activity {
private Thread thread;
Button btnShowLocation;
double latitude,longitude,Ch1,Ch2;
final Handler handler = new Handler();
// GPSTracker class
GPSTracker gps;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Search();
    Ch1=gps.getLatitude();
    Ch2=gps.getLongitude();
    update();
}
private void update() {
(new Thread(new Runnable()
{

    @Override
    public void run()
    {
        while (!Thread.interrupted())
            try
            {
                Thread.sleep(20000);
                runOnUiThread(new Runnable() // start actions in UI thread
                {
                    double a=gps.getLatitude();
                    double  b=gps.getLongitude();
                    @Override
                    public void run()
                    {
                         if ( a==Ch1 && b==Ch2){
                            Toast.makeText(getApplicationContext(), "No Changes " ,Toast.LENGTH_LONG).show();
                        }
                        else
                            Search(); // this action have to be in UI thread
                    }
                });
            }
            catch (InterruptedException e)
            {
                // ooops
            }
    }
})).start(); // the while thread will start in BG thread

}
private void Search() {
    // TODO Auto-generated method stub

    // create class object
    gps = new GPSTracker(MainActivity.this);


    // check if GPS enabled     
    if(gps.canGetLocation()){

         latitude = gps.getLatitude();
         longitude = gps.getLongitude();

        String sms="http://maps.google.com/maps?q=" + latitude + "," + longitude + "&iwloc=A";
        String number = "012345";

        SmsManager smsManager =     SmsManager.getDefault();
        smsManager.sendTextMessage(number, null, sms, null, null);

        Toast.makeText(getApplicationContext(), "Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();

    }

    else{

        gps.showSettingsAlert();
    }

}

}
 Location locationA = new Location("point A");
 locationA.setLatitude(a);
 locationA.setLongitude(b);

 Location locationB = new Location("point B");
 locationB.setLatitude(Ch1);
 locationB.setLongitude(Ch2);

 float distance = locationA.distanceTo(locationB);