1

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Biribu
  • 535
  • 3
  • 12
  • 27
  • If you where to print all the data you are throwing in the equation what would it print? Are you sure that the data is all entirely numeric? – npinti Jul 05 '12 at 10:05
  • That is the point. I think they should be all numeric but a = NaN and that is making all equation is invalid... I don't know if my fault is in toRad function or in a = Math.sin... line – Biribu Jul 05 '12 at 10:25
  • Well, I try to print before and after to convert using toRad function. Before prints my lat value and after prints other number, but a number. So the problem is in a = Math.sin... line – Biribu Jul 05 '12 at 10:30
  • I would recommend printing all the data you have and the seperate functions you are using as well. This will help you identify where is your code failing. – npinti Jul 05 '12 at 10:37
  • I did. Stupid epic fail for my part. dlat != dLat.... Thanks for your help and sorry hahaha – Biribu Jul 05 '12 at 10:40

0 Answers0