I found this post: Google Maps - How to get the distance between two point in metre?
I have the same problem, my variable "a" has the NaN value. I prefer not using google api if I can do it.
I am using the code in this web page: http://www.movable-type.co.uk/scripts/latlong.html
But my variables still return NaN...
This is my code:
function actualizaLimpieza(jsonData){ //jsonData keeps all points (lat&lon)
var latJSON;
var lonJSON;
var R = 6371; // km
var dLat;
var dLon;
var lat1;
var lat2;
var a;
var c;
var d;
latGPS = 43.466827534582606; //this are supossed to be gotten by gps, but for try...
lonGPS = -3.863614797592163;
for(i = 0; i < jsonData.length; i ++){
latJSON = jsonData[i].lng;
lonJSON = jsonData[i].lat;
dlat = toRad(latJSON-latGPS);
dlon = toRad(lonJSON-lonGPS);
latGPS = toRad(latGPS);
latJSON = toRad(latJSON);
a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(latGPS) * Math.cos(latJSON);
alert("a vale: " + a); //returns NaN
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
alert("c vale: " + c);
d = R * c;
alert("d vale: " + d);
if(d < 0,010){
alert("less than 10 meters");
}
}
}
function toRad(degrees){
return degrees * Math.PI / 180;
}
Should I do any more changes to get it working? Any script I should add? I haven't add anyone for this work.
Ok, I fail writing variables: dlat != dLat... Now it works more or less