我正在尝试在 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>