why getSpeed() always return 0 on android

前端 未结 11 1271
半阙折子戏
半阙折子戏 2020-11-28 07:35

I need to get the speed and heading from the gps. However the only number i have from location.getSpeed() is 0 or sometimes not available. my code:

         


        
11条回答
  •  隐瞒了意图╮
    2020-11-28 08:16

    I basicaslly calculate the instantaneous speed and then use the setSpeed() method to add it in the location. Its pretty accurate because I compared it inside a vehicle where I could check the tachymeter.

    private double calculateInstantaneousSpeed(Location location) {
    
    
    
        double insSpeed = 0;
        if (y1 == null && x1 <= -1) {
            //mark the location y1 at time x1
            y1 = location;
            x1 = duration.getDurationAsSeconds();
    
    
        } else {
             //mark the location y2 at time x2
            y2 = location;
            x2 = duration.getDurationAsSeconds();
    
    
            //calculate the slope of the curve (instantaneous speed)
            dy = y1.distanceTo(y2);
            dx = x2 - x1;
    
            insSpeed = dy / dx;
    
            y1 = y2;
            x1 = x2;
    
        }
    
        Singleton.getInstance().instantaneousSpeedSamples.add(insSpeed);
        //System.out.println("Instantaneous Speed m/s: "+insSpeed);
        return insSpeed;
    }
    

提交回复
热议问题