-1

I am using this code ,

function initMap() {
  var directionsService = new google.maps.DirectionsService;
  var directionsDisplay = new google.maps.DirectionsRenderer;
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 6,
    center: {lat: 41.85, lng: -87.65}
  });
  directionsDisplay.setMap(map);

  document.getElementById('submit').addEventListener('click', function() {
    calculateAndDisplayRoute(directionsService, directionsDisplay);
  });
}

function calculateAndDisplayRoute(directionsService, directionsDisplay) {
  var waypts = [];
  var checkboxArray = document.getElementById('waypoints');
  for (var i = 0; i < checkboxArray.length; i++) {
    if (checkboxArray.options[i].selected) {
      waypts.push({
        location: checkboxArray[i].value,
        stopover: true
      });
    }
  }

  directionsService.route({
    origin: document.getElementById('start').value,
    destination: document.getElementById('end').value,
    waypoints: waypts,
    optimizeWaypoints: true,
    travelMode: google.maps.TravelMode.DRIVING
  }, function(response, status) {
    if (status === google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
      var route = response.routes[0];
      var summaryPanel = document.getElementById('directions-panel');
      summaryPanel.innerHTML = '';
      // For each route, display summary information.
      for (var i = 0; i < route.legs.length; i++) {
        var routeSegment = i + 1;
        summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment +
            '</b><br>';
        summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
        summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
        summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
      }
    } else {
      window.alert('Directions request failed due to ' + status);
    }
  });
}

above code works fine.But when I changed the part below,it gives me error code "Directions request failed due to zero_results".because of it can not find this coordinat (36.9165612, 34.895210200000065) in map.But actually I am getting this coordinats another google api.so this coordinat exists.Even it finds the address in google map https://www.google.com.tr/maps/place/36.9165612,+34.895210200000065/data=!4m2!3m1!1s0x0:0x0?sa=X&ved=0ahUKEwiB6OT03JjNAhWLLMAKHZrIApgQ8gEIGjAA

lastly when I change to coordinat to (41.85, -87.65), it works fine coz it finds the address.Many less coordinats works like this. I want to find all coordinats in google maps. How can I solve this problem?

var waypts = [];
    var checkboxArray = document.getElementById('waypoints');
    for (var i = 0; i < checkboxArray.length; i++) {
      if (checkboxArray.options[i].selected) {
        waypts.push({
          location:  new google.maps.LatLng(36.9165612, 34.895210200000065),
          stopover: true
        });
      }
    }

1 Answers1

0

It may not find an address from a lat and lng. You have to consider it.

More info here and here

Community
  • 1
  • 1
DanielGatti
  • 653
  • 8
  • 21
  • But when I changed it to this " new google.maps.LatLng (41.85, -87.65)", it works..? – talan suna Jun 08 '16 at 15:33
  • Yes, because 41.85 will look for directions from 41.8500000 to 41.8599999, and -87.6500000 to -87.6599999. So, if a address is in that range, it will find it. But not all coordinats have an address. For example, a coordinat in the sea, will not have an address.. – DanielGatti Jun 08 '16 at 15:37
  • thank you, I got it now. there is no driving america to europe :).probably I have to look FLYING mode like google.maps.TravelMode.DRIVING :D – talan suna Jun 08 '16 at 15:50