0

I need to call pass params to calcRoute() on click, what I am doing is:

function calcRoute(source,destt) {
  var start = new google.maps.LatLng(source);
  var end = new google.maps.LatLng(destt);
  var request = {
    origin:start,
    destination:end,
    travelMode: google.maps.TravelMode.DRIVING
  };
  directionsService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(result);
    } 
  });
}

Click Event:

$('#go').click(function(){
  var from= "24.942834,67.121237";
  var to = "24.944908,67.124491";
  calcRoute(from,to);
});

It's returning the status with ZERO_RESULT

It was working fine when I hard coded lat n lng in calcRoute(), i.e

var start = new google.maps.LatLng(24.942834,67.121237);
var end = new google.maps.LatLng(24.944908,67.124491);

Thanks for any Help.

Fahad Khan
  • 1,635
  • 4
  • 23
  • 37

1 Answers1

1

Direct string will not work as LatLng object you have to convert "24.942834,67.121237" to google.maps.LatLng to make it work.

$('#go').click(function(){
          var from= new google.maps.LatLng(24.942834,67.121237);// convert it to latlng object
          var to = new google.maps.LatLng(24.944908,67.124491);
          calcRoute(from,to);
        });

if you have "24.942834,67.121237" then you can use it like this:

var str="24.942834,67.121237";
var latlng=str.split(',')

var to = new google.maps.LatLng(parseFloat(latlng[0]), parseFloat(latlng[1]));
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44