-1

以下代码显示地图并显示 div 部分但不显示地图上的标记点,任何人都可以帮助我提前感谢!

<!DOCTYPE html>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false">                                                            </script>

<script>
    var myCenter=new google.maps.LatLng(17.382094947877505,78.431396484375);
    var geocoder,map;

    function initialize()
    {
        geocoder = new google.maps.Geocoder();
        var mapProp = {
            center:myCenter,
            zoom:5,
            mapTypeId:google.maps.MapTypeId.ROADMAP
        };

        var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
    }

    function codeAddress() {
        var address = document.getElementById('address').value;
        geocoder.geocode( { 'address': address}, function(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);
            }
        });
     }
     google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
    <div id="pannel">
        <input type="address" type="textbox" value="hyderbad,Andhra Pradesh">
        <input type="button" value="Geocode" onclick="codeAddress();">
    </div>
    <div id="googleMap" style="width:1200px;height:800px;"></div>
</body>
</html>
4

1 回答 1

0

要使代码正常工作,您需要做两件事:

  1. 确保您的输入字段具有id属性(因为您正在使用document.getElementById
  2. 不要重新初始化map变量

所以地址输入应该是这样的:

<input id="address" type="text" value="hyderbad,Andhra Pradesh">

地图实例化:

map = new google.maps.Map(document.getElementById("googleMap"),mapProp);

这是完整的代码:

<!DOCTYPE html>
<html>
<head>
   <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
   <script>
      var myCenter=new google.maps.LatLng(17.382094947877505,78.431396484375);
      var geocoder,map;

      function initialize() {
         geocoder = new google.maps.Geocoder();
         var mapProp = {
            center:myCenter,
            zoom:5,
            mapTypeId:google.maps.MapTypeId.ROADMAP
         };

         map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
      }

      function codeAddress() {
         var address = document.getElementById('address').value;
         geocoder.geocode( { 'address': address}, function(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);
            }
         });
      }

      google.maps.event.addDomListener(window, 'load', initialize);
   </script>
</head>
<body>
   <div id="panel">
      <input id="address" type="text" value="hyderbad,Andhra Pradesh">
      <input type="button" value="Geocode" onclick="codeAddress();">
   </div>
   <div id="googleMap" style="width:1200px;height:800px;"></div>
</body>
</html>
于 2013-06-22T09:01:54.863 回答