Google地图自动填充不考虑边界

问题描述 投票:-1回答:2

我有一个输入字段,我用于autosuggest。这很好用,我也将AutoSuggest限制在特定范围内。但是,AutoSuggest不会考虑边界。我有以下代码

google.maps.event.addListenerOnce(map, 'idle', function() {
                var input = document.getElementById('userlocation');
                console.log(bounds);
                var options = {
                    bounds: bounds,
                    types: ['address']
                };
                autocomplete = new google.maps.places.Autocomplete(input, options);
            });

bounds是具有以下数据的LatLngBounds对象

j:38.15153503418 k:37.81086730957

j:23.63938140869095 k:23.958692550658952

这应该是雅典的边界,还有更多的东西。

我现在面临的问题是,当我输入“Athen”时,我也会收到来自德国,Malyisa,纽约等的条目,其中包含雅典这个词。

有人知道一些不好的事吗?

我也尝试过这个

autocomplete.bindTo('bounds', map);

这不起作用,并且还具有在用户更改边界时调整边界的功能。但我不希望动态更改视口。我该怎么办?

javascript google-maps google-places-api
2个回答
1
投票

我在雅典,希腊获得的界限是;

  "geometry": {
    "bounds": {
      "Ea": {
        "A": 37.9488181,
        "j": 38.0328563
      },
      "va": {
        "j": 23.68698619999998,
        "A": 23.7896925
      }

视口是:

    "viewport": {
      "Ea": {
        "A": 37.9488181,
        "j": 38.0328563
      },
      "va": {
        "j": 23.68698619999998,
        "A": 23.7896925
      }
    }

看起来你在google.maps.LatLngBounds构造函数中反转了坐标。

autocomplete example from the documentation modified to start limited to Greece (you can change the bias)

original reference

autocomplete seach for hotels in Athens, Greece


0
投票

老线程,但仍然存在,所以我的5个cts:

这就是我将边界传递给选项的方法

options.bounds = { east = eastLng, north = northLat, south = southLat, west = westLng };

不需要google.maps.LatLngBounds,这是一个LatLngBoundsLiteral,非常不适合记录,但它的效果很好。

© www.soinside.com 2019 - 2024. All rights reserved.