toRad() Javascript function throwing error

I'm trying to find the distance between two points (for which I've latitudes & longitudes) using the technique described here at Calculate distance between two latitude-longitude points? (Haversine formula)

The codes are as below Javascript:

var R = 6371; // Radius of the earth in km
var dLat = (lat2-lat1).toRad();  // Javascript functions in radians
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; // Distance in km

But when I try to implement it, an error shows up saying Uncaught TypeError: Object 20 has no Method 'toRad'.

Do I need a special library or something to get .toRad() working? because it seems to be screwing up on the second line.



You are missing a function declaration.

In this case toRad() must be defined first as:

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

according to the code segment all at the bottom of the page


Or in my case this didn't work. It may because i needed to call toRad() inside jquery. Im not 100% sure, so i did this:

function CalcDistanceBetween(lat1, lon1, lat2, lon2) {
    //Radius of the earth in:  1.609344 miles,  6371 km  | var R = (6371 / 1.609344);
    var R = 3958.7558657440545; // Radius of earth in Miles 
    var dLat = toRad(lat2-lat1);
    var dLon = toRad(lon2-lon1); 
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * 
            Math.sin(dLon/2) * Math.sin(dLon/2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c;
    return d;

function toRad(Value) {
    /** Converts numeric degrees to radians */
    return Value * Math.PI / 180;

I needed to calculate a lot of distances between the points for my project, so I went ahead and tried to optimize the code, I have found here. On average in different browsers my new implementation runs almost 3 times faster than mentioned here.

function distance(lat1, lon1, lat2, lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = (lat2 - lat1) * Math.PI / 180;  // deg2rad below
  var dLon = (lon2 - lon1) * Math.PI / 180;
  var a = 
     0.5 - Math.cos(dLat)/2 + 
     Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * 
     (1 - Math.cos(dLon))/2;

  return R * 2 * Math.asin(Math.sqrt(a));

You can play with my jsPerf (which was vastly improved thanks to Bart) and see the results here.


Why not simplify the above equation and same a few computations?

Math.sin(dLat/2) * Math.sin(dLat/2) = (1.0-Math.cos(dLat))/2.0

Math.sin(dLon/2) * Math.sin(dLon/2) = (1.0-Math.cos(dLon))/2.0


I changed a couple of things:

if (!Number.prototype.toRad || (typeof(Number.prototype.toRad) === undefined)) {

and, I noticed there was no checking for the arguments. You should make sure the args are defined AND probably do a parseInt(arg, 10) / parseFloat on there.


I was having the same problem.. looking at Casper's answer, I just did a quick-fix: Ctrl+H (Find and Replace), replaced all instances of .toRad() with * Math.PI / 180 . That worked for me.

No idea on the browser performance speeds etc, though.. My use case only needs this when the user clicks on a map.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.