I am trying to pass coords to a function for counting a distance between two points:
function calcRoute(from, to){
var distance;
//43.653226, -79.38318429999998
//34.0522342, -118.2436849
console.log(from); // => 34.0522342,-118.2436849
console.log(to); // => 43.653226, -79.38318429999998
var p1 = new google.maps.LatLng(from);
var p2 = new google.maps.LatLng(to);
console.log(p1); //
console.log(p2); //
distance = (google.maps.geometry.spherical.computeDistanceBetween(p1, p2)/1600).toFixed(2);
// => NaN
}
The problem is that when I pass variables (from
, to
) to the functions new google.maps.LatLng
, it always returns
{k: NaN, A: NaN, toString: function, j: function, equals: function…}
But when I manually put them the coords, like
var p1 = new google.maps.LatLng(43.653226, -79.38318429999998);
I get the needed distance.
How's that possible? Why I can't pass the cords through variables?
Thank you