2010-10-21 67 views
0

首先這裏是我的代碼有問題:GMAP v3和JS不打漂亮

var geocoder; 
    var map; 

    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-34.397, 150.644); 
    var myOptions = { 
     zoom: 18, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.HYBRID 
    } 
    map = new google.maps.Map(document.getElementById("details"), myOptions); 

    var q = $("span#address").text() + ' ' + $("span#city").text() + ' ' +$("span#state").text() + ' ' +$("span#zip").text(); 
    var t = document.getElementById('address').value; 
    console.log(t); 
    //var city = document.getElementById("home-city").value; 
    //console.log(city); 
    geocoder.geocode({ 'address': t}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      map.setCenter(results[0].geometry.location); 
      var marker = new google.maps.Marker({ 
       map: map, 
       //draggable: true, 
       position: results[0].geometry.location 
      }); 
     } else { 
      console.log("Geocode was not successful for the following reason: " + status); 
     } 
    }); 

好了,我的問題是,我可以加載到geocoder.geocode()可變這樣

var address = "string address"; 

我想從特定頁面抓取地址並將地址存儲在一個var中,然後將該var傳遞給Gmaps,並且在完美的世界中這應該起作用!但它似乎沒有這樣的世界;-(

所以我做了一些測試,足夠有趣...

var q = $("span#address").text() + ' ' + $("span#city").text() + ' ' +$("span#state").text() + ' ' +$("span#zip").text(); 

的作品時,我做了的console.log(地址),我得到的地址outputed 。但是我不能將變量地址插入到地理編碼中,因爲它不會識別它。其次,我嘗試了很好的常規JavaScript方法,並嘗試使用地址唯一標識符來獲取元素的val。Firebug和地理編碼錯誤tell我認爲值是NULL ...

所以我很困惑,jQuery可以抓取數據,但不能傳入地理編碼。然後正常的JavaScript告訴我我瘋了,有沒有在數據領域我試圖通過ID參考...

任何人都得到了這樣的情況下工作?

嗯,也許我應該提一下,我有所有這些代碼在jQuery實時點擊功能上執行。當用戶點擊地圖圖標時,所有此代碼將假定執行...

回答

0

所以,問題是,我是第一個宣佈谷歌地圖,然後嘗試引用地址信息的價值。我在創建一個新的谷歌地圖api引用之前將var地址移至頂端。

0
  • 使用逗號而不是空格來分隔值。
  • 確保您只有一個帶有「地址」id的HTML元素。
  • getElementById()檢索,使用.value拿到元件(未.val
+0

是的,我使用.value,並給它一個隨機數字ID只是爲了確保我沒有錯,並且已經有一個具有相同ID的元素。但我仍然無效。並做了逗號事情 – Eli 2010-10-21 07:07:31