0

i used this code which takes longitude and latitude of two different location and calculates the distance between them my code is

protected void Button1_Click(object sender, EventArgs e)
{
    double lat1= Convert.ToDouble(TextBox1.Text);
    double lon1= Convert.ToDouble(TextBox2.Text);
    double lat2= Convert.ToDouble(TextBox3.Text);
    double lon2= Convert.ToDouble(TextBox4.Text);

    var rlat1 = Math.PI * lat1/180;
    var rlat2 = Math.PI * lat2/180;
    var rlon1 = Math.PI * lon1/180;
    var rlon2 = Math.PI * lon2 / 180;
    var theta = lon1-lon2;
    var rtheta = Math.PI * theta/180;
    var dist = Math.Sign(rlat1) * Math.Sign(rlat2) + Math.Cos(rlat1) * Math.Cos(rlat2) * Math.Cos(rtheta);
    dist = Math.Acos(dist);
    dist = dist * 180/Math.PI;
    dist = dist * 60 * 1.1515;
    dist = dist * 1.609344 ;
    TextBox5.Text = dist.ToString("0.######");  
}

but for all the input values the result i am getting is NaN.please help me.

helb
  • 7,609
  • 8
  • 36
  • 58
user2775241
  • 119
  • 3
  • 12
  • 1
    can you add some sample inputs to the question – Marcom Mar 27 '14 at 10:11
  • Debugging this should provide the solution. I'm not sure were in a better position to do this than you? – Liam Mar 27 '14 at 10:12
  • for any coordinates it gives the same answers – user2775241 Mar 27 '14 at 10:12
  • 1
    Better take a look at this question [what does NaN mean for doubles in C#?](http://stackoverflow.com/q/8112529/1671639) – Praveen Mar 27 '14 at 10:15
  • Could be for many reasons. Usually it happens when for example the input is not in the domain of the function ie Acos has a domanain −1 ≤ x ≤ 1. Or even that the resulting number is imaginary – Murdock Mar 27 '14 at 10:17

2 Answers2

10

Ok, because I led you up the garden path with my previous answer, I ported this to give you an algorithm that works:

void Main()
{
    double lat1=12.916933d, 
           lon1=77.562658d,
           lat2=12.930140d,
           lon2=77.587732d;
    double dist = GetDistanceFromLatLonInKm(lat1, lon1, lat2, lon2);
    // dist == 3.08890370651166 yay!

}

double GetDistanceFromLatLonInKm(double lat1,
                                 double lon1,
                                 double lat2,
                                 double lon2) {
  var R = 6371d; // Radius of the earth in km
  var dLat = Deg2Rad(lat2 - lat1);  // deg2rad below
  var dLon = Deg2Rad(lon2 - lon1); 
  var a = 
    Math.Sin(dLat / 2d) * Math.Sin(dLat / 2d) +
    Math.Cos(Deg2Rad(lat1)) * Math.Cos(Deg2Rad(lat2)) * 
    Math.Sin(dLon / 2d) * Math.Sin(dLon / 2d); 
  var c = 2d * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1d - a)); 
  var d = R * c; // Distance in km
  return d;
}

double Deg2Rad(double deg) {
  return deg * (Math.PI / 180d);
}

Your calculation appears to work correctly when you supply fixed values:

var lat1 = 0d;
var lon1 = 52d;
var lat2 = 0d;
var lon2 = -52d;

which rather implies that these conversions are failing:

double lat1 = Convert.ToDouble(TextBox1.Text);
double lon1 = Convert.ToDouble(TextBox2.Text);
double lat2 = Convert.ToDouble(TextBox3.Text);
double lon2 = Convert.ToDouble(TextBox4.Text);

If you're still having trouble, place a breakpoint on these lines and take a look at the values of TextBox1.Text etc.

For more predictable parsing of floating point numbers, it's best to supply culture information:

Convert.ToDouble("1.2", System.Globalization.CultureInfo.InvariantCulture)

Community
  • 1
  • 1
spender
  • 117,338
  • 33
  • 229
  • 351
2

Check if decimal delimiter is valid. It has to be comma or dot, depending on your regional settings.

Elvedin Hamzagic
  • 825
  • 1
  • 9
  • 22