Google地图说明的HTML代码无效

时间:2013-04-04 05:59:07

标签: html css api google-maps

我正在尝试允许用户使用Google的自动填充功能进入2个城市,然后在Google地图上绘制它们之间的界限。自动填充功能适用于两个盒子。起始位置有效(当用户进入城市时,地图会放大到该城市并显示标记),但输入结束位置时没有任何反应。如果有人看到问题所在,我会非常感谢一些建议。

Directions.html

<!DOCTYPE html>
<html>
<head>
<title>Google Maps JavaScript API v3 Example: Places Autocomplete</title>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>

<style>
  body 
    {
        font-family: sans-serif;
        font-size: 14px;
    }
  #lred { color: #FF0000;}
  #map-canvas 
    {
        height: 400px;
        width: 600px;
        margin-top: 0.6em;
    }
  start {border: 1px solid  rgba(0, 0, 0, 0.5);}
  start.notfound 
    {
        background-color: #F5A9A9;
        }
  end {border: 1px solid  rgba(0, 0, 0, 0.5);}
  end.notfound 
    {
        background-color: #F5A9A9;
        }
 </style>

 <script>
  var directionDisplay;
  var directionsService = new google.maps.DirectionsService();
  function initialize() 
    {
            directionsDisplay = new google.maps.DirectionsRenderer();
        var mapOptions = 
            {
                center: new google.maps.LatLng(39.8282, -98.5795),
                zoom: 4,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
            directionsDisplay.setMap(map);
        var options = 
            {
                types: ['(cities)'],
                componentRestrictions: {country: 'us'}
            };
        var start = document.getElementById('start');   
        var sAutocomplete = new google.maps.places.Autocomplete(start, options);
        var end = document.getElementById('end');   
        var eAutocomplete = new google.maps.places.Autocomplete(end, options);

        sAutocomplete.bindTo('bounds', map);
        eAutocomplete.bindTo('bounds', map);

        var infowindow = new google.maps.InfoWindow();
        var marker = new google.maps.Marker({ map: map });

        google.maps.event.addListener(sAutocomplete, 'place_changed', function() {
            infowindow.close();
            marker.setVisible(false);
            start.className = '';
            var place = sAutocomplete.getPlace();
                // Inform the user that the place was not found and return.
                if (!place.geometry) 
                    { 
                        start.className = 'notfound';
                        return;
                    }
                else 
                    {
                        map.setCenter(place.geometry.location);
                        map.setZoom(6);  // Why 17? Because it looks good.
                    }
            var image = 
                {
                    url: place.icon,
                    size: new google.maps.Size(71, 71),
                    origin: new google.maps.Point(0, 0),
                    anchor: new google.maps.Point(17, 34),
                    scaledSize: new google.maps.Size(35, 35)
                };
            marker.setIcon(image);
            marker.setPosition(place.geometry.location);
            marker.setVisible(true);
     });
    }

    function calcRoute() {
    var start2 = document.getElementById("start").value;
    var end2 = document.getElementById("end").value;
    var request = {
        origin:start2,
        destination:end2,
        travelMode: google.maps.TravelMode.DRIVING
    };
    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 onload="initialize()">
 <div>
  <input id="start" onchange="calcRoute();" type="text" size="50">
  <input id="end" onchange="calcRoute();" type="text" size="50">
</div>
<div id="map-canvas"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

将HTML文件加载到语法检查编辑器中,例如Komodo Edit。它确定了这两行中的错误:

<input id="start" onchange="calcRoute(); type="text" size="50">
<input id="end" onchange="calcRoute(); type="text" size="50">
你看到了吗?两个标签中都缺少引号。

修复后,您的页面行为会有所不同。现在它确实调用了calcRoute()函数,之前根本没有调用它。但是JavaScript控制台中显示错误:

Uncaught ReferenceError: directionsService is not defined

果然,我没有在代码中的任何地方看到directionsService

告诉你......