Google 地图地点 API:"Failed to execute 'write' on doc" 错误

Google Maps Places API: "Failed to execute 'write' on doc" error

我正在尝试加载 Google 地图地点 API,以便我可以将自动完成功能添加到我的搜索框。我本来尝试把写在页面上的脚本标签贴上去,但是我得到了错误"Execute write on doc: It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened." 我查了一下错误,很多消息来源说我需要在window.onload中加载脚本,所以我试了这样做但我仍然收到 "Execute write on doc" 错误。

<script type="text/javascript">
    var acService; 

    function loadScript() {
        debugger;
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'http://maps.google.com/maps/api/js?v=3&libraries=places!callback';
        document.body.appendChild(script);
        acService = new google.maps.places.AutocompleteService(),
            placesService = new google.maps.places.PlacesService(document.createElement('div'));

        $("input#location").autocomplete({
            source: function(req, resp) {

                acService.getPlacePredictions({
                    input: req.term,
                    types:['(regions)']
                }, function(places, status) {
                    if (status === google.maps.places.PlacesServiceStatus.OK) {
                        var _places = [];
                        for (var i = 0; i < places.length; ++i) {
                            _places.push({
                                id: places[i].place_id,
                                value: places[i].description,
                                label: places[i].description
                            });
                        }
                        resp(_places);
                    }
                });
            },
            select: function(e, o) {
                placesService.getDetails({
                    placeId: o.item.id
                }, function(place, status) {
                    if (status === google.maps.places.PlacesServiceStatus.OK) {
                        alert(o.item.value +
                            '\n is located at \n ' +
                            place.geometry.location.toUrlValue());
                    }
                });


            }
        });
    }

    window.onload = loadScript;
</script>

这是我尝试的第一个错误:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places"></script>
<input id="location" />

<script type="text/javascript">
    function bindAutocomplete() {

        var acService = new google.maps.places.AutocompleteService(),
          placesService = new google.maps.places.PlacesService(document.createElement('div'));

        $("input#location").autocomplete({
            source: function(req, resp) {

                acService.getPlacePredictions({
                    input: req.term,
                    types:['(regions)']
                }, function(places, status) {
                    if (status === google.maps.places.PlacesServiceStatus.OK) {
                        var _places = [];
                        for (var i = 0; i < places.length; ++i) {
                            _places.push({
                                id: places[i].place_id,
                                value: places[i].description,
                                label: places[i].description
                            });
                        }
                        resp(_places);
                    }
                });
            },
            select: function(e, o) {
                placesService.getDetails({
                    placeId: o.item.id
                }, function(place, status) {
                    if (status === google.maps.places.PlacesServiceStatus.OK) {
                        alert(o.item.value +
                          '\n is located at \n ' +
                          place.geometry.location.toUrlValue());
                    }
                });


            }
        });
    }
    $(window).load(bindAutocomplete);
</script>

这是不正确的:

script.src = 'http://maps.google.com/maps/api/js?v=3&libraries=places!callback';

根据 documentation 应该是:

script.src = 'https://maps.googleapis.com/maps/api/js?v=3&libraries=places&callback=initialize';

(不是 & 而不是 !