map.GetCenter seemed to return wrong value

I thought that map.GetCenter was returning wrong values (to see the effect use http://www.svrsig.org/cgi-bin/gemap.cgi lat=52&long=-4&zoom=6 which places Edwinsford at the centre. Move the mouse over the map and the centre is shown to be 52deg4'2.2"N instead of 52degN.

The problem is that the javascript function 'parseInt()' returns 4 instead of zero for the argument parseInt(4.547473508864641e-13). Now how do I sort that out

The relevant finction is:

function latmin(cin) {);
var ain = 0;
var bin = 0;
if (cin>0) {
ain = (60*cin) % 60;
bin = (36000*cin) % 600;
return " "+parseInt(cin)+"°"+parseInt(ain)+"'"+parseInt(bin/10)+'.'+parseInt((bin) % 10)+'"N';
}
cin = -cin;
ain = (60*cin) % 60;
bin = (36000*cin) % 600;
return " "+parseInt(cin)+"°"+parseInt(ain)+"'"+parseInt(bin/10)+'.'+parseInt((bin) % 10)+'"S';
}

which returns 52deg4'2.2"N for latmin(52.00000000000001)

Any ideas please




Answer this question

map.GetCenter seemed to return wrong value

  • Tom Phillips

    The reason this occurs is because of the way parseInt works.  It stops counting from the left when it hits any non-numeric character (in this case, e).  Therefore, it only understood 4.547473508864641 and then convert that to nearest Int (you will notice this behaviour with parseInt on floating point numbers too such as (4.54,-10)). 

    What I would recommend is that you do a Math.round rather than parseInt.  Putting this into any html file seems to work for me:

    alert(Math.round(4.547473508864641e-13));

    outputs:
    0

    Hope that helps,


  • map.GetCenter seemed to return wrong value