2013-06-22 66 views
-1

下面的代码显示地图,也显示div部分,但没有在地图上显示标记点,任何人都可以提前帮助我!谷歌地图地理编码API

<!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> 
+0

一次声明变量,在顶部(VAR地图)变种。 然后只需重新分配它们,而不使用var(在初始化函数中) http://jsfiddle.net/B5ZM7/ – jamiltz

+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>