Geting Speed From GPS on Android Calculating Distance And Time(从 Android 上的 GPS 获取速度计算距离和时间)
问题描述
我一直在尝试借助 GPS 来提高速度.当我的手机摔倒时,我想获取 Gps 数据(纬度-经度-速度).
I have been trying to get speed by the help of GPS. I would like to get Gps datas(latitude-longitude-speed) when my telephone fell down.
当手机摔倒时,获取gps数据并发送到我的服务器.为此,我使用了线程.每个线程获取 gps 数据并将其发送到服务器.
When telephone fell down, gps datas are obtained and sent to my server. For this aim, I used threads. Each thread get gps datas and send to server them.
我的问题是速度值.我得到了速度计算两个位置和时间之间的距离(速度=距离/时间)
My problem is speed value. I got speed calculating distance between two location and time (speed = distance / time)
线程制作:
@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 公里/小时.
I tried my program in the car that in motion. But I couldn't get correct speed datas.. For example 7.31365 , 8.29663 etc.. They should be 50- 60 or 70 km/h..
你有什么想法?
推荐答案
我想知道你的实现是否正确.我认为问题在于位置更新未连接到新位置的修复.因此,如果您的 gps 没有进行新的修复,则将再次发送当前位置.
I am wondering if your implementation is correct. I think the problem is that the location updates are not connected to the fix of a new location. So if your gps did not make a new fix, then the current location will be sent again.
一个逻辑可能是每 X 秒,您打开 requestLocationUpdates() 处理位置,如果/当它收到并关闭更新 (removeUpdates())查看这篇文章以获取一个很好的示例(为我解决了许多问题):在 Android 上获取用户当前位置的最简单、最可靠的方法是什么?
A logic could be that every X seconds, you turn on your requestLocationUpdates() process the location if/when it is received and turn off updates (removeUpdates()) Check this post for an excellent example (that solved many problems for me): What is the simplest and most robust way to get the user's current location on Android?
上面的例子很容易理解.棘手的部分是在服务中使用它,因为当手机进入睡眠模式时服务不起作用.在这里,您可以使用 commonsware 组件 来克服这个问题.
The example above is clear to understand. The tricky part is to use it in a service as a service does not work when the phone goes on sleep mode. Here you can use a commonsware component to overcome this.
我已经使用并混合了这两个组件来设置定位服务,它很稳定.
I have used and mixed those two components to set up a location service and it is stable.
希望对你有帮助
这篇关于从 Android 上的 GPS 获取速度计算距离和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 Android 上的 GPS 获取速度计算距离和时间
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01