【发布时间】: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