1

I know this is a repetitive question but I have to ask because my code is not working. I have no clue why not.

My code:

<!DOCTYPE html>
<html>
<head>
<script>
function displayDistance()
{

var R = 6371; // km
var dLat = (23.87284-23.76119).toRad();
var dLon = (90.39603-90.43491).toRad(); 
var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
        Math.cos(23.76119.toRad()) * Math.cos(23.87284.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;
document.getElementById("demo").innerHTML=d;
}
</script>
</head>
<body>
<div id="demo"></div>
<button type="button" onclick="displayDistance()">Display Distance</button>

</body>
</html> 

But just nothing happens. Thanks

user1559230
  • 2,790
  • 5
  • 26
  • 32

1 Answers1

2

toRad is not a native javascript function. You must declare it before using it.

/** Converts numeric degrees to radians */
if (typeof(Number.prototype.toRad) === "undefined") {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
}

Code from here : toRad() Javascript function throwing error

You can check this jsfiddle : http://jsfiddle.net/ySsQ3/

By the way, you really should put your javascript code at the end of the body (and not in the head)

Community
  • 1
  • 1
Magus
  • 14,796
  • 3
  • 36
  • 51