簡體   English   中英

谷歌地圖自動完成邊界不起作用

[英]google maps autocomplete bounds not working

我想在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國家/地區。 但我想限制在一個國家的地區搜索。 例如一個城市范圍。 但其他地址正在走出這個界限。 不考慮界限。

問題是LatLngBounds需要西南角和東北角。 也稱為左下角和右上角。

代替:

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

它應該是:

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

更新於10/2018:這個答案已經過時; 請看其他答案

它不會按你的意願工作。 這是文檔說的:

搜索地點的區域。 結果偏向於但不限於這些范圍內包含的位置。

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

也許當你使用邊界時,你不能使用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

Bounds僅適用於maps API,而不適用於Places API。

對於Places API中的位置偏差 ,您需要在選項中使用位置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'}
        };

請注意, 這只會將自動完成結果偏向您輸入的半徑; 它不會將結果限制在該半徑范圍內。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM