google.load 导致空的 dom/screen [英] google.load causes empty dom/screen

查看:13
本文介绍了google.load 导致空的 dom/screen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加一个 google 异步可视化,但我遇到了问题.我已将其范围缩小到导致问题的 google.load.当 js 的 google.load 部分运行时,我得到一个空屏幕/dom.任何人都知道我做错了什么.

I'm trying to add a google visualization asynchronous, but I am running into problems. I've narrowed it down to the google.load causing the problem. When google.load part of the js runs, I get an empty screen/dom. Any one know what I am doing wrong.

我也尝试过使用 google.setOnLoadCallback,得到相同的结果.

I've also tried using the google.setOnLoadCallback, I get the same result.

任何帮助都会很棒

相关代码:

    $(document).ready(function () {
google.load('visualization', '1', { 'packages': ['geomap'] }, { 'callback': drawVisualization });


                function drawVisualization() {
                   $.ajax({
                        type: "POST",
                        data: "{'monitorId':'" + monitor + "','monitorName':'" + name + "','context':'" + context + "'}",
                        dataType: "json",
                        url: "WebService.asmx/LoadMonitorToolGeo",
                        contentType: "application/json; charset=utf-8",
                        processData: true,
                        success: function (msg) {

                            var obj = jQuery.parseJSON(msg.d);


                            // $(msg.d).hide().appendTo("#sortable").fadeIn();
                            $("#" + obj.context).find(".toolContent").hide().html(obj.data).fadeIn();

                            DrawWorldMap(obj.map, obj.context);

                        },
                        error: function (req, status, error) {

                        },
                        complete: function (req, status) {


                        }
                    });



function DrawWorldMap(response, id) {
    var data = new google.visualization.DataTable();
    data.addRows(response.d.length);
    data.addColumn('string', 'Country');
    data.addColumn('number', 'Popularity');
    for (var i = 0; i < response.d.length; i++) {
        data.setValue(i, 0, response.d[i].Country);
        data.setValue(i, 1, response.d[i].Popularity);
    }
    var options = {};
    options['dataMode'] = 'regions';

    var container = document.getElementById(id);
    var geomap = new google.visualization.GeoMap(container);
    geomap.draw(data, options);
}

});

推荐答案

这篇文章对我有帮助:

如何动态加载 Google Maps javascript API(按需加载)

这里的不同之处在于在 load 方法选项属性中定义了 callback:

What made the difference here was to define the callback in the load method options attribute:

var options = {packages: ['corechart'], callback : myCallback};
google.load('visualization', '1', options);

我相信这样 google.setOnLoadCallback(myCallback) 将不再需要,幸运的是,当 callback 添加时,该方法似乎不会清理页面.

I believe this way google.setOnLoadCallback(myCallback) won't be needed anymore and fortunately, it seems when callback is added the method won't cleanup the page.

这篇关于google.load 导致空的 dom/screen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆