单击事件后将值连接到函数

时间:2015-03-03 04:52:16

标签: javascript html google-maps

我正在尝试在Google Places API中实现文本搜索字段,我创建了文本字段以及导致'calcRoute()'函数的click事件。我在将文本字段设置为变量'start'和'end'时遇到了麻烦(至少我认为这是问题)。谁能帮我吗?路线列表应显示在地图右侧。

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Displaying text directions with <code>setPanel()</code></title>
    <style>
      html, body, #map-canvas {
        height: 80%%;
        margin: 0px;
        padding: 0px
      }
      #panel {
        position: absolute;
        top: 5px;
        left: 50%;
        margin-left: -180px;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
      }
    </style>
    <style>
      #directions-panel {
        height: 80%%;
        float: right;
        width: 390px;
        overflow: auto;
      }

      #map-canvas {
        margin-right: 400px;
      }

      #control {
        background: #fff;
        padding: 5px;
        font-size: 14px;
        font-family: Arial;
        border: 1px solid #ccc;
        box-shadow: 0 2px 2px rgba(33, 33, 33, 0.4);
        display: none;
      }

      @media print {
        #map-canvas {
          height: 500px;
          margin: 0;
        }

        #directions-panel {
          float: none;
          width: auto;
        }
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true">      </script>
    <script>
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();

   function initialize() {
   directionsDisplay = new google.maps.DirectionsRenderer();
   var mapOptions = {
    zoom: 7,
    center: new google.maps.LatLng(41.850033, -87.6500523)
    };
   var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);
  directionsDisplay.setMap(map);
  directionsDisplay.setPanel(document.getElementById('directions-panel'));

  var control = document.getElementById('control');
  control.style.display = 'block';
  map.controls[google.maps.ControlPosition.TOP_CENTER].push(control);
  }

function calcRoute() {
  var start = document.getElementById('start').value;
  var end = document.getElementById('end').value;
  var request = {
    origin: start,
    destination: end,
    travelMode: google.maps.TravelMode.TRANSIT
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="control">
      <strong>Start:</strong>

        <input type="text" data-bind="value:start"/>

      <strong>End:</strong>

        <input type="text" data-bind="value:end"/>

        <button onclick="calcRoute()">Get Directions</button>

    </div>

    <div id="directions-panel"></div>
    <div id="map-canvas"></div>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

我认为您在没有在HTML中设置任何ID

的情况下使用getElementById
<!-- Elements don't have IDs -->
<input type="text" data-bind="value:start"/>
<input type="text" data-bind="value:end"/>

试试这个:

<input id="start" type="text" data-bind="value:start"/>
<input id="end" type="text" data-bind="value:end"/>

现在调用此代码时,应该找到元素

var start = document.getElementById('start').value;
var end = document.getElementById('end').value;