gpt4 book ai didi

android - 在 Android 上从 GPS 获取速度计算距离和时间

转载 作者:太空狗 更新时间:2023-10-29 15:20:00 25 4
gpt4 key购买 nike

我一直在尝试借助 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 公里/小时。

你有什么想法?

最佳答案

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

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

上面的例子很容易理解。棘手的部分是在服务中使用它,因为当手机进入休眠模式时服务不起作用。在这里你可以使用 commonsware component克服这一点。

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

希望对你有帮助

关于android - 在 Android 上从 GPS 获取速度计算距离和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8808004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com