Google 地图 directionsService.route 引发 javascript 错误

Google Maps directionsService.route throws javascript errors

例如,当 "route" 由于地址不存在而失败时,它会在控制台中抛出 javascript 错误。我真的不喜欢那样。不应该只是友好地 return "not found" 吗?我一直在查看文档,但找不到有关如何避免这种情况的任何答案。我真的需要将它封装在 try/catch 中吗?

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

我们的 public 问题跟踪器中报告了此 issue。您可以为其加注星标以接收有关此问题的 public 报告。

同时,您可以先清除控制台以消除错误,然后记录方向服务响应的状态。这是一个 sample code 实现下面的代码片段。

  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);
        }
      });