未定义随机Google错误

时间:2018-08-07 12:23:46

标签: javascript jquery google-api

几次重新加载页面时,出现以下错误。

  

未捕获的ReferenceError:未定义google

这不是我总是得到的错误。

以下是我的JavaScript代码:

$('document').ready(function () {
        var apiKey = '@System.Configuration.ConfigurationManager.AppSettings["googleAPIKey"]';
        var language = $('#Language').text();
        var n = language.indexOf('-');
        language = language.substring(0, n != -1 ? n : language.length);
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.id = "gogapi"
        s.src = "https://maps.googleapis.com/maps/api/js?key=" + apiKey + "&libraries=places&language=" + language + "&callback=initialize";
        document.head.appendChild(s);
        if ($("#Country").val() != '') {
            updateCityLoad($("#Country").val(), "City");
        }
    });


         function updateCityLoad(country, city) {
                getCountryCode(country, function (code) {
                    initialize(code, city)
                });
        }

        function getCountryCode(countryId, callback) {
            var countryCode = "";
            $.getJSON('@Url.Action("GetCountryCode", "TestController")' + "&countryId=" + countryId, function (result) {
                countryCode = result.data;
                callback(countryCode);
            });
        }

                function initialize(country, cityID) {
                    var cityfield = document.getElementById(cityID);
                    var options = {
                        types: ['(cities)'],
                        componentRestrictions: { country: country },
                    };
                    var autocomplete = new google.maps.places.Autocomplete(cityfield, options);
                    google.maps.event.addListener(autocomplete, 'place_changed', function () {
                        place = autocomplete.getPlace();
                        $(cityfield).val(place.address_components[0].long_name);
                    })
        }

我正在做的是在更改国家/地区字段并加载页面时调用upDateCity函数,如果国家/地区字段不为空,请加载google api。是在对document.ready调用函数updateCityLoad时,我收到了google错误。

有什么解决办法的想法吗?

1 个答案:

答案 0 :(得分:1)

我敢打赌,在您的updateCityLoad中调用$(document).ready(){}之前,没有及时检索到您尝试引用的脚本(googleapi)。

您可以在调用之前设置超时,可以在html标记的标题标签中呈现脚本,也可以在附加操作之后再次尝试调用$(document).ready()。像...

$('document').ready(function () {
    var apiKey = '@System.Configuration.ConfigurationManager.AppSettings["googleAPIKey"]';
    var language = $('#Language').text();
    var n = language.indexOf('-');
    language = language.substring(0, n != -1 ? n : language.length);
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.id = "gogapi"
    s.src = "https://maps.googleapis.com/maps/api/js?key=" + apiKey + "&libraries=places&language=" + language + "&callback=initialize";
    document.head.appendChild(s);

    $(document).ready(function(){//additional call here to wait for document to load
        if ($("#Country").val() != '') {
        updateCityLoad($("#Country").val(), "City");
        }
    });
});