How to calculate distance between two locations using their longitude and latitude value
<p>Here my code i used below code to calculate the distance between two location using Their latitude and longitude. it is giving wrong distance. sometimes getting right and sometimes getting irrelevant distance. we are getting lat1 and lng1 from database.
//getting lat2 and lng2 from GPS as below</p>

<p>public class MyLocationListener implements LocationListener</p>

<pre><code>   {
    @Override
    public void onLocationChanged(Location loc)
    {
    lat2=loc.getLatitude();
    lng2=loc.getLongitude();
    String Text = "My current location is: " +"Latitud = "+ loc.getLatitude() +"Longitud = " + loc.getLongitude();

    //System.out.println("Lat &amp; Lang form Loc"+Text);
    //Toast.makeText( getApplicationContext(), Text,Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onProviderDisabled(String provider)
    {
    }
    @Override
    public void onProviderEnabled(String provider)
    {
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }

//Calculating distance
                       double earthRadius = 3958.75;

                    double dLat = Math.toRadians(lat1-lat2);
                    double dLng = Math.toRadians(lng1-lng2);
                    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                               Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(lat1)) *
                               Math.sin(dLng/2) * Math.sin(dLng/2);
                    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
                    double dist = earthRadius * c;
</code></pre>
