葛亭速度从GPS Android上计算距离和时间 [英] Geting Speed From GPS on Android Calculating Distance And Time

查看:220
本文介绍了葛亭速度从GPS Android上计算距离和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图通过全球定位系统的帮助来获得速度。我想获得DATAS全球定位系统(经纬度,速度)时,我的电话掉了下来。

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 DATAS并发送至服务器它们。

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
    }
}

我是在运动中试着我的程序在车上。但我无法得到正确的速度DATAS。例如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()) 检查这个职位的一个很好的例子(即解决了很多问题,对我来说):<一href="http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a">What是获得在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 in 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.

希望它可以帮助

这篇关于葛亭速度从GPS Android上计算距离和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆