谷歌地图自动完成边界不起作用

时间:2013-09-20 06:55:50

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

我想在Google地图服务上使用自动填充边界。但是没有工作。

//----autocomplete start
var input = (document.getElementById('searchPlace'));
var defaultBounds = new google.maps.LatLngBounds(
            new google.maps.LatLng(40.518, 29.215),
            new google.maps.LatLng(41.242, 30.370));

var options = {
            bounds:defaultBounds,
            componentRestrictions: { country: 'XX'},};

var autocomplete = new google.maps.places.Autocomplete(input, options);
//----autocomplete end

当我输入文本框时,地址将来自XX国家/地区。但我想限制在一个国家的地区搜索。例如一个城市范围。但其他地址正在走出这个界限。不考虑界限。

5 个答案:

答案 0 :(得分:5)

问题是LatLngBounds期望西南角和东北角。也称为左下角和右上角。

而不是:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>

它应该是:

var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(40.518, 29.215),
    new google.maps.LatLng(41.242, 30.370));

答案 1 :(得分:2)

更新于10/2018:此答案已过时;请参阅其他答案

它不会按你的意愿工作。这就是文档所说的:

  

搜索地点的区域。结果偏向于,   但不限于这些范围内包含的地方。

https://developers.google.com/maps/documentation/javascript/places-autocomplete#address_forms

答案 2 :(得分:0)

也许当你使用边界时,你不能使用componentRestrictions ......

试试这个..

    //----autocomplete start
    var input = (document.getElementById('searchPlace'));
    var defaultBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(40.518, 29.215),
        new google.maps.LatLng(41.242, 30.370));

    var options = {
        bounds:defaultBounds
    };
    var autocomplete = new google.maps.places.Autocomplete(input, options);
    //----autocomplete end

答案 3 :(得分:0)

Bounds仅适用于maps API,而不适用于Places API。

对于Places API中的location biasing,您需要在选项中使用位置latlng对象和半径(以米为单位)。

        var myLatlng = new google.maps.LatLng(35.9907,-84.0497);

        var acOptions = {
            location: myLatlng,
            radius: 150000,  // (in meters; this is 150Km)
            types: ['address'],
            componentRestrictions: {country: 'us'}
        };

请注意,此仅将自动填充结果偏向您输入的半径; 不会将结果限制到该半径。

答案 4 :(得分:0)

var defaultBounds = new google.maps.LatLngBounds(
                new google.maps.LatLng(17.2916377 ,78.2387067),
                new google.maps.LatLng(17.5608321, 78.6223912)
);

var input = document.getElementById('destination');
var options = {
  bounds: defaultBounds,
  types: ['establishment'],
  strictBounds: true
};

var destination_autocomplete = new google.maps.places.Autocomplete(input, options)