从Android获取GPS速度计算距离和时间

时间:2012-01-10 17:53:42

标签: android gps latitude-longitude

我一直试图通过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公里/小时..

你有什么想法?

1 个答案:

答案 0 :(得分:0)

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

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

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

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

希望有所帮助

相关问题