我想使用JSON数据为Google Maps中的国家/地区着色。目前,我已将所有内容“硬编码”:
<script type="text/javascript">
google.charts.load('current', {'packages':['geochart']});
google.charts.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable([
['Country'],
['China'],
['Taiwan'],
['Malaysia'],
]);
var options = {
backgroundColor: 'none',
defaultColor: '#F27935'
};
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(data, options);
}
</script>
该代码绘制以下图像:
现在,我想为其使用JSON文件,因为我还有其他要显示的数据。到目前为止,我拥有的JSON:
{
"Chinese": {
"Country": {
"China": {},
"Taiwan": {},
"Malaysia": {}
}
},
"English": {
"Country": {
"United States": {},
"United Kingdom": {},
"Australia": {},
"New Zealand": {}
}
}
}
我也想在特定页面上显示特定数据。例如,在Chinese.php上,我想在JSON中显示数据“ Chinese”。
我应该使用if语句吗?我以为这段代码可能会有所帮助...
if(document.URL.indexOf("chinese.php") >= 0){
...json code here
}
我的问题:如何在Google Maps中加载仅请求特定页面的特定部分的JSON文件。
以下代码解决了我的问题。我为此修复程序使用了单独的JSON文件。
function drawcountryMap() {
var options = {
backgroundColor: 'none',
defaultColor: '#F27935'
};
$.ajax({
url: "cn.json",
dataType: "JSON"
}).done(function(data) {
var countryArray = [["Country"]];
$.each(data.country, function() {
var countryitem = [this.name];
countryArray.push(countryitem);
});
var countryData = google.visualization.arrayToDataTable(countryArray);
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(countryData, options);
});
}
google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawcountryMap);
我的JSON文件
{ "country":
[
{"name":"Taiwan"},
{"name":"China"},
{"name":"Malaysia"}
] }
得到相同的结果:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句