-1

Possible Duplicate:
toRad() javascript function throwing error

So first I got these two p tags

<p id="pos"></p>
<p id="dist"></p>

Where I put my position in "pos" and the distance between me and New York in "dist". I got these two buttons to trigger the JS functions:

<button onclick="getLocation()">Get position</button>
<button onclick="calcDistance()">Calculate distance to NY</button>

And here is all the script:

var x=document.getElementById("pos");
var d=document.getElementById("dist");
var startPos;
function getLocation(){
      navigator.geolocation.getCurrentPosition(function(position) {
        startPos = position;
        x.innerHTML= "<h3>Your position</h3>" +
        "Latitude: " + startPos.coords.latitude + 
        "<br>Longitude: " + startPos.coords.longitude;
      });
};
function calcDistance(){
    var distt = calculateDistance(startPos.coords.latitude, startPos.coords.longitude, 40.28371627, -73.994901);
    d.innerHTML = distt.toFixed(1);
};

function calculateDistance(lat1,lon1,lat2,lon2) {
    var R = 6371;
    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)); 
    return R * c;   
}

My calcDistance button is not working and I don't have a clue what the problem is. I've tried to just put numbers in calculateDistance() but it still doesn't work. How come?

Community
  • 1
  • 1
Dean.V
  • 111
  • 9
  • 3
    Because `.toRad()` is not a number function of numbers? – apsillers Nov 26 '12 at 17:12
  • Our teacher asked us to use the function, so I thought we wouldn't care about it. So why is the toRad making it not working? – Dean.V Nov 26 '12 at 17:16
  • 1
    You're calling a nonexistent function. Maybe your teacher asked you to use a library that defined `toRad`? Or maybe your teacher expected you to use a language that has a `toRad` function? Or your teacher just expected you to define it yourself. – apsillers Nov 26 '12 at 17:18

1 Answers1

2

Put this above your JS:

Number.prototype.toRad = function() {
  return this * Math.PI / 180;
}

As @apsillers said, .toRad() is not a native function. For future reference, you should use a web console which will let you know what the error is. In this case, you get:

Uncaught TypeError: Object -0.123871823 has no method 'toRad'

Prisoner
  • 27,391
  • 11
  • 73
  • 102
  • 2
    Noticed this question that is essentially the same: http://stackoverflow.com/questions/5260423/torad-javascript-function-throwing-error – Tom Pietrosanti Nov 26 '12 at 17:16
  • Yeah, could probably be closed as a dupe. edit: voted. – Prisoner Nov 26 '12 at 17:17
  • Thanks! Appearently, I forgot to include this `if (typeof(Number.prototype.toRad) === "undefined") { Number.prototype.toRad = function() { return this * Math.PI / 180; }; } if (typeof(Number.prototype.toDeg) === "undefined") { Number.prototype.toDeg = function() { return this * 180 / Math.PI; }; }` – Dean.V Nov 26 '12 at 17:20