2014-02-10 76 views
1

我有下面的代碼,我已經操縱了一段時間,但我根本無法得到它的返回結果..自動完成國家列表使用谷歌地圖API

<script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places" type="text/javascript"></script> 
    <script type="text/javascript"> 
     function initialize() { 
      var options = { 
       types: ['(cities)'], 
       componentRestrictions: {country: "us"} 
      }; 
      var input = document.getElementById('searchTextField'); 
      var autocomplete = new google.maps.places.Autocomplete(input , options); 
     } 
     google.maps.event.addDomListener(window, 'load', initialize); 
    </script> 

</head> 
<body> 
    <div class = "row"> 
     <input id="searchTextField" type="text" size="50" placeholder="Enter a location" autocomplete="on"> 
    </div> 
</body> 

使用上面的代碼,我可以得到特定國家的城市列表。但我的要求是我需要單獨獲取國家名單。

如果選項未通過,則所有地點都會返回爲結果。

有沒有辦法單獨過濾國家?如果您有任何解決方案,請幫助我。

回答

3

在這一點上,你不能因爲沒有'國家'place type。雖然地區將自動完成國家(但不包括國家)。

var options = { 
     types: ['(regions)'] 
    }; 

Get only countries to autocomplete from Google Maps API提供了一些替代解決方案。以下是一個區域的例子:http://plnkr.co/edit/QjPEczt6AosghVP8EHNV

+0

非常感謝您的快速回復。我想我需要嘗試一下你提到的替代解決方案。非常感謝。 – Mahahari

+0

任何人都可以提供任何代碼片段或使用其他解決方案檢索國家列表的任何想法?請幫助 – Mahahari

相關問題