【问题标题】:Google API for address autocompletion not working用于地址自动完成的 Google API 不起作用
【发布时间】:2016-01-11 00:00:55
【问题描述】:

我试图在我的 Siebel 应用程序中的一个文本字段上使用 Google API 实现地址自动完成功能。我不断收到错误“谷歌未定义”。我正在使用 jquery 以下是我的代码:

     PhoneChangePR.prototype.BindData = function(a) {
                SiebelAppFacade.PhoneChangePR.superclass.BindData.call(this, a);
$.getScript("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js", function() {
            console.log("Google maps successfully added");
            });
                  $.getScript("http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en-AU", function() {
            console.log("Second script loaded successfully");
            });


             var autocomplete = new google.maps.places.Autocomplete($("input[aria-labelledby=NewAddressLine1_Label]")[0], {});

                google.maps.event.addListener(autocomplete, 'place_changed', function() {
                    var place = autocomplete.getPlace();
                    console.log(place.address_components);
    });

请帮助。提前致谢。

【问题讨论】:

  • google not defined 通常在地图 api js 未加载时发生。因此,请确保您正确加载脚本。
  • 我从stackoverflow.com/questions/4061125/… 那里得到了解决方案。当我在一个在线平台上尝试时,它似乎有效。我所做的只是更改了元素的名称。
  • 我很好奇你在用 jQuery 做什么......当你提到 $.getScript() 时,你正在使用 jQuery,但是你正在加载 http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js......但是那时您的页面中一定已经有 jQuery 可用!?

标签: jquery google-maps google-api siebel


【解决方案1】:

您正在异步加载 google maps API,但您尝试在 API 加载之前调用 google.maps.places.Autocomplete 构造函数。

将所有依赖于正在加载的 API 的代码移动到成功回调中。

PhoneChangePR.prototype.BindData = function(a) {
    SiebelAppFacade.PhoneChangePR.superclass.BindData.call(this, a);
    $.getScript("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js", function() {
        console.log("jQuery successfully loaded");
    });

    $.getScript("http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en-AU", function() {
        console.log("Google maps successfully added");
        var autocomplete = new google.maps.places.Autocomplete($("input[aria-labelledby=NewAddressLine1_Label]")[0], {});

        google.maps.event.addListener(autocomplete, 'place_changed', function() {
            var place = autocomplete.getPlace();
            console.log(place.address_components);
        });
    });
});

您可能希望在地图 JS 成功加载时使用 Google 自己的回调,而不是依赖 jQuery 的成功回调,方法是将 callback URL 参数添加到加载地图 API 的请求中。试试这个:

PhoneChangePR.prototype.BindData = function(a) {
    SiebelAppFacade.PhoneChangePR.superclass.BindData.call(this, a);
    $.getScript("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js", function() {
        console.log("jQuery successfully loaded");
    });

    $.getScript("http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en-AU&callback=setupAutocomplete");
});

function setupAutocomplete() {
    console.log("Google maps successfully added");
    var autocomplete = new google.maps.places.Autocomplete($("input[aria-labelledby=NewAddressLine1_Label]")[0], {});

    google.maps.event.addListener(autocomplete, 'place_changed', function() {
        var place = autocomplete.getPlace();
        console.log(place.address_components);
    });
}

【讨论】:

  • 嗨,上述解决方案有时有效。但大多数时候我收到错误:Uncaught TypeError: Cannot read property 'Autocomplete' of undefined 你能建议我一个解决方案吗?谢谢
  • @GeetsuAnnJoseph 我已经用另一种方法更新了我的答案,一旦地图 js 加载完毕,就可以调用自动完成代码
  • 还是不行。我收到错误:未捕获的类型错误:window.setupAutocomplete 不是一个函数以及之前的错误。请帮助。谢谢
  • 应该没问题。可能与您的 JS 框架的设置方式有关。请参阅这个简单的工作示例jsfiddle.net/doktormolle/7cu2f
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 2021-04-19
  • 2017-12-22
  • 1970-01-01
  • 2019-01-10
相关资源
最近更新 更多