谷歌可视化世界地图

时间:2014-11-08 06:05:14

标签: php json google-maps

我尝试使用json检索结果,以便显示带有国家/地区列表的世界地图。

这是我到目前为止所得到的:

function drawRegionsMap() {
    $.ajax({
        url: 'getlist.php",
        dataType: "json"
    }).done(function(result) {
        var data = google.visualization.arrayToDataTable(result);

        var view = new google.visualization.DataView(data)
        view.setColumns([0, 1])
        var chart = new google.visualization.GeoChart(
            document.getElementById('map'));
        chart.draw(data, options);

        var geochart = new google.visualization.GeoChart(
            document.getElementById('map'));
        var options = {
            width: "auto",
            height: "auto",
            colorAxis: {
                colors: ['#E4B6D3', '#E06D94']
            } // Map Colors 
        };

        geochart.draw(data, options);
    });

};

google.load('visualization', '1', {
    'packages': ['geochart']
});
google.setOnLoadCallback(drawRegionsMap);
从getlist.php返回的

结果是json格式:

{
   "Country":[
      "Germany",
      "United States",
      "Brazil",
      "France",
      "RU"
   ],
   "Hits":[
      200,
      300,
      400,
      500,
      600,
      700
   ]
}

php代码:

  $data = array();
  $data['Country'] = array();
  $data['Hits'] = array();

  $country = array('Germany','United States','Brazil','France','RU');
  $hits = array(200,300,400,500,600,700);

  for ($i = 0; $i < 6; $i++){
      $data['Country'] = $country;
      $data['Hits'] = $hits;
  }

echo json_encode($data);

在firebug控制台中查看,我总是得到:错误:不是数组 我已经试图解决这个难题几个小时,但没有成功。

1 个答案:

答案 0 :(得分:0)

返回的结果是一个对象,而不是一个数组。

预期的数组是:

[
 ["Country","Hits"],
 ["Germany",200],
 ["United States",300],
 ["Brazil",400],
 ["France",500],
 ["RU",600]
]

修改返回此数组的PHP代码:

<?php
  $data = array(['Country','Hits']);


  $country = array('Germany','United States','Brazil','France','RU');
  $hits = array(200,300,400,500,600,700);

  foreach ($country as $k => $v){
      $data[] = array($v,$hits[$k]);
  }

  echo json_encode($data);
?>