title: Calculating Speed for a navigation app without getSpeed() method
body: <p>I am developing an app which is more of a time-shift racing between your friends.</p>
  
  <p>I need to calculate speed of a moving vehicle, and I don't want to use <code>Location.getSpeed()</code> method. (Explained in detail in the bottom why I don't want to use it)</p>
  
  <p>I am trying to calculate speed with the help of Latitude and Longitude available to me, and this is where I need help.</p>
  
  <p><strong>The help needed:</strong> I would want to know is:</p>
  
  <ul>
  <li>If the algorithm is correct</li>
  <li>Should I calculate in Centimeters instead of meters</li>
  <li>And if there's any code/library already available which does it.</li>
  </ul>
  
  <p>I am using the following code:</p>
  
  <p><strong>This gives me distance between two LatLng points:</strong></p>
  
  <pre><code>long getDistanceBetweenPoints(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;
      }
  </code></pre>
  
  <p><strong>And the following code is how it is being used:</strong></p>
  
  <pre><code>if(lastLat == -1 &amp;&amp; lastLng == -1){
      lastLat = location.getLatitude();
      lastLng = location.getLongitude();
      lastTimeStamp = location.getTime();
      return;
  }
  long distanceInMeters = getDistanceBetweenPointsAndSetTotal(lastLat, lastLng, location.getLatitude(), location.getLongitude());
  long timeDelta = (location.getTime() - lastTimeStamp)/1000;
  long speed = 0;
  if(timeDelta &gt; 0){
  speed = (distanceInMeters/timeDelta);
  }
  Log.d("Calculations","Distance: "+distanceInMeters+", TimeDelta: "+timeDelta+" seconds"+",speed: "+speed+" Accuracy: "+location.getAccuracy());
  
  lastLat = location.getLatitude();
  lastLng = location.getLongitude();
  lastTimeStamp = location.getTime();
  </code></pre>
  
  <p>When I run it, I get following output from that LogCat:</p>
  
  <pre><code>Distance: 0, TimeDelta: 0 seconds,speed: 0 Accuracy: 5.0
  </code></pre>
  
  <p><strong>Detailed Reasons</strong><br>
  The target consumers are not supposed to have high quality devices with high-quality GPS chips, thus always getting a very accurate fix when the device is on the move is not possible.</p>
  
  <p>I thus don't want to depend on the <code>Location.getSpeed()</code> method, since I have observed it gives out speed values only when the accuracy is in the range of 5~8 metres.</p>
  
  <p>The normal accuracy ranges I am getting in general circumstances is 10-15 metres, and <code>getSpeed()</code> doesn't give any speed. Even <code>hasSpeed()</code> starts returning false.</p>
  
  <p>I have been tinkering my head around this thing for more than 3 days, any help in this would be deeply appreciated.</p>
  
  <p>Much Thanks in Advance!</p>