0

我正在尝试在 Google 的 Geochart 上绘制一些城市。这个函数工作得很好,可以绘制城市。

function drawMarkersMap() {
  var newInfo = google.visualization.arrayToDataTable([
   ['City'],
   ['San Diego'],
   ['Los Angeles'],
   ['San Jose']
  ]);

虽然这很好,但我正在通过 php 从我的数据库中生成我的城市。所以,假设我们有这个 php 数组

<?php $city = array("San Diego", "Los Angeles", "San Jose"); ?>

现在到了我正在挣扎的部分。我似乎无法让循环正常工作,以便我可以分别输出每个城市。

当我这样做时,只有数组中的第一个城市(圣地亚哥)会显示,我明白为什么......我只是表明这种方法至少会打印“一些东西”

function drawMarkersMap() {
  var newInfo = google.visualization.arrayToDataTable([
    ['City'],
    ['<?php foreach($city as $location){ echo json_encode($location); } ?>']
]);
//options here
}

所以,我正在尝试使用javascript循环并打印数组中的每个项目,但我似乎没有正确的逻辑

function drawMarkersMap() {
  var newInfo = google.visualization.arrayToDataTable([
    ['City'],
    for(i=0;i<json_encode($city).count;i++){
     //some kind of print statement mixing php and javascript. I need help here please!
     }
]);

这是整个页面

<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>

<?
$city = array("San Jose","Los Angeles","San Diego");
foreach($city as $key=>$value) {
     echo "$value";
}  
?>
<script type="text/javascript">
 google.load('visualization', '1.1', {packages: ['geochart']});


function drawMarkersMap() {
  var newInfo = google.visualization.arrayToDataTable([
    ['City'],
['<?php echo json_encode($city);  ?>']
]);

var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));

chart.draw(newInfo, {width: 556, height: 347, displayMode: 'markers', region: 'US-CA', resolution: 'provinces'});
}


google.setOnLoadCallback(drawMarkersMap);
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<div id="chart_div"></div>
</body>
</html>
4

2 回答 2

2
<?php $city = array(array('City'), array('San Diego'), array('Los Angeles'), array('San Jose')); ?>

var newInfo = google.visualization.arrayToDataTable(<?php echo json_encode($city); ?>);

这将重新创建与原始脚本完全相同的数组结构。

于 2012-05-18T01:24:48.950 回答
1

你为什么不使用内功能?

<script type="text/javascript">
<?php $city = array("San Diego", "Los Angeles", "San Jose"); ?>
    function drawMarkersMap() {
        var newInfo = google.visualization.arrayToDataTable([
        ['City'],
        ['<?php echo implode("'],\n\t\t['",$city) ?>']
        ]);
    }
</script>

这将导致

<script type="text/javascript">
    function drawMarkersMap() {
        var newInfo = google.visualization.arrayToDataTable([
        ['City'],
        ['San Diego'],
        ['Los Angeles'],
        ['San Jose']]);
    }
</script>

根据要求……</p>

<script type="text/javascript">
<?php $city = array("San Diego", "Los Angeles", "San Jose"); ?>
    function drawMarkersMap() {
        var newInfo = google.visualization.arrayToDataTable([
        ['City'],
        ['<?php foreach($city as &$c)$c=htmlspecialchars($c,ENT_QUOTES,'UTF-8'); echo implode("'],\n\t\t['",$city); ?>']
        ]);
    }
</script>

..虽然我应该同意使用 JSON 非常实用的事实:)

于 2012-05-18T01:36:52.730 回答