GPS位置就像存储在阵列中一样,并在每个请求上逐个发送

时间:2014-03-25 15:05:23

标签: android android-intent gps

我正在构建一个应用程序,它会定期向服务器发送GPS坐标并将其存储在数据库中。但是当在数据库中看到时,我发现gps实际上后来发送了错误的(旧的)坐标。

旧的意思是,假设我在车里旅行,

At 2:00PM the coordinates are 73.982749854638 and 99.65363535375 (suppose)
At 2:05PM the coordinates are 73.982749812343 and 99.12334567866

现在我关闭了应用程序,并再次在

运行它
6:00PM the coordiantes are 73.1234546789 and 99.123456789 

但是当我在下午6:00检查服务器数据库时,服务器实际上收到了下午2:05的协调

我不明白为什么会发生这种情况,下面是我的代码,

public class MainActivity extends Activity implements LocationListener{
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
protected SendLocation sendLoc;
protected DeviceInformation devInfo;
TextView txtLat, status;
Button regDevice;
ToggleButton toggBtn;
String lat, provider;
protected long minTime = 60000;
protected Double latt, lonn;
protected boolean toggle;
protected String latitude,longitude; 
protected boolean gps_enabled,network_enabled;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    devInfo = new DeviceInformation(MainActivity.this);
    txtLat = (TextView) findViewById(R.id.textview1);
    status = (TextView) findViewById(R.id.status);
    toggBtn = (ToggleButton) findViewById(R.id.toggleButton1);
    toggBtn.setText("Start Tracking!");
    toggBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if(toggBtn.isChecked()){
                status.setText("Tracking is Enabled!");
                toggBtn.setText("Stop Tracking!");
                status.setTextColor(Color.BLUE);
                toggle = true;
            }else{
                status.setText("Tracking is Disabled!");
                toggBtn.setText("Start Tracking!");
                status.setTextColor(Color.RED);
                toggle = false;
            }
        }
    });

    final Context context = this;
    regDevice = (Button) findViewById(R.id.regButton);

    regDevice.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View arg0) {
        Intent intent = new Intent(context, Register.class);
        startActivity(intent);
      }

    });

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, 0, this);
}
@Override
public void onLocationChanged(Location location) {
    latt = location.getLatitude();
    lonn = location.getLongitude();
    txtLat = (TextView) findViewById(R.id.textview1);

    if(toggle == true){
        txtLat.setText(latt+"\n"+lonn);
        String uniq = devInfo.imei;
        String aobj[] = new String[3];
        aobj[0]=Double.toString(latt);
        aobj[1]=Double.toString(lonn);
        aobj[2]=uniq;
        sendLoc = new SendLocation();
        sendLoc.execute(aobj);
    }
    else if(toggle == false){
        //Toast.makeText(getApplicationContext(), "Not Sending", Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onProviderDisabled(String provider) {
    Log.d("Latitude","disable");
    toggBtn.setEnabled(false);
    txtLat = (TextView) findViewById(R.id.textview1);
    txtLat.setText("GPS Disabled! Please Enable GPS.");
}

@Override
public void onProviderEnabled(String provider) {
    Log.d("Latitude","enable");
    toggBtn.setEnabled(true);
    txtLat = (TextView) findViewById(R.id.textview1);
    txtLat.setText("Click button below to start tracking.");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("Latitude","status");
}
}

1 个答案:

答案 0 :(得分:0)

那时候是来自该位置的timstamp还是收到它时捕获的timstamp?所有位置对象都保留时间戳和持续时间,自系统启动后以毫秒和纳秒为单位。在OnLocationChanged中,您可以检查此内容并确保在发送之前位置不会太旧。

编辑: 我重读了你的帖子,似乎问题也可能出在发送代码的位置。如果屏幕上输出的位置由您加上时间戳并发送到服务器,则服务器应该会收到与您在手机上相同的信息。您的发送代码可能会出现问题。