How to calculate distance between two locations using their longitude and latitude value

formula to calculate distance between two latitude and longitude
how to calculate distance between two latitude and longitude in excel
latitude/longitude distance calculator
distance between two longitudes
calculate distance between two gps coordinates formula
calculate distance between two coordinates latitude longitude python
how to calculate distance between two latitude and longitude in android
calculate bearing between two coordinates latitude/longitude

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

public class MyLocationListener implements LocationListener {

  public void onLocationChanged(Location loc)
    String Text = "My current location is: " +"Latitud = "+ loc.getLatitude() +"Longitud = " + loc.getLongitude();

    //System.out.println("Lat & Lang form Loc"+Text);
    //Toast.makeText( getApplicationContext(), Text,Toast.LENGTH_SHORT).show();

  public void onProviderDisabled(String provider)

  public void onProviderEnabled(String provider)

  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;

Here getting distance in kilometers (km)

private double distance(double lat1, double lon1, double lat2, double lon2) {
    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;
    return (dist);

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

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

Calculate distance and bearing between two Latitude/Longitude , Enter latitude and longitude of two points, select the desired units: nautical Important Note: The distance calculator on this page is provided for informational purposes only. Click here to find your latitude/longitude (used with permission). The shortest distance along the surface of an ellipsoid between two points on the surface is along the geodesic. Vincenty Algorithm is used to calculate this distance. To calculate the distance between two points there are two popular algorithm Haversine and Geodesic distance is used:

There is an android.location.Location.distanceBetween() method which does this quite well.

Android Developer Docs: Location

Latitude/Longitude Distance Calculator, This link might be helpful to you, as it details the use of the Haversine formula to calculate the distance. Excerpt: This script [in Javascript] calculates great-circle  'OUTPUT: Distance between the ' two points in Meters. ' '===== ' Calculate geodesic distance (in m) ' between two points specified by ' latitude/longitude (in numeric ' [decimal] degrees) ' using Vincenty inverse formula ' for ellipsoids '===== ' Code has been ported by lost_species ' from to VBA ' from javascript

Try this code.

startPoint.distanceTo(endPoint) function returns the distance between those places in meters.

Location startPoint=new Location("locationA");

Location endPoint=new Location("locationA");

double distance=startPoint.distanceTo(endPoint);

here "distance" is our required result in Meters. I hope it will work for android.

Calculate distance between two latitude-longitude points , But, if you insist on using this one, you are free to use the code in your own function to calculate the great circle distance between two points on the Earth. To calculate the air distance between two places, you need to create a table containing columns with: site names, latitude and longitude of both locations. Repeat this step and create another field named Long (for longitude). For the maximum distance of 200 miles that one might see a peak, the difference is 0.

in build.gradle:

compile ''

and then:

public static Double distanceBetween(LatLng point1, LatLng point2) {
    if (point1 == null || point2 == null) {
        return null;

    return SphericalUtil.computeDistanceBetween(point1, point2);

Finding distances based on Latitude and Longitude, Using Longitude and Latitude to Determine Distance: I've been looking for the equation for finding the distance between two cities, given their latitudes and  Latitude and longitude are used to determine one's exact position on Earth relative to two reference lines: the equator circling the planet horizontally (east-west) and a vertical line called the prime meridian circling it vertically. The distance between latitudes is about 69.5 miles.

If you have two Location Objects Location loc1 and Location loc2 you do

float distance = loc1.distanceTo(loc2);

If you have longitude and latitude values you use the static distanceBetween() function

    float[] results = new float[1];
    Location.distanceBetween(startLatitude, startLongitude,
    endLatitude, endLongitude, results);
    float distance = results[0];

Ask Dr. Math Archives: Latitude and Longitude, In this post, we show the formula to calculate the shortest distance between two points using Latitude and Longitude. This calculation Latitude and Longitude in Excel: Calculate Distance, Convert Degrees, and Geocode Addresses. If you have a long list of geographic coordinates to work with, a Microsoft Excel spreadsheet is sure to be useful. There are three basic Excel tools that can work for you, no matter how you want to manipulate your geographic coordinates.

Calculate Distance Between Two Points Using Latitude and Longitude, Most of us are more accustomed to seeing latitude and longitude simply as but one of their most common uses is measuring distances between points. For instance, an airline that has to fly between two locations with a  Calculate Distance between two location with Latitude Longitude using PHP. Calculate Distance between two location with Latitude Longitude using PHP. The Debuggers.

Latitude and Longitude Distance Calculation Explained l Sisense, This routine calculates the distance between two points (given the ::: //::: latitude/​longitude of those points). It is being used to calculate ::: //::: the distance  Input coordinates of two points: Starting latitude: 23.5 Ending longitude: 67.5 Starting latitude: 25.5 Ending longitude: 69.5 The distance is 300.67km. Visualize Python code execution: The following tool visualize what the computer is doing step-by-step as it executes the said program: Online Python Tutor - iframe embed page.

Calculate Distance by Latitude and Longitude using JavaScript , This query will determine the distance between two points on the earth given their latitudes and longitudes. Valid input formats are at the bottom of this page. Calculating the distance between two point is quite straightforward with the distm function from the geosphere package: distm(p1, p2, fun = distHaversine) where: p1 = longitude/latitude for point(s) p2 = longitude/latitude for point(s) # type of distance calculation fun = distCosine / distHaversine / distVincentySphere / distVincentyEllipsoid

  • can we add distance to latlng to get distance latng
  • It would be nice if you added Units in your answer.
  • i think this output units is Miles, you can convert as kilometers with this formula-> km =mi / 0.62137;
  • why multiplying by 60 and 1.1515 --> dist = dist * 60 * 1.1515;
  • Best Distance calculation code found in whole stack. Very Accurate. But please explain each step. Can't understand what you did. Thanks for Share!.
  • is this aerial or onroad distance ?
  • Probably Aerial
  • Definitely aerial :)
  • Heyy @sandeepmaaram I'm using "SphericalUtil.computeDistanceBetween(from, to);" to calculate distance, but the thing is, as i print that distance in log it gives me 13047. Also the actual distance is 15.6 km. So I'm confused that what are that numbers?? If its the distance then how does it calculates.
  • SphericalUtil is this API provided by java/android? did you try with my solution?
  • Thankss a lot @sandeepmaaram I tried ur solution nd ur answer gives the exact location i want. Nd yes SphericalUtil api is provided by android.
  • In this method distanceBetween() what is the LatLng? I tried to pass longitude latitude double value but it is not supporting.