GoogleMaps v3 autocomplete.getBounds()在设置后返回undefined

CHa*_*awk 9 javascript autocomplete google-maps-api-3 coffeescript

我在尝试将Google地图自动填充的结果偏向地图上的最后一个引脚时遇到问题.我在这里关注Google的教程:https: //developers.google.com/maps/documentation/javascript/places-autocomplete#change_search_area

我的CoffeeScript代码如下:

initSearch = ->
  autocomplete = new (google.maps.places.Autocomplete)(document.getElementById('location-query'), types: [ 'geocode' ])
  autocomplete.addListener 'place_changed', ->
    place = autocomplete.getPlace()
    addDestination(place, map, insertIndex) #places pin on map

    console.log 'autocomplete bounds (before): ' + autocomplete.getBounds()
    #setting bounds around new place
    biasCircle = new google.maps.Circle
      center: place.geometry.location
      radius: 500 #in kilometers

    autocomplete.setBounds(biasCircle.getBounds())
    console.log 'autocomplete bounds (after): ' + autocomplete.getBounds()
Run Code Online (Sandbox Code Playgroud)

其中编译成以下javascript:

initSearch = function() {
    var autocomplete;
    autocomplete = new google.maps.places.Autocomplete(document.getElementById('location-query'), {
      types: ['geocode']
    });
    return autocomplete.addListener('place_changed', function() {
      var biasCircle;
      place = autocomplete.getPlace();
      addDestination(place, map, insertIndex);

      console.log('autocomplete bounds (before): ' + autocomplete.getBounds());
      biasCircle = new google.maps.Circle({
        center: place.geometry.location,
        radius: 500
      });
      autocomplete.setBounds(biasCircle.getBounds());
      return console.log('autocomplete bounds (after): ' + autocomplete.getBounds());
    });
  };
Run Code Online (Sandbox Code Playgroud)

该引脚已正确放置在地图上,但无论我添加多少个地方,console.logs始终返回:

autocomplete bounds (before): undefined
autocomplete bounds (after): ((9.923577823579402, -84.09528446042509), (9.932560976420598, -84.08616473957488))
Run Code Online (Sandbox Code Playgroud)

正确设置了边界(如第二个所示console.log),但结果实际上并没有偏差,第一个结果总是未定义.

我们用来测试结果是否存在偏差的测试用例是:1.在自动填充中键入"San Jose"(最佳结果应该在美国)2.在地图上添加"Limon,Costa Rica"作为标记3.在自动填充中键入"San Jose",最佳结果应该是圣何塞,COSTA RICA

我们认为这可能是一个范围问题,所以我们window.autocomplete到处尝试并且仍然遇到同样的问题.我们还尝试在单独的方法中设置自动完成边界(而不是place_changed事件,但这也不起作用).

预先感谢您的帮助!

更新:尝试Cedric的答案 在Cedric的建议中,我试图在侦听器回调之外设置边界(方法是从侦听器内调用的),但我仍然遇到同样的问题.新代码:

#Sets autocomplete bias
setAutocompleteBias = (place) ->
  console.log 'autocomplete bounds (before): ' + window.autocomplete.getBounds()
  #setting bounds around new place
  biasCircle = new google.maps.Circle
    center: place.geometry.location
    radius: 500 #in kilometers

  window.autocomplete.setBounds(biasCircle.getBounds())
  console.log 'autocomplete bounds (after): ' + window.autocomplete.getBounds()

window.autocomplete = null
#Autocomplete search box initialization
initSearch = ->
  window.autocomplete = new (google.maps.places.Autocomplete)(document.getElementById('location-query'), types: [ 'geocode' ])
  #autocomplete.bindTo('bounds', map)
  window.autocomplete.addListener 'place_changed', ->
    place = window.autocomplete.getPlace()

    addDestination(place, map, insertIndex)

    setAutocompleteBias(place)
Run Code Online (Sandbox Code Playgroud)

Céd*_*and 3

看来您的自动完成功能缺少地图上所需的边界初始化

initSearch = ->
  autocomplete = new (google.maps.places.Autocomplete(                  
    document.getElementById('location-query'), types: [ 'geocode' ])
  autocomplete.bindTo('bounds', map)
  autocomplete.addListener 'place_changed', ->
    place = autocomplete.getPlace()
    addDestination(place, map, insertIndex) #places pin on map

    console.log 'autocomplete bounds (before): ' + autocomplete.getBounds()
    #setting bounds around new place
    biasCircle = new google.maps.Circle
      center: place.geometry.location
      radius: 500 #in kilometers
    autocomplete.setBounds(biasCircle.getBounds())
    console.log 'autocomplete bounds (after): ' + autocomplete.getBounds()
Run Code Online (Sandbox Code Playgroud)

或通过特定位置

var defaultBounds = new google.maps.LatLngBounds(
  new google.maps.LatLng(-33.8902, 151.1759),
  new google.maps.LatLng(-33.8474, 151.2631));

var input = document.getElementById('searchTextField');
var options = {
  bounds: defaultBounds,
  types: ['establishment']
};
autocomplete = new google.maps.places.Autocomplete(input, options);
Run Code Online (Sandbox Code Playgroud)

教程中的链接https://developers.google.com/maps/documentation/javascript/places-autocomplete#add_autocomplete

文档链接https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete