【问题标题】:jVectorMap Issue while loading ajax data加载 ajax 数据时出现 jVectorMap 问题
【发布时间】:2015-02-18 01:45:32
【问题描述】:

我正在尝试在使用 jvectormap 插件创建的地图中显示来自访问者的数据。

这快把我逼疯了,我无法通过 ajax 加载数据,如果我手动输入数据就可以了。

到目前为止,我有这个:

map.php

$datos = array();
$link->set_charset("utf8");
$sql = $link->query("SELECT SUM(ID) as visitors, state FROM visitors WHERE state != '' GROUP BY state");
while($row = $sql->fetch_row()){
    $ss = $link->query("SELECT * FROM states WHERE state = '".$row[1]."'");
    $rr = $ss->fetch_row();
    $datos[] = array("ccode" => $rr[2], "visits" => $row[0]);
}
$data = array("countries" => $datos);
echo json_encode($data,JSON_NUMERIC_CHECK);

这将返回以下数据:

{"countries":[{"ccode":"VE-A","visits":81},{"ccode":"VE-L","visits":24}]}

现在加载地图的功能:

function cargaMapa(){
        //jvectormap data
    $.post("ajax/map.php",{},function(mapa){
        var dataC = eval(mapa);
        //var dataC = {"countries":[{"ccode":"VE-A","visits":81},{"ccode":"VE-L","visits":24}]};
        var countryData = []; 
        //for each country, set the code and value
        $.each(dataC.countries, function() {
            countryData[this.ccode] = this.visits;
            console.log("Estado: "+this.ccode+" Visitas: "+this.visits);
        });
        //World map by jvectormap
        $('#world-map').vectorMap({
            map: 've_mill_en',
            backgroundColor: "#fff",
            regionStyle: {
                initial: {
                    fill: '#e4e4e4',
                    "fill-opacity": 1,
                    stroke: 'none',
                    "stroke-width": 0,
                    "stroke-opacity": 1
                }
            },
            series: {
                regions: [{
                        values: countryData,
                        scale: ["#3c8dbc", "#2D79A6"], //['#3E5E6B', '#A6BAC2'],
                        normalizeFunction: 'polynomial'
                    }]
            },
            onRegionLabelShow: function(e, el, code) {
                //search through dataC to find the selected country by it's code
                var country = $.grep(dataC.countries, function(obj, index) {
                    return obj.ccode == code;
                })[0]; //snag the first one
                //only if selected country was found in dataC
                if (country != undefined) { 
                    el.html(el.html() + ': ' + country.ccode + country.visits + ' visitas');
                }
            }
        });
    });
}

正如您在函数中看到的那样,我有 var dataC,如果我在其中加载来自 map.php 的数组,它会给我Uncaught SyntaxError: Unexpected token : 但如果将 map.php 的结果复制并粘贴到 var dataC 中效果不错。

我该如何解决这个问题?

感谢您的帮助

谢谢

【问题讨论】:

    标签: javascript php jquery ajax jvectormap


    【解决方案1】:

    我想通了,只是将$.post 更改为$.getJSON,然后魔法开始了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 2016-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      相关资源
      最近更新 更多