6

I am trying to sort out a method to calculate the distance between 2 points in c#.

This is the code I have been trying though I fear the answer I get is not correct.

static void Main()
    {
        //postcode australia 2600 -> 3000
        float latA = -31.997976f;
        float longA = 115.762877f;
        float latB = -31.99212f;
        float longB = 115.763228f;
        decimal distance = (DistanceBetween(latA, latB, longA, longB));
        Console.WriteLine("Distance is" + distance);
        Console.ReadLine();
    }

    static decimal DistanceBetween(float latA, float longA, float latB, float longB)
    {
        var RadianLatA = Math.PI * latA / 180;
        var RadianLatb = Math.PI * latB / 180;
        var RadianLongA = Math.PI * longA / 180;
        var RadianLongB = Math.PI * longB / 180;

        double theDistance = (Math.Sin(RadianLatA)) *
                Math.Sin(RadianLatb) +
                Math.Cos(RadianLatA) *
                Math.Cos(RadianLatb) *
                Math.Cos(RadianLongA - RadianLongB);

        return Convert.ToDecimal(((Math.Acos(theDistance) * (180.0 / Math.PI)))) * 69.09M * 1.6093M;
    }

this was adapted from a response found on this site here Distance between addresses

Any thoughts on what is going wrong/

Thanks Ryan

Community
  • 1
  • 1
user1672867
  • 75
  • 1
  • 2
  • 7
  • http://www.movable-type.co.uk/scripts/latlong.html This site always helped me out with calculating distances between two points. – woutervs Mar 27 '14 at 11:28
  • Thanks, good link. I am putting this in an asp.net application to calculate the distances between postcodes so i need to get this code right! – user1672867 Mar 27 '14 at 11:29
  • My first guess will be a sign issue in your trignometric conversions. Pull up wolframalpha or a calculator, step through the code and see the results for each of your ```Math.Sin``` (or other trig functions) and see if the results match. Converting from degrees to radians must also take account for sign when being used in trig functions – Daniel Kotin Mar 27 '14 at 11:32

4 Answers4

18

The class I usually use is GeoCoordinate

double latA = -31.997976f;
double longA = 115.762877f;
double latB = -31.99212f;
double longB = 115.763228f;

var locA = new GeoCoordinate(latA, longA);
var locB = new GeoCoordinate(latB, longB);
double distance = locA.GetDistanceTo(locB ); // metres
Liath
  • 9,913
  • 9
  • 51
  • 81
6
double lat1 = {};
double lat2 = {};
double lon1 = {};
double lon2 = {};

var R = 6376.5000; //Km
lat1 = lat1.ToRad();
lat2 = lat2.ToRad();
lon1 = lon1.ToRad();
lon2 = lon2.ToRad();
var dLat = lat2 - lat1;
var dLon = lon2 - lon1;
var a = Math.Pow(Math.Sin(dLat / 2), 2) + (Math.Pow(Math.Sin(dLon / 2), 2) * Math.Cos(lat1) * Math.Cos(lat2));
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
var distance = R * c;

public double ToRad(this double degs) {
    return degs * (Math.PI/180.0);
}

Input expects doubles.

This is the haversine formula, it's used to calculate the distances on our globe between two points. This is the distance in a straight line, if you need the distance on a path you will have to find all points on that path and then calculate the distances between each two points and then take the sum of that.

woutervs
  • 1,500
  • 12
  • 28
  • Thanks I am giving this a go now :) – user1672867 Mar 27 '14 at 11:48
  • 1
    Since you are a new user, don't forget to upvote/mark as an answer it this answer was helpfull. – woutervs Mar 27 '14 at 11:51
  • It suggests i need 15 reputation before i can perform said action :S – user1672867 Mar 27 '14 at 11:56
  • While this approach is perfectly valid (assuming the formula is correct) I prefer to use a built in method where possible as it's used by users across the world - less chance of an error! – Liath Mar 27 '14 at 11:58
  • @Liath which built-in methods are you talking about? – woutervs Mar 27 '14 at 12:02
  • @woutervs I use GeoCoordinate.GetDistanceTo() myself – Liath Mar 27 '14 at 12:52
  • Will look into that next time I need distance data. Thanks that might be usefull. – woutervs Mar 27 '14 at 12:54
  • Someone upvoted, which led me to revisiting my old code and noticing an error in it. It's updated now, if you decompile GeoCoordinate.GetDistanceTo() you'll find about the same code, but instead of it being in kilometres it's in metres. – woutervs Aug 23 '18 at 07:58
2

You can use DbGeography for spatial calculation. It has DbGeography.Distance method which is used to calculate the distance between two gps points.

Otherwise, try Ref: Harversine Formula to calculate the distance between two points.

petchirajan
  • 4,152
  • 1
  • 18
  • 20
  • Not seen that one, I usually use GeoCoordinate - http://msdn.microsoft.com/en-us/library/system.device.location.geocoordinate(v=vs.110).aspx – Liath Mar 27 '14 at 11:46
  • Please forgive my stupidity. i tried Geocoordinate but could not get it to work. I am quite new to programming. I am going to try this now. Will I have to define "using System.Data.Spatial" to be able to use that method? – user1672867 Mar 27 '14 at 11:47
  • @user1672867 I think I've got an example somewhere in one of my previous question. I'll try and dig it out - I was just curious of the difference between GeoCoordinate and DBGeography... – Liath Mar 27 '14 at 11:49
-6

Distance Formula: Given the two points (x1, y1) and (x2, y2), the distance between these points is given by the formula:

enter image description here

use it accordingly

suku
  • 217
  • 3
  • 13