在自动填充功能中使用Google Maps Places API; API不起作用

Eri*_*ert 2 javascript ajax jquery google-maps autocomplete

我正在尝试将Google Maps Places API与搜索框联系起来.为了测试jquery自动完成,我使用了这个简单的例子:

function bindAutocomplete() {
    var locationSearch = $("input#Location");

    locationSearch.autocomplete({
        source: ["Massachusetts", "New Hampshire", "Boston"],
        minLength: 2
    });
}
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚如何使用谷歌地图API的结果作为来源,我不知道该怎么做.我可以在浏览器中从API获得结果:

https://maps.googleapis.com/maps/api/place/autocomplete/xml?types=(regions)&input=mas&key=xxx

这些是我尝试过的东西:

locationSearch.autocomplete({
    source: function (request, response) {
     $.getJSON("https://maps.googleapis.com/maps/api/place/autocomplete/json?types=(regions)&key=xxx&input=mass", {
            term: "mass"
        }, response),
    minLength: 2
});
Run Code Online (Sandbox Code Playgroud)

但我得到了"访问控制允许原因"安全错误.我尝试像这样做jsonp:

locationSearch.autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "https://maps.googleapis.com/maps/api/place/autocomplete/json?types=(regions)&key=xxx&input=mass",
            type: "GET",
            dataType: 'jsonp',
            cache: false,
            success: function (response) {
                console.log(response);
            }
        });
    },
    minLength: 2
});
Run Code Online (Sandbox Code Playgroud)

但是我在尝试记录响应时遇到错误,因为响应是json但是它期待jsonp.(另外,即使这确实有效,我也不确定如何将ajax调用的输出设置为源)

我尝试的最后一件事是:

locationSearch.autocomplete({
    source: new google.maps.places.Autocomplete("mass"),
    minLength: 2
});
Run Code Online (Sandbox Code Playgroud)

在我的页面上使用这些脚本:

<script src='http://maps.googleapis.com/maps/api/js?libraries=places&key=xxx'></script>
<script>$(bindAutocomplete)</script>
Run Code Online (Sandbox Code Playgroud)

但是当页面加载时,我得到错误"google is not defined"以及一堆错误,说"主线程上的同步XMLHttpRequest因其对最终用户体验的不利影响而被弃用"和"执行失败" 'on'Document':除非明确打开,否则无法从异步加载的外部脚本写入文档"

Dr.*_*lle 5

当您想通过JS请求这些数据时,您必须使用Places-libraryAutocompleteService

采样implemetation:

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);
Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places"></script>
<input id="location" />
Run Code Online (Sandbox Code Playgroud)

注意:此服务不支持types-filter

但是:places-library也包含自动填充功能