Google地理编码器不起作用

时间:2013-12-12 17:10:36

标签: javascript html geocoding google-geocoder

这是我的index.html文件 - 我遇到问题的部分是使地理编码功能正常工作。表格和按钮显示但是当我完全按下按钮时没有任何反应。

 <!DOCTYPE html>
<html>
<head>
 <title>First Google Map</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
 <script type="text/javascript">
  function initialize() {
    var latlng = new google.maps.LatLng(43.696299,-79.346271); 
    var myOptions = {
     zoom:13,
     center: latlng,
     mapTypeId: google.maps.MapTypeId.MAP
 };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
   geocoder = new google.maps.Geocoder();

    var myPlacesKML = new google.maps.KmlLayer('http://mristo.webatu.com/melissaristo/kml/torontodonvalley.kml?ver=1');
    myPlacesKML.setMap(map);

   function geocode() {
   address = document.getElementById("address").value;
   geocoder.geocode({
     'address': address,
     'partialmatch': true}, geocodeResult);
   }
   }
  </script>
</head> 
<body onload="initialize()">
<input type="text" id="address">
<input type="button" onclick="geocode();" value="Geocode it!"
}
</script>
 <body onload="initialize()">
 <div id="map_canvas" style="width:500px; height:400px"></div>
 </body>
 </html>

表单和按钮显示但单击按钮时按钮不执行任何操作。 有任何想法吗?我对此非常陌生并且很丢失

1 个答案:

答案 0 :(得分:1)

这段代码中有很多错误。我修好了它会在您在文本框中输入的地址处放置一个标记。

<!DOCTYPE html>
<html>
<head>
 <title>First Google Map</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
 <script type="text/javascript">
  var map;
  function initialize() {
    var latlng = new google.maps.LatLng(43.696299,-79.346271); 
    var myOptions = {
     zoom:13,
     center: latlng,
     mapTypeId: google.maps.MapTypeId.MAP
 };
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
   geocoder = new google.maps.Geocoder();

    var myPlacesKML = new google.maps.KmlLayer('http://mristo.webatu.com/melissaristo/kml/torontodonvalley.kml?ver=1');
    myPlacesKML.setMap(map);
   }

   function geocode() {
   address = document.getElementById("address").value;
   geocoder.geocode({
     'address': address,
     'partialmatch': true}, geocodeResult);
   }

   function geocodeResult( results, status )
   {if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }

   }
  </script>
</head> 
<body onload="initialize()">
<input type="text" id="address">
<input type="button" onclick="geocode();" value="Geocode it!">

 <div id="map_canvas" style="width:500px; height:400px"></div>
 </body>
 </html>

以下是错误: 1.额外的} 2. map变量是本地的,需要是全局的 3.缺少功能geocodeResult
4.额外的身体标签
5.缺少结束&gt;来自输入
6.将geocode()从initialize()内部移动到global。