2012-01-06 77 views
2

我試圖當前顯示頁面加載時的地址的位置,但我的地圖根本不顯示。我試圖提醒(地址),它的工作原理。它顯示了我想要得到的正確地址。但我不確定我是否做得對?顯示地址位置與谷歌地圖onload

<script> 
    var geocoder; 
    var map; 
    function codeAddress() { 
    geocoder = new google.maps.Geocoder(); 
    //var latlng = new google.maps.LatLng(-34.397, 150.644); 
    var myOptions = { 
     zoom: 8, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
      var city_state_zip = document.getElementById("city_state_zip").innerHTML; 
      var street_address = document.getElementById("street_address").innerHTML; 
      var address = street_address + " " + city_state_zip;//document.getElementById(street_addres +" "+ city_state_zip).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); 
       } 
      }); 
      } 



    window.onload = function(){ 
      codeAddress(); 
     } 
</script> 

我結合初始化()與codeAddress()在這個例子中發現:http://code.google.com/apis/maps/documentation/javascript/geocoding.html#GeocodingStatusCodes我想,因爲我不希望加載與隨機經緯度地圖結合這一點,我希望它加載的原因我已經有了一個地址。

有沒有人有任何想法,如果我甚至正確地做到這一點?

謝謝!

+0

既然你有latlng註釋掉我不知道這是否會導致問題。此外,你是否能夠在沒有所有地理編碼的情況下顯示地圖? – asawilliams 2012-01-06 04:58:03

+0

當我取消對latlng的註釋時,它會轉到特定的latlng,然後直接轉到所需的地址,我如何不讓它轉到latlng並直接尋址onload? – hellomello 2012-01-06 05:20:08

+1

在設置地圖選項並使用結果[0] .geometry.location填充選項的中心屬性之前,請先執行所有地理編碼器的工作內容 – asawilliams 2012-01-06 05:39:29

回答

4

腳本的下列修改使其適用於與我處在同一條船上的人員。這現在允許我加載頁面時加載一個特定的地址,它會自動檢測我的html中的lat,lng。

這是從谷歌的網站稍作修改。

<script> 
    var geocoder; 
    var map; 
    function codeAddress() { 
    geocoder = new google.maps.Geocoder(); 
    var lat=''; 
    var lng='' 
    var city_state_zip = document.getElementById("city_state_zip").innerHTML; 
    var street_address = document.getElementById("street_address").innerHTML; 
    var address = street_address + " " + city_state_zip; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     lat = results[0].geometry.location.lat(); //getting the lat 
     lng = results[0].geometry.location.lng(); //getting the lng 
     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); 
      } 
    }); 
    var latlng = new google.maps.LatLng(lat, lng); 
    var myOptions = { 
     zoom: 8, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    } 


window.onload = function(){ 
     codeAddress(); 
} 
</script> 
+0

然後,您應該使用工作代碼編輯此答案並接受您自己的答案!這比在一起的問題和答案要好 – 2012-01-06 06:30:38

+0

我會把工作代碼放在這裏,但直到幾天後我才接受它 – hellomello 2012-01-06 16:10:15