tags:

views:

122

answers:

5

Possible Duplicates:
help with GPS position
calculate distance between 2 gps coordinates

hi

i have this (x,y) point: 40.716948,-74.006138

and i have this (x,y) point: 40.704977,-73.958588

(this points are on google map)

how to calculate the distance between those points ?

thank's in advance

+2  A: 

Here is a piece of code I have written not long ago :

double e=(3.1415926538*latitude1/180);
double f=(3.1415926538*longitude1/180);
double g=(3.1415926538*latitude2/180);
double h=(3.1415926538*longitude2/180);
double i=(Math.cos(e)*Math.cos(g)*Math.cos(f)*Math.cos(h)+Math.cos(e)*Math.sin(f)*Math.cos(g)*Math.sin(h)+Math.sin(e)*Math.sin(g));
double j=(Math.acos(i));
double k=(6371*j);

return k;

Of course, 3.1415926538 is Pi, and 6371 is the radius of the earth.

Scorpi0
(in kilometers)
BlueRaja - Danny Pflughoeft
+4  A: 

Here is a Javascript implementation of the haversign formula:

var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad(); 
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
        Math.sin(dLon/2) * Math.sin(dLon/2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c;

http://www.movable-type.co.uk/scripts/latlong.html

Here is a similar question: http://stackoverflow.com/questions/365826/calculate-distance-between-2-gps-coordinates

Tom Gullen
A: 
public class GeoMath
{
    /// <summary>
    /// The distance type to return the results in.
    /// </summary>
    public enum MeasureUnits { Miles, Kilometers };


    /// <summary>
    /// Returns the distance in miles or kilometers of any two
    /// latitude / longitude points. (Haversine formula)
    /// </summary>
    public static double Distance(double latitudeA, double longitudeA, double latitudeB, double longitudeB, MeasureUnits units)
    {
        if (latitudeA <= -90 || latitudeA >= 90 || longitudeA <= -180 || longitudeA >= 180
            || latitudeB <= -90 && latitudeB >= 90 || longitudeB <= -180 || longitudeB >= 180)
        {
            throw new ArgumentException(String.Format("Invalid value point coordinates. Points A({0},{1}) B({2},{3}) ",
                                                      latitudeA,
                                                      longitudeA,
                                                      latitudeB,
                                                      longitudeB));
        }


        double R = (units == MeasureUnits.Miles) ? 3960 : 6371;
        double dLat = toRadian(latitudeB - latitudeA);
        double dLon = toRadian(longitudeB - longitudeA);
        double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
        Math.Cos(toRadian(latitudeA)) * Math.Cos(toRadian(latitudeB)) *
        Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
        double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
        double d = R * c;
        return d;
    }



    /// <summary>
    /// Convert to Radians.
    /// </summary>      
    private static double toRadian(double val)
    {
        return (Math.PI / 180) * val;
    }   

    }
Orsol
A: 

Convert the coordinates to UTM and then use sqrt((x1-x2)^2+(y1-y2)^2) to calculate distance. You can find many coordinate converters in codeplex.com

Sepidar
I have found another member of the flat earth society!
Tom Gullen
That's not actually true. In cases that distance of two points are less than 6 degrees (like this case), this method is enough accurate.
Sepidar
Only joking of course :) The problem is that OP didn't specify what sort of ranges he was expecting, so we should assume that we could be testing data over 6 degrees.
Tom Gullen
+1  A: 

The other solutions give a more accurate result, but they use transcendental functions and that might be a bit of performance issue if you are going to be running that many times.


(lifted from this post) Here's an alternative; an approximation that's way less computationally expensive:

Approximate distance in miles:

sqrt(x * x + y * y)

where x = 69.1 * (lat2 - lat1) 
and y = 53.0 * (lon2 - lon1) 

You can improve the accuracy of this approximate distance calculation by adding the cosine math function:

Improved approximate distance in miles:

sqrt(x * x + y * y)

where x = 69.1 * (lat2 - lat1) 
and y = 69.1 * (lon2 - lon1) * cos(lat1/57.3) 

Source: http://www.meridianworlddata.com/Distance-Calculation.asp

NullUserException