2012-04-23 70 views
2

我有這個JavaScript在我的HTML什麼,我想在這裏做的是讓地圖表示給定位置嘗試使用谷歌API V3從地址獲得位置

<script type="text/javascript"> 
var geocoder; 
var map; 
function getmaploc() { 
    geocoder = new google.maps.Geocoder(); 
    var myOptions = { 
     zoom: 8, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    geocoder.geocode('cairo', 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); 
     } 
     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    }); 
} 
</script> 

<body onload="getmaploc()"> 
    <div id="map_canvas" style="width: 320px; height: 480px;"></div> 
</body> 

我無法弄清楚什麼是錯與此任何幫助?

+0

請格式化你的代碼,很多人(包括我在內),將無法讀取未格式良好的 – 2012-04-23 22:58:22

+0

固定它對不起進出口新的計算器 – 2012-04-23 23:05:55

+0

格式化也爲你的代碼,你*修復*仍然不是很可讀。這裏有另一個提示:「它不起作用」沒有錯誤信息使其他人更難以幫助。 – 2012-04-24 00:37:37

回答

4

有多個錯誤,

  • 一個省略支架
  • 初始化地圖第一
  • 地理編碼()需要使用GeocoderRequest對象作爲參數

固定代碼:

var geocoder; 
var map; 
function getmaploc() { 
    geocoder = new google.maps.Geocoder(); 
    var myOptions = { 
     zoom : 8, 
     mapTypeId : google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

    geocoder.geocode({ 
     address : 'cairo' 
    }, function(results, status) { 
     console.log(results); 
     if(status == google.maps.GeocoderStatus.OK) { 
      map.setCenter(results[0].geometry.location); 
      new google.maps.Marker({ 
       map : map, 
       position : results[0].geometry.location 
      }); 
     } else { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 

    }); 
} 
+0

+1並重新格式化!大聲笑 – 2012-04-24 00:12:22

+1

請不要抱怨我的縮進,我喜歡它^^ – 2012-04-24 00:17:30

+0

非常感謝修復Dr.Molle – 2012-04-24 01:21:24