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

Geting Speed From GPS on Android Calculating Distance And Time(从 Android 上的 GPS 获取速度计算距离和时间)

本文介绍了从 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 获取速度计算距离和时间

基础教程推荐