如何在安卓应用程序中使用位置或加速度计或其他方式获取速度



我正在开发应用程序,并试图获得用户行驶的速度和距离。我使用Google Play服务的位置类来获得速度,但它总是给我返回0.0的值,而且根本不可靠。我希望实时准确地行驶速度和距离。

我在我的设备上安装了GPS测速仪应用程序,它非常完美,即使我在走路,它也会给我速度。我也想得到同样的东西。我对如何获得速度、使用位置或使用加速度计感到困惑,或者有其他方法吗?

我的代码在这个链接上可用:-

使用谷歌地图在谷歌地图上绘制路线Android API v2

我正在开发纯基于位置的应用程序,其中包括地图、速度和其他与位置相关的内容。

如果有人有任何想法,请帮助我解决速度和距离的问题。

我不得不处理同样的问题,您所能做的就是使用Location Strategies代码。

然后,每次更新位置时,都会保存当前更新的时间。因此,您将有以前和现在的位置,以及更新的时间。

然后计算这两个位置(新旧位置)之间的距离(以米为单位)

private static long calculateDistance(double lat1, double lng1, double lat2, double lng2) {
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lng2 - lng1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
            * Math.sin(dLon / 2);
    double c = 2 * Math.asin(Math.sqrt(a));
    long distanceInMeters = Math.round(6371000 * c);
    return distanceInMeters;
}

所以,如果你有距离和时差,我认为获得速度不会有什么大不了的。

我在使用Google Play Location API时也遇到了这个问题,我希望这能有所帮助。

它返回0,因为您的设备无法锁定GPS,或者无法连接到GPS。

我试着用一个旧的联想设备来获取速度,但它返回0,因为它无法锁定gps。

我试着使用三星galaxy nexus,它恢复了我的速度(有一个更好的GPS传感器)。

你手机中的GPS传感器可能不好,或者你所在的区域GPS信号较弱,比如房子或建筑物内。

我所做的是,如果location.hasSpeed为假,则计算速度,如果location.hasSpeed为真,则使用location.getSpeed。

我还试图在计算时使用活动识别来提高速度的准确性。

    //Computation for speed
    cur_time = System.currentTimeMillis() / 1000L;
    if (location.hasSpeed()) {
        loc_Speed = location.getSpeed();
        //counter goes back to 0
        count_OnFoot = 0;
        Toast.makeText(getBaseContext(), "has speed", Toast.LENGTH_SHORT).show();
    } else {
        float[] result = new float[1];
        location.distanceBetween(prev_latitude, prev_longitude,
                loc_Latitude, loc_Longitude,
                result);
        float loc_distance = result[0];
        //speed if location.getSpeed is null
        loc_Speed = loc_distance/(cur_time - prev_time);
        //if activity type is on foot
        //estimate AVE_RUNNING_SPEED = 3m/s
        if (act_ActivityType.equals(ActivityRecognitionService.ACT_ON_FOOT) && loc_Speed > AVE_RUNNING_SPEED) {
            count_OnFoot++;
            if (count_OnFoot < 2) {
                Toast.makeText(getBaseContext(), "on foot and 1st > 3m/s", Toast.LENGTH_SHORT).show();
                /*
                 * Since the speed is more than
                 * the average running speed,we will
                 * assume that its not a correct value
                 * and a fault that it detected a very far signal from the previous one.
                 * (This happens sometimes)
                 * We will assign the previous speed
                 * as its current speed.
                 */
                loc_Speed = prev_Speed;
            } else {
                Toast.makeText(getBaseContext(), "on foot and TWICE > 3m/s in a row", Toast.LENGTH_SHORT).show();
                /*
                 * Do Nothing
                 * loc_Speed is equals the computed speed 
                 * if it happens twice or more in a row.
                 * We will assume that its running fast.
                 */
            }
        }else {
            count_OnFoot = 0;
        }
    }
    prev_Speed = loc_Speed;
    /*
     * If more than 60% sure that its still.
     * 
     * Let your speed and direction be 0
     * latitude and longitude should not change
     */
    if (act_ActivityType.equals(ActivityRecognitionService.ACT_STILL)) {
        loc_Speed = 0;
        loc_Direction = 0;
        if (prev_latitude != 0 && prev_longitude != 0) {
            loc_Latitude = prev_latitude;
            loc_Longitude = prev_longitude;
        }
    }
    prev_time = cur_time;
    prev_latitude = loc_Latitude;
    prev_longitude = loc_Longitude;
    //Note: My activity type will return on foot or still if its more than 60% sure
    //otherwise null.

在这个答案中,我将向您展示两条获取当前速度的主要路径。一条是使用定位服务(location.getSpeed()),另一条是老式的手动计算速度版本。

首先定义三个主要的全局变量

double curTime= 0;
double oldLat = 0.0;
double oldLon = 0.0;

现在转到onLocationChanged方法,并在其内部调用此方法

getspeed(location);

现在让我们实现getSpeed方法

private void getspeed(Location location){
    double newTime= System.currentTimeMillis();
    double newLat = location.getLatitude();
    double newLon = location.getLongitude();
    if(location.hasSpeed()){
        float speed = location.getSpeed();
        Toast.makeText(getApplication(),"SPEED : "+String.valueOf(speed)+"m/s",Toast.LENGTH_SHORT).show();
    } else {
        double distance = calculationBydistance(newLat,newLon,oldLat,oldLon);
        double timeDifferent = newTime - curTime;
        double speed = distance/timeDifferent;
        curTime = newTime;
        oldLat = newLat;
        oldLon = newLon;
        Toast.makeText(getApplication(),"SPEED 2 : "+String.valueOf(speed)+"m/s",Toast.LENGTH_SHORT).show();
    }
}

好的,我们已经完成了,现在实现计算Bydistance方法

public double calculationBydistance(double lat1, double lon1, double lat2, double lon2){
    double radius = EARTH_RADIUS;
    double dLat = Math.toRadians(lat2-lat1);
    double dLon = Math.toRadians(lon2-lon1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                    Math.sin(dLon/2) * Math.sin(dLon/2);
    double c = 2 * Math.asin(Math.sqrt(a));
    return radius * c;
}

在其他部分,速度将以米/毫秒为单位,你可以将其转换为秒。。。我们完成了

public static double getSpeed(Location currentLocation, Location oldLocation)
{
    double newLat = currentLocation.getLatitude();
    double newLon = currentLocation.getLongitude();
    double oldLat = oldLocation.getLatitude();
    double oldLon = oldLocation.getLongitude();
    if(currentLocation.hasSpeed()){
        return currentLocation.getSpeed();
    } else {
        double radius = 6371000;
        double dLat = Math.toRadians(newLat-oldLat);
        double dLon = Math.toRadians(newLon-oldLon);
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(Math.toRadians(newLat)) * Math.cos(Math.toRadians(oldLat)) *
                        Math.sin(dLon/2) * Math.sin(dLon/2);
        double c = 2 * Math.asin(Math.sqrt(a));
        double distance =  Math.round(radius * c);
        double timeDifferent = currentLocation.getTime() - oldLocation.getTime();
        return distance/timeDifferent;
    }
}

最新更新