地理编码地址输入谷歌地图API html{高度:100%} 正文{高度:100%;边距:0;填充:0} #地图画布{宽度:1000px;高度:600px}/*ID*/ //当窗口加载到addDomlistener中时调用的函数 函数初始化(){ var geocoder=new google.maps.geocoder(); var mapOptions={center:new google.maps.LatLng(40.5,-75.5),zoom:8}; var map=new google.maps.map(document.getElementById(“地图画布”),mapOptions); } 函数地址(地址) { var address=document.getElementById(“地址”).value; geocoder.geocode({'address':address},函数(结果,状态){ if(status==google.maps.GeocoderStatus.OK){ //得到结果后,将地图居中并放在那里 map.setCenter(结果[0].geometry.location); var marker=new google.maps.marker({ 地图:地图, 位置:结果[0]。几何体。位置 }); }否则{ 警报(“地理编码因以下原因未成功:“+状态”); } }); } google.maps.event.addDomListener(窗口“加载”,初始化);

地理编码地址输入谷歌地图API html{高度:100%} 正文{高度:100%;边距:0;填充:0} #地图画布{宽度:1000px;高度:600px}/*ID*/ //当窗口加载到addDomlistener中时调用的函数 函数初始化(){ var geocoder=new google.maps.geocoder(); var mapOptions={center:new google.maps.LatLng(40.5,-75.5),zoom:8}; var map=new google.maps.map(document.getElementById(“地图画布”),mapOptions); } 函数地址(地址) { var address=document.getElementById(“地址”).value; geocoder.geocode({'address':address},函数(结果,状态){ if(status==google.maps.GeocoderStatus.OK){ //得到结果后,将地图居中并放在那里 map.setCenter(结果[0].geometry.location); var marker=new google.maps.marker({ 地图:地图, 位置:结果[0]。几何体。位置 }); }否则{ 警报(“地理编码因以下原因未成功:“+状态”); } }); } google.maps.event.addDomListener(窗口“加载”,初始化);,api,maps,Api,Maps,我正在学习谷歌地图API,我只是想让一个简单的地址搜索工作,我错过了什么?我看过类似的帖子,但找不到不正确的地方,可能需要一些新的视角。变量map和geocoder在initialize()函数之外不可见。如果打开console,则会写入错误: <!DOCTYPE html> <html> <head> <title></title> <meta name="vie

我正在学习谷歌地图API,我只是想让一个简单的地址搜索工作,我错过了什么?我看过类似的帖子,但找不到不正确的地方,可能需要一些新的视角。

变量
map
geocoder
initialize()
函数之外不可见。如果打开console,则会写入错误:

    <!DOCTYPE html> 
    <html>
      <head>
          <title></title>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <style type="text/css">
          html { height: 100% }
          body { height: 100%; margin: 0; padding: 0 }
          #map-canvas { width: 1000px; height: 600px }  /* ID */
    </style>
      <!--Include the Maps API JavaScript using a script tag.-->
    <script type="text/javascript"
      src="http://maps.google.com/maps/api/js?sensor=false">
    </script>
     <script type="text/javascript">
         //Function called when the window loads shown below in the addDomlistener
         function initialize() {
             var geocoder = new google.maps.Geocoder();
             var mapOptions = { center: new google.maps.LatLng(40.5, -75.5), zoom: 8 };
             var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
         }

         function calcAddress(address)
         {
             var address = document.getElementById("address").value;
             geocoder.geocode({ 'address': address }, function (results, status) {
                 if (status == google.maps.GeocoderStatus.OK) {
                     //Got result, center the map and put it out there
                     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);
                 }
             });
         }

    google.maps.event.addDomListener(window, 'load', initialize);
    </script>

  </head>
  <body>
      <div id="panel">
      <input id="address" type="text">
       <input type="button" value="Search" onclick="calcAddress()" />
   </div>
    <div id="map-canvas"/>
  </body>
</html>
它们应该全球化,就像:

ReferenceError: geocoder is not defined 
另外,
div
标记不是自动关闭的

     var map,
         geocoder;

     function initialize() {
         geocoder = new google.maps.Geocoder();
         var mapOptions = { center: new google.maps.LatLng(40.5, -75.5), zoom: 8 };
         map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
     }

应改为

<div id="map-canvas"/>


我相应地更改了它,它正确加载但不执行搜索。如果输入,例如Allentown并按下搜索按钮,您会得到什么?你必须在地图上找到马克。控制台窗口中是否有错误?
<div id="map-canvas"></div>