【问题标题】:How to send GPS cordinates and current moving speed to phone db with timestamp如何使用时间戳将 GPS 坐标和当前移动速度发送到手机数据库
【发布时间】:2017-07-04 12:15:00
【问题描述】:

我是 Android 新手。我尝试了几种将 GPS 坐标存储在手机数据库中的方法。

现在的问题是我想每隔 x 秒存储一次 GPS 坐标以及当前移动的车辆速度和时间。

这个函数也应该在按钮点击事件之后启动。

【问题讨论】:

标签: android gps


【解决方案1】:

您只需要创建一个后台服务,该服务将在每 x 秒内连续获取 GPS 坐标。为此创建服务的主要原因是当您的应用程序不可见并且它被最小化时,您的服务将继续运行,并且在应用程序的后台,GPS 坐标将定期更新。

为了实现,您需要一个 FusedLocationAPI 和 GooglAPiClient。如果您想要一个非常简单的代码,请点击此链接: Android make method run every X seconds (involves gps and server call)

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • 感谢@Dwhitz 的建议,我一定会在回答下一个问题时考虑您的建议。
  • 非常感谢您的回答@Pranay Soni
【解决方案2】:

创建一个服务并启动一个计时器,如下所示。希望这会有所帮助

 public void starttimer(){
   timer = new CountDownTimer(300000, 2000) {
    @Override
    public void onTick(long millisUntilFinished) {
           locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    try {
        location = locationManager.getLastKnownLocation(provider);
    }
    catch (Exception e)
    {

    }
    if(location==null) {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }

    if (location != null) {
        double lat = location.getLatitude();
        double longg = location.getLongitude();
        mMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(lat,longg) , 14.0f) );


    } else {
        Toast.makeText(MainActivity.this, "Device Failed To Fetch Lat Long", Toast.LENGTH_LONG).show();
    }
    }


    @Override
    public void onFinish() {
        //your code when timer finishes
    }
}.start();
  }

【讨论】:

  • 非常感谢@shivadeep 的帮助
  • 我需要存储gps坐标与车辆当前的移动速度和时间
  • 我不确定速度,但您可以通过将坐标写入 txt 文件来存储坐标
猜你喜欢
  • 2012-04-23
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
相关资源
最近更新 更多