Search code examples
androidfusedlocationproviderapiandroid-fusedlocation

Android - Fused API getSpeed()


By using fused API

  1. I'm getting speed as 3.0 something. But I am in the same place and am not traveling anywhere, so from where is the speed coming from? I am testing with a Samsung device and a Motorola device. The problem is appearing on the Motorola device.

  2. I'm setting the interval with: mLocationRequest.setInterval(1000 * 60); But the problem shows up on the Motorola device before 60 seconds which is updating (approximately at 30 second)


Solution

  • Calculate like this,

    1. Find distance between two points;

       private double distanceCalc(double lat1, double lon1, double lat2, double lon2, char unit) {
          if (lat1 == lat2 && lon1 == lon2) {
              return 0.0;
          } else {
              double theta = lon1 - lon2;
              double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
              dist = Math.acos(dist);
              dist = rad2deg(dist);
              dist = dist * 60 * 1.1515;
              if (unit == 'K') {
                  dist = dist * 1.609344;
              } else if (unit == 'N') {
                  dist = dist * 0.8684;
              }
              return (dist);
          }
      
      }
      

    calculating as per the unit of kilometer.

     private double deg2rad(double deg) {
            return (deg * Math.PI / 180.0);
        }
    

    This function converts decimal degrees to radians.

    private double rad2deg(double rad) {
            return (rad * 180.0 / Math.PI);
        }
    

    This function converts radians to decimal degrees.

    1. To get speed,Calculate time difference between both and convert in to hours and speed as distance/time.Here i have done with km/h.