2011-05-13 92 views
9
google.visualization.events.addListener( 
      geomap, "regionClick", function(e) { 
      console.log(e["region"]); 
      console.log(data.getValue(e["region"],1)); 
      }); 

我使用此代碼來查看哪個區域被點擊。 e["region"]給出了該區域的行號,然後使用getValue查看區域(標記)名稱。谷歌API,從數據表中獲取價值

現在,在控制檯日誌顯示了這個錯誤:

Uncaught Error: Invalid row index 1. Should be in the range [0-14]

行索引1怎能是無效的,因爲它是在範圍[0-14]

編輯:

你去那裏,更多的代碼:)

 <!-- 
You are free to copy and use this sample in accordance with the terms of the 
Apache license (http://www.apache.org/licenses/LICENSE-2.0.html) 
--> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
    <title>Google Visualization API Sample</title> 
    <script type="text/javascript" src="http://www.google.com/jsapi"></script> 
<script type="text/javascript"> 

    google.load("visualization", "1", {"packages": ["geomap"]}); 

    google.setOnLoadCallback(drawMap); 



    function drawMap() { 

     var data = new google.visualization.DataTable(); 

     data.addRows(24); 

     data.addColumn("string", "City"); 

     data.addColumn("number", "Numar anunturi");data.setValue(0, 0, 'Ilfov'); 

    data.setValue(0, 1, 19); 



    data.setValue(1, 0, 'Giurgiu'); 

    data.setValue(1, 1, 7); 



    data.setValue(2, 0, 'Brasov'); 

    data.setValue(2, 1, 6); 



    data.setValue(3, 0, 'Buzau'); 

    data.setValue(3, 1, 3); 



    data.setValue(4, 0, 'Valcea'); 

    data.setValue(4, 1, 3); 



    data.setValue(5, 0, 'Dolj'); 

    data.setValue(5, 1, 3); 



    data.setValue(6, 0, 'Neamt'); 

    data.setValue(6, 1, 2); 



    data.setValue(7, 0, 'Calarasi'); 

    data.setValue(7, 1, 2); 



    data.setValue(8, 0, 'Dambovita'); 

    data.setValue(8, 1, 2); 



    data.setValue(9, 0, 'Prahova'); 

    data.setValue(9, 1, 2); 



    data.setValue(10, 0, 'Braila'); 

    data.setValue(10, 1, 2); 



    data.setValue(11, 0, 'Constanta'); 

    data.setValue(11, 1, 2); 



    data.setValue(12, 0, 'Suceava'); 

    data.setValue(12, 1, 2); 



    data.setValue(13, 0, 'Caras-Severin'); 

    data.setValue(13, 1, 2); 



    data.setValue(14, 0, 'Cluj'); 

    data.setValue(14, 1, 2); 



    data.setValue(15, 0, 'Bihor'); 

    data.setValue(15, 1, 1); 



    data.setValue(16, 0, 'Bacau'); 

    data.setValue(16, 1, 1); 



    data.setValue(17, 0, 'Maramures'); 

    data.setValue(17, 1, 1); 



    data.setValue(18, 0, 'Arges'); 

    data.setValue(18, 1, 1); 



    data.setValue(19, 0, 'Gorj'); 

    data.setValue(19, 1, 1); 



    data.setValue(20, 0, 'Ialomita'); 

    data.setValue(20, 1, 1); 



    data.setValue(21, 0, 'Bucuresti'); 

    data.setValue(21, 1, 1); 



    data.setValue(22, 0, 'Mures'); 

    data.setValue(22, 1, 1); 



    data.setValue(23, 0, 'Sibiu'); 

    data.setValue(23, 1, 1); 





     var options = {width: 800,height:400}; 

     options["region"] = "RO"; 

     options["colors"] = [0xFF8747, 0xFFB581, 0xc06000]; //orange colors 

     options["dataMode"] = "markers"; 



     var container = document.getElementById("map_chart_div"); 

     var geomap = new google.visualization.GeoMap(container); 

     google.visualization.events.addListener( 
      geomap, "regionClick", function(e) { 
      console.log(e["region"]); 
      console.log(data.getValue(e["region"],1)); 
      }); 

     geomap.draw(data, options); 

    } 

    </script> 
</head> 
<body style="font-family: Arial;border: 0 none;"> 
<div id="map_chart_div" style="width: 800px; height: 400px;"></div> 
</body> 
</html> 
​ 
+2

,請複製粘貼更多的代碼。 – 2011-05-18 18:36:04

+0

是JavaScript嗎?如果是這樣,請重新標記。 – 2011-05-18 20:59:50

+0

你能準備一個完整的例子嗎?這樣,每個感興趣的人都可以自己運行它,並檢查它是如何工作/調試/等,你可能會得到更好的答案。 – 2011-05-18 21:01:41

回答

5

我不能與你發佈的代碼重現你的問題 - 我似乎無法得到regionClick函數完全觸發 - 但是你看到的錯誤信息讓我懷疑你的問題是e["region"]返回一個字符串,而不是一個數字。

嘗試添加該給你的函數:

google.visualization.events.addListener( 
      geomap, "regionClick", function(e) { 
      console.log(e["region"]); 
      console.log("Type of value is: " + typeof(e["region"])); 
      console.log(data.getValue(e["region"],1)); 
      }); 

然後,如果事實證明你得到一個字符串,你可以嘗試:

google.visualization.events.addListener( 
      geomap, "regionClick", function(e) { 
      console.log(e["region"]); 
      console.log("Type of value is: " + typeof(e["region"])); 
      console.log(data.getValue(Math.floor(e["region"]),1)); 
      }); 
+0

謝謝你,我從來沒有想過這件事。谷歌應該已經顯示了「」之間的值,以便我們可以看到它被解釋爲一個字符串。這解決了這個問題:console.log(data.getValue(Math.floor(e [「region」]),0)); (不得不用「0」代替「1」,我的不好)。 – Cristy 2011-05-22 20:06:54