2013-07-04 184 views
0

這很難解釋,所以請裸露在我身邊。谷歌地圖圖表

我使用Google的地理圖表API,並使用json更新數據。

這是我的地圖JS(map.php):

google.load('visualization', '1', {'packages': ['geochart']}); 
google.setOnLoadCallback(drawRegionsMap); 

    function drawRegionsMap() { 

    var jsonData = $.ajax({ 
     url: "includes/_ajax_home_map.php?metric=Clicks", 
     dataType:"json", 
     async: false 
     }).responseText; 

    var data = new google.visualization.DataTable(jsonData);  
    var options = { 
     //region: '009', 
     backgroundColor: 'EAF7FE', 
     colorAxis: {colors: ['910101']} 
     }; 

    var chart = new google.visualization.GeoChart(document.getElementById('chart_div')); 
    chart.draw(data, options); 
}; 

這是我的PHP產生JSON(_ajax_home_map.php):

$data = '{ 
    "cols": [ 
     {"id":"","label":"Country","pattern":"","type":"string"}, 
     {"id":"","label":"Popularity","pattern":"","type":"number"} 
     ], 
    "rows": ['; 


$metric = $_REQUEST['roi_tag']; 

$sql = "SELECT SUM($metric) as '$metric',tag_1 FROM Dashboard_ClientsCampaigns_Today_HoH WHERE tag_1 != '' GROUP BY tag_1"; 
$sql = mysql_query($sql); 

$count = 1; 


while($row = mysql_fetch_array($sql)){ 
    $tag_1 = strtoupper($row["tag_1"]); 
    if($tag_1 == "UK")$tag_1 = "GB"; 
    if($count != 1)$data .= ","; 
    $count++; 
    $data .= '{"c":[{"v":"'.$tag_1.'","f":null},{"v":'.$row[$metric].',"f":null}]}'; 
} 

$data .= ']}'; 

echo $data; 

現在,當我連接這兩個,我只是得到一張空白的地圖。沒有數據在那裏。但是..有一些奇怪的部分。如果我直接瀏覽到我的PHP JSON,我得到這個:

{ "cols": [ {"id":"","label":"Country","pattern":"","type":"string"}, {"id":"","label":"Popularity","pattern":"","type":"number"} ], "rows": [{"c":[{"v":"AU","f":null},{"v":139,"f":null}]},{"c":[{"v":"CZ","f":null},{"v":3,"f":null}]},{"c":[{"v":"DE","f":null},{"v":4,"f":null}]},{"c":[{"v":"DK","f":null},{"v":978,"f":null}]},{"c":[{"v":"ES","f":null},{"v":32,"f":null}]},{"c":[{"v":"HU","f":null},{"v":2,"f":null}]},{"c":[{"v":"IE","f":null},{"v":65,"f":null}]},{"c":[{"v":"IT","f":null},{"v":5081,"f":null}]},{"c":[{"v":"PT","f":null},{"v":4452,"f":null}]},{"c":[{"v":"RO","f":null},{"v":16,"f":null}]},{"c":[{"v":"RS","f":null},{"v":0,"f":null}]},{"c":[{"v":"RU","f":null},{"v":0,"f":null}]},{"c":[{"v":"SE","f":null},{"v":69,"f":null}]},{"c":[{"v":"GB","f":null},{"v":28123,"f":null}]}]} 

如果我藉此輸出JSON,複製,家就在它自己的文件(data2.php),然後讓我的地圖JS來訪問它,它完美地填充地圖。

我怎麼可以有2個相同的JSON,只有一個谷歌圖表工作?

如果任何人有任何想法,以幫助我再次得到持續,這將是可愛..

感謝

回答

1

也許是因爲你試圖用錯誤的PARAM

_ajax_home_map.php?metric=Clicks 

加載PHP但在你的代碼查找

$metric = $_REQUEST['roi_tag']; 

嘗試

$metric = $_REQUEST['metric']; 

..代替。順便說一句 - JSON的運作很好!那不是問題。

+0

謝謝sooo多。我拉着我的頭髮! – user1894292