【问题标题】:GoogleMaps v3 autocomplete.getBounds() returns undefined after being setGoogleMaps v3 autocomplete.getBounds() 设置后返回 undefined
【发布时间】:2016-02-23 19:59:07
【问题描述】:

我在尝试将 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()

编译成以下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());
    });
  };

图钉正确放置在地图上,但无论我添加多少地方,console.logs 总是返回:

autocomplete bounds (before): undefined
autocomplete bounds (after): ((9.923577823579402, -84.09528446042509), (9.932560976420598, -84.08616473957488))

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

我们用来测试结果是否有偏差的测试用例是: 1. 在自动完成中输入“San Jose”(最好的结果应该在美国) 2. 在地图上添加“利蒙,哥斯达黎加”作为标记 3. 在自动完成中输入“San Jose”,最上面的结果应该是 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)

【问题讨论】:

    标签: javascript google-maps-api-3 autocomplete coffeescript


    【解决方案1】:

    您的自动完成功能似乎缺少地图所需的边界初始化

    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()
    

    或通过特定位置

    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);
    

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

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

    【讨论】:

    • 太棒了!我能够得到你的第一个解决方案,autocomplete.bindTo 工作。但是,这会绑定地图当前视口,并尝试将其绑定到用户放置在地图上的 LAST 引脚。您的第二个解决方案始终将第一个 console.log 设置为 defaultBounds。每次更改地点后,如何设置biasCircle的边界?
    • 不是 100% 肯定明白你的意思;如果您设置默认范围,我们同意您需要从侦听器回调外部进行设置,否则它将始终覆盖用户输入。除此之外,当您从biasCircle 设置边界时,我看不出有什么问题。
    • 编辑了我的问题以包含我当前的代码。我尝试在侦听器回调之外设置边界,但我得到了我原来的错误(每次第一次调用都未定义)。
    猜你喜欢
    • 2018-03-15
    • 1970-01-01
    • 2019-03-19
    • 2022-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 2022-08-17
    相关资源
    最近更新 更多