-1

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

user984621
  • 46,344
  • 73
  • 224
  • 412

1 Answers1

0

The LatLng class constructor accepts two numbers (lat and lng) and an optional boolean:

LatLng(lat:number, lng:number, noWrap?:boolean)

Your are now passing a string and that's why it doesn't work. One possible way to fix this would be to pass to your function number parameters like from_lat,from_long,to_lat,to_long , or instead you could split your input string at the comma(,) and parse the lat and long into float variables.

Dimitris Kalaitzis
  • 1,426
  • 1
  • 11
  • 16