2016-04-28 6 views
0

google.maps.places.AutocompleteServiceを使用すると、検索国(カナダとアメリカ)をどのように制限できますか。 AutoCompleteを使用している場合は、その国をオプションに追加することはできますが、それはAutocompleteServiceを使用しているようには見えません。私はまた、ドキュメントcomponentRestrictionsを見ている間に見つかったが、それも動作しません。AutocompleteService()を使用してGoogleマップの場所を特定の国に限定する

Googleプレイスを使用して検索する国を制限するにはどうすればよいですか?

function initService(controlInput) { 

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

    var displaySuggestions = function(predictions, status) { 

     if (status != google.maps.places.PlacesServiceStatus.OK) { 
      console.log(status); 
      return; 
     } 

     predictions.forEach(function(prediction) { 
      console.log(prediction); 
     }); 
    }; 

    controlInput.addEventListener('keyup', function(event, input) { 
     service.getQueryPredictions({ 
     input: controlInput.value, 
     //country: 'ca', <--- doesn't work 
     componentRestrictions: { 
      country: 'ca' <--- also doesn't work 
     } 
     }, displaySuggestions); 
    }); 
} 
+0

がservice.setComponentRestrictionsのようなもの({国:「CA」})してみてください –

+0

こんにちは@UriBrecherを、私は実際にサービスにそれを装着していることを試してみましたが、それは 'service.setComponentRestrictionsがfunction' – mtpultz

+0

ないスロー私の悪い。 setComponentRestrictionsは、AutoCompleteServiceではなく、AutoCompleteクラスのメソッドです。 「うまくいかない」と言うと、予測は制限されていないということですか?それとも予測が得られないのでしょうか? –

答えて

0

Component Filteringから、このガイドを、次の試してみてください。

ジオコーディングに応じて、GoogleマップのジオコーディングAPIは、特定の領域に限定されたアドレスの結果を返すことができます。制限はコンポーネントフィルタを使用して指定します。フィルタは、パイプ(|)で区切られたコンポーネント:値のペアのリストで構成されます。すべてのフィルタに一致する結果のみが返されます。フィルタ値は、他のジオコーディング要求と同様のスペル修正と部分一致の方法をサポートします。ジオコーディング結果がコンポーネントフィルターの部分一致である場合、応答にpartial_matchフィールドが含まれます。濾過することができる

コンポーネントが含まれる:

  • 経路は、経路の長いか短い名前に一致します。
  • ローカリティは、ローカリティタイプとサブローカルタイプの両方に一致します。
  • administrative_areaはすべてのadministrative_areaレベルと一致します。
  • postal_codeは、postal_codeとpostal_code_prefixに一致します。
  • 国は国名または2文字の国コードと一致しますISO 3166-1国コード。

:各アドレスコンポーネントのみアドレスパラメータまたは成分フィルタのいずれかとして指定され、両方ではないことができます。そうすると、ZERO_RESULTSになることがあります。

"Santa Cruz"のジオコードはcomponents=country:ESで、カナリア諸島、スペインのSanta Cruz de Tenerifeを返します。要求:それは検索の制限についての実装だとISO 3166-1(2文字の国コード)に従う方法についても、ここでRestrict the search to a specific country

https://maps.googleapis.com/maps/api/geocode/json?address=santa+cruz&components=country:ES&key=YOUR_API_KEY 

。私はこれが役立つことを願っています

0

componentRestrictionsオプションを受け付けないAutocompleteService.getQueryPredictionsメソッドを使用しています。 AutocompleteService.getPlacePredictionsメソッドで有効です。

AutocompleteService: https://developers.google.com/maps/documentation/javascript/reference#AutocompleteService

AutocompletionRequest: https://developers.google.com/maps/documentation/javascript/reference#AutocompletionRequest

QueryAutocompletionRequest: https://developers.google.com/maps/documentation/javascript/reference#QueryAutocompletionRequest

0

私はautocomplete.setComponentRestrictionsの値を設定するには、国選択オプションを使用していたドキュメントを参照してください。

オートコンプリートは、その国からの検索に限定されます。 フォーマットを書き留めておきますので、ここに書き込んでください。

function setAutocompleteCountry() { 
    var country = document.getElementById('id_countryselect').value; 
    if (country === 'ALL') { 
     autocomplete.setComponentRestrictions({'country': []}); 
    } else { 
     autocomplete.setComponentRestrictions({'country': country}); 
    } 
} 

autocomplete.addListener('place_changed', function() { 
     var place = autocomplete.getPlace(); 
    ------> Rest of code 
関連する問題