19

カスタムオートコンプリートフィールドを設定して、検索クエリに一致するGoogleプレイスの場所とデータベースのイベントを表示しています。このため、Places Autocompleteをテキストフィールドに直接接続するのではなく、GooglePlacesAutocompleteサービスを使用してクエリ予測を取得しています。

問題は、オートコンプリートサービスを使用して、国ごとにプレイスオートコンプリートの候補をフィルタリングする方法がわからないことです。

私はもう試した:

var service = new google.maps.places.AutocompleteService(null, {
        types: ['cities'],
        componentRestrictions: {country: "au"}
    });

ただし、ドイツとフランスのオートコンプリートオプションは引き続き表示されます:(

助言がありますか?

4

5 に答える 5

56

サービスを作成するときではなく、サービスを呼び出すときに制限を渡す必要があります。ここ:

//create service
service = new google.maps.places.AutocompleteService();

//perform request. limit results to Australia
var request = {
    input: 'Brisbane',
    componentRestrictions: {country: 'au'},
};
service.getPlacePredictions(request, callback);
于 2013-03-09T11:56:53.490 に答える
8

AutocompletionRequestを使用して、タイプと componentRestrictions を指定できます。ここに例があります -

var service = new google.maps.places.AutocompleteService();

    service.getPlacePredictions({ input: query, types: ['geocode'], 
                                componentRestrictions:{ country: 'us' } },
            function (predictions, status) {
                if (status == google.maps.places.PlacesServiceStatus.OK) {
                    var results = document.getElementById('results');
                    for (var i = 0, prediction; prediction = predictions[i]; i++) {
                        results.innerHTML += '<li>' + prediction.description + '</li>';
                    }
                }
            });
于 2013-06-01T13:45:10.083 に答える
1

ドキュメントリファレンスで概説されているように、 PlacesLibraryはAutocompleteOptionsAutocompleteServiceをサポートしていません。これが価値のある機能であると思われる場合は、PlacesAPI-FeatureRequestを提出してください。

于 2013-02-05T04:00:32.503 に答える
-1

この作業コードを使用してください

var input = document.getElementById("query_value");
var autocomplete = new google.maps.places.Autocomplete(input, {
  componentRestrictions: { country: "IN" },
  place_id: "YOUR_PLACE_ID"
});
google.maps.event.addListener(autocomplete, "place_changed", function() {
  var place = autocomplete.getPlace();
});


https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places&language=en&key=YOUR_KEY
于 2017-07-31T05:34:51.283 に答える