Andreas Norman
Andreas Norman

Reputation: 1027

Google Maps directionsService.route throws javascript errors

When the "route" fails due to a non-existent address for example, it throws a javascript-error in the console. I really don't like that. Shouldn't it just return "not found" in a friendly manner? I've been looking at the documentation but cannot find any answers on how to avoid this. Do I really need to encapsulate it in a try/catch?

directionsService.route(request, function(response, status) {
  if (status == 'OK') {
    directionsRenderer.setDirections(response);
  }
});

Upvotes: 0

Views: 638

Answers (1)

Pagemag
Pagemag

Reputation: 2977

This issue is reported in our public issue tracker. You can star it to receive public reports regarding this issue.

In the mean time, you can clear the console first to remove the error then log the status of the direction service response. Here's a sample code implementing the snippet below.

  directionsService.route(
      {
        origin: {query: Your_ORIGIN},
        destination: {query: YOUR_DESTINATION},
        travelMode: 'DRIVING'
      },
      function(response, status) {
        if (status === 'OK') {
          directionsRenderer.setDirections(response);
        } else {
          console.clear();
          console.log('Directions request failed due to ' + status);
        }
      });

Upvotes: 1

Related Questions