【问题标题】:GPS location acts like it is stored in array and sent one by one on each requestGPS 位置就像它存储在数组中一样,并在每个请求中一一发送
【发布时间】:2014-03-25 15:05:22
【问题描述】:

我正在构建一个应用程序,它会定期将 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");
}
}

【问题讨论】:

    标签: android android-intent gps


    【解决方案1】:

    那个时间是来自该位置的时间戳还是在您收到它时捕获的?所有 Location 对象都保留一个时间戳以及自系统启动以来接收到的持续时间(以毫秒和纳秒为单位)。在 OnLocationChanged 中,您可以检查这一点并确保在发送之前位置不是太旧。

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

    【讨论】:

    • 那么它似乎是你的服务器或发送的东西搞砸了。如果您查看位置 developer.android.com/reference/android/location/Location.html 的 Android 文档,您会发现我正在谈论的功能。在这种情况下,我要做的是向服务器发送一条消息,该消息带有您获得的 Location 对象的时间。这确保了在服务器上实际显示位置的时间。但请注意,该位置可能与用户实际所在的位置不匹配,因此请确保在发送前检查其年龄。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 2017-01-03
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多