【问题标题】:Geting Speed From GPS on Android Calculating Distance And Time从 Android 上的 GPS 获取速度计算距离和时间
【发布时间】:2012-01-10 17:53:41
【问题描述】:

我一直在尝试借助 GPS 来提高速度。当我的手机摔倒时,我想获取 Gps 数据(纬度-经度-速度)。

当手机摔倒时,获取gps数据并发送到我的服务器。为此,我使用了线程。每个线程获取 gps 数据并将其发送到服务器。

我的问题是速度值。我得到了速度计算两个位置和时间之间的距离 (速度=距离/时间)

线程制作:

@Override
public void run() {

    Looper.prepare();

    float distance = 0.0f;
    float[] results = new float[1];
    Long longValue = new Long(11);
    double firstLatitude;
    double firstLongitude;
    long firstTime;
    float speedOfDevice = 0.0f;

    //for sendin gps datas to web server
    ReportLocation reportObj = new ReportLocation(this);

    locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);


    locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);

    //latitude, longitude and timeOffix is set in location listener
    firstLatitude = latitude;
    firstLongitude = longitude;
    firstTime = timeOfFix;

    // Waiting 1.5 second 
    try {
        Thread.sleep(1500);
    } catch (InterruptedException e) {
        e.printStackTrace();
        Toast.makeText(this, "Sleep exception", Toast.LENGTH_LONG).show();
    }


    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);


    if( timeOfFix != firstTime ) {
        Location.distanceBetween(firstLatitude, firstLongitude, latitude, longitude, results);
        distance = results[0];
        longValue = new Long(timeOfFix - firstTime );
        speedOfDevice = 3600 * distance / ( longValue.floatValue() ); 
    }

    try {

        reportObj.send( Double.toString(firstLatitude),  Double.toString(firstLongitude), Float.toString( speedOfDevice ) );

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        Toast.makeText(this, "Client protokol exception ", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Toast.makeText(this, "IO exception "+e.getMessage(), Toast.LENGTH_LONG).show();
    }


    handler.sendEmptyMessage(0);

    Looper.loop();
}

}


class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {

            locManager.removeUpdates(locListener); 

            latitude = location.getLatitude();
            longitude = location.getLongitude();
            timeOfFix = location.getTime();

        }  
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }
}

我在行驶的汽车中尝试了我的程序。但我无法获得正确的速度数据。例如 7.31365、8.29663 等。它们应该是 50-60 或 70 公里/小时。

你有什么想法?

【问题讨论】:

  • 返回的位置是否正确?
  • 是的,位置返回正确。但是 firstLatitude 和 latitude 或 firstLongitude 和 longitude 值有时可能相同。没看懂。。
  • 我想知道您的实现是否正确。侦听器应该通过回调返回调用服务/线程/活动(查看这篇文章:stackoverflow.com/questions/3145089/…)。另外,我认为当手机进入睡眠模式时它会停止工作

标签: android gps latitude-longitude


【解决方案1】:

我想知道您的实现是否正确。我认为问题在于位置更新未连接到新位置的修复。因此,如果您的 gps 没有进行新的修复,则将再次发送当前位置。

一个逻辑可能是每 X 秒,您打开 requestLocationUpdates() 处理位置,如果/当它收到并关闭更新 (removeUpdates()) 查看这篇文章以获得一个很好的例子(它为我解决了很多问题):What is the simplest and most robust way to get the user's current location on Android?

上面的例子很容易理解。 棘手的部分是在服务中使用它,因为当手机进入睡眠模式时服务不起作用。在这里,您可以使用commonsware component 来解决这个问题。

我已经使用并混合了这两个组件来设置位置服务,它很稳定。

希望对你有帮助

【讨论】:

  • 谢谢..我会分析你建议的例子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
  • 2012-01-19
  • 2014-02-28
相关资源
最近更新 更多