Search code examples
javascriptgoogle-mapsgoogle-maps-api-3directions

Google directions don't work


I try to make directions for my current position and marker coordinate. so i get latitude and longitude using getcurrentposition. then, i mark it.

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var node = new google.maps.LatLng(-7.276442,112.791174);
  var mapOptions = {
    zoom: 15,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map'),
      mapOptions);
  directionsDisplay.setMap(map);

    navigator.geolocation.getCurrentPosition(function(position) {
      pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);

      var marker = new google.maps.Marker({
            position : pos,
            map: map,
            title:'Lokasi Anda'
      });
      var marker1 = new google.maps.Marker({
            position : node,
            map: map,
            title:'Lokasi Anda'
      });

      map.setCenter(pos);
    }, function() {
      handleNoGeolocation(true);
    });

}

then, I calculate it using this function.

function calcRoute() {

  var request = {
      origin:pos,
      destination:node,
      travelMode: google.maps.DirectionsTravelMode.DRIVING
  };

  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}

but why i can't see direction from pos to node. any suggestion ?


Solution

  • If I call the calcRoute function (and pass in the "pos" and "node" locations, it can't find directions between where I am (in the US) and where the point is located (Raya Its, Sukolilo, Surabaya City, East Java 60117, Republic of Indonesia). The DirectionsService returns a status of "ZERO_RESULTS".

    ZERO_RESULTS No route could be found between the origin and destination.

    working example

    function calcRoute(pos,node) {
    var directionsService = new google.maps.DirectionsService();
      var request = {
          origin:pos,
          destination:node,
          travelMode: google.maps.DirectionsTravelMode.DRIVING
      };
    
      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
        } else { alert("Directions failed: "+status); }
      });
    }