【问题标题】:Google Apps Scripts, Distance Matrix URL not properly formingGoogle Apps 脚本,距离矩阵 URL 未正确形成
【发布时间】:2017-07-05 01:10:01
【问题描述】:

应用程序的总体要点是计算并显示由 html 输入定义的两个地址之间的距离。我遇到的问题是,当单击 Calculate 按钮时,它会打开一个带有奇怪 URL 的新选项卡,“@ 987654321@" 我猜实际上应该是距离矩阵请求 url,因为奇怪的 URL 包含 origin=&destination= 但我肯定是错的。

这是我当前的代码:

    <!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h2>Distance Between two Addresses</h2>
<form id="distance_form">
    <label>Origin: </label>
    <input id="origin" type="text" name="origin"/>
    <br/>
    <label>Destination: </label>
    <input id="destination" type="text" name="destination"/>
    <br/>
    <input type="submit" value="Calculate"/>
</form>
<div id="result">
</div>
  </body>

  <script>

$(function () {

  function calculateDistance(origin, destination) {
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
    {
      origins: [origin],
      destinations: [destination],
      travelMode: google.maps.TravelMode.DRIVING,
      unitSystem: google.maps.UnitSystem.IMPERIAL,
      avoidHighways: false,
      avoidTolls: false
    }, callback);
  }

  function callback(response, status) {
    if (status != google.maps.DistanceMatrixStatus.OK) {
      $('#result').html(err);
    } else {
      var origin = response.originAddresses[0];
      var destination = response.destinationAddresses[0];
      if (response.rows[0].elements[0].status === "ZERO_RESULTS") {
        $('#result').html("Better get on a plane. There are no roads between " 
                          + origin + " and " + destination);
      } else {
        var distance = response.rows[0].elements[0].distance;
        var distance_value = distance.value;
        var distance_text = distance.text;
        var miles = distance_text.substring(0, distance_text.length - 3);
        $('#result').html("It is " + miles + " miles from " + origin + " to " + destination);
      }
    }
  }

  $('#distance_form').submit(function(){

      var origin = $('#origin').val();
      var destination = $('#destination').val();
      var distance_text = calculateDistance(origin, destination);
  });

});

  </script>
  <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAxmrFQipNODQwlv57BHQxati2jR8_xWdA" async defer></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


</html>

【问题讨论】:

    标签: javascript html json google-apps-script


    【解决方案1】:

    表单提交的默认行为是打开一个新页面,这就是导致 Apps Script Web 应用出现奇怪 URL 的原因。您可以通过在 HTML 中添加以下内容来关闭此功能:

    <script>
    
      function preventFormSubmit() {
    
        var forms = document.querySelectorAll('form')
    
        for (var i = 0; i < forms.length; i++) {      
          forms[i].addEventListener('submit', function(event) {
            event.preventDefault()
          })
        }
    
      } // preventFormSubmit()
    
      window.addEventListener('load', preventFormSubmit)
    
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 2022-07-19
      相关资源
      最近更新 更多