Math.radians is not a function(Geofence)

I plot marker on map as boundary, and every time I click in a certain location of map I get the latlang coordinates and save it to my database,

this is the sample coordinates that was saved in my database

53.198526418064645,-105.76238214969635
53.198564980328875,-105.76508045196533
53.199002016899044,-105.76231241226196
53.19939406061905,-105.76508045196533
53.19941012790812,-105.76509118080139
53.19941976827867,-105.76212465763092
53.199426195191194,-105.76357841491699

Now I want that if the user passes each coordinate or gets inside 5 meters perimeter of each coordinates it will prompt like this, the user “get’s in” or “get’s out” in the area.but i have no idea how do i achieve like this to detect if the user passes or get inside in this 5 meters peremeter in each coordinates.is this possible?

I followed this example approach

but i got an error,Math.radians is not a function!

this is my code


function geofence(){
/****this is the latng coordinates user's tracker location*****/
$lat = 53.199425;//this is the lat user's tracker
$lng = -105.763580;//this is the lng user's tracker

/*****************************************************************/
//here i don't know how to put 5 meters  radius.and i get error in using Math.radians is not a function.

    $distance = 3959 * Math.acos( Math.cos( Math.radians(53.199425) ) * Math.cos( radians( $lat ) ) * Math.cos( Math.radians( $lng ) - Math.radians(-105.763580) ) + Math.sin( Math.radians(53.199425) ) * Math.sin( Math.radians( $lat ) ) );

  console.log("Alert distance",$distance);
}



can you help me please.

Radians are worked out by multiplying the degrees angle by Tau over 360.


function radians(degrees) {
    var TAU = 2 * Math.PI;
    return degrees * TAU / 360;
}

Or, now that Brendan Eich is going to champion the inclusion of Math.TAU, I’ve been taking to doing the following instead:


if (!Math.TAU) {
    Math.TAU = 2 * Math.PI;
}
// ...
function radians(degrees) {
    return degrees * Math.TAU / 360;
}

Yes, you could just use Math.PI/180, but to me it just seems to be counter-intuitive to use half a circle to convert degrees around a circle.

Hi paul,

Thank you for the quick reply,I am just confuse how can i apply the 5 meters in my code?

Thank you in advance.

JavaScript doesn’t have a Math.radians function.

Instead, you need to supply your own, called radians


function radians(degrees) {
    var TAU = 2 * Math.PI;
    return degrees * TAU / 360;
}

Once you have that function, you can use it to convert degrees into radians, for example:


radians(53.199425); // 0.9285051264177843

So include that radians function, change any Math.radians(…) function calls to be instead radians(…) and you should be fine.

Hi paul,thanks i got exactly same output of your comment…can i ask do i need to covert the 3959 into meter ?

Only if you want the answer in metres.
Look at the Great circle page and you will see that the number is the radius of the earth.

Hi paul,ok so i will change it to meters,so if i want 5 meters , i will just do this

if (distance <5){
alert(“5 meters”);
}

please correct me if i am wrong.

Thank you in advance.

Yea, though it would be more accurate if you said that they are within 5 metres.

Thank you paul :slight_smile: