How to get speed in android app using Location or accelerometer or some other way

前端 未结 4 1391
孤城傲影
孤城傲影 2020-12-24 00:02

I am working on app and try to get the speed and distance travelled by the user. I have used Google Play services location class to get the speed but it always returns me 0.

相关标签:
4条回答
  • 2020-12-24 00:16

    I had to deal with same problem, what you can do is to use Location Strategies code.

    Then, on each update of location, you save the time of the current update. So, you will have the previous and current location, and time of update.

    Then you calculate the distance in meters between those two locations (the old and new one)

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

    So, then you have the distance and the time difference, I think it wouldn't be a big deal to get the speed.

    0 讨论(0)
  • 2020-12-24 00:30

    I also encountered this problem when I was using Google Play Location API, I hope this can help.

    It returns 0 because your device cannot get a lock on the GPS, or cannot connect to the GPS.

    I tried to get the speed using an older lenovo device and it returns 0 because it cannot lock on a gps.

    I tried using a samsung galaxy nexus and it returned my speed(has a better GPS sensor).

    The GPS sensor in your phone might not be good or you are in an area that has a weak GPS signal such as inside a house or building.

    What I did was compute for speed if location.hasSpeed is false and use location.getSpeed if location.hasSpeed is true.

    I also tried to use the activity recognition to have a better accuracy of speed when computing.

        //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.
    
    0 讨论(0)
  • 2020-12-24 00:33

    in this answer im gonna show you two main path to get current speed.one is by using location service (location.getSpeed()) and other one is old school manually calculate speed version.

    first define three main global variables

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

    now move on to your onLocationChannged method and inside it call to this method,

    getspeed(location);
    

    now lets implement getSpeed method

    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();
        }
    }
    

    ok we are done with it, and now implement calculationBydistance method

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

    in here else part the speed will comes in m/miliseconds you can convert it to seconds...and we are done

    0 讨论(0)
  • 2020-12-24 00:34
    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题