这里是第一次海报。在搜索字段上执行国家/地区限制时遇到一些问题。我的代码当前是:
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
//center: new google.maps.LatLng(-33.8688, 151.2195),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
// Autocomplete.
function setupClickListener(id, types) {
var radioButton = document.getElementById(id);
google.maps.event.addDomListener(radioButton, 'click', function() {
autocomplete.setTypes(types);
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
我需要做什么才能使其受到国家/地区的限制?我尝试添加:
componentRestrictions: {country: 'us'}
这会完全停止自动完成工作,没有明显的语法错误。
如果我看起来很懒,请原谅我,但我对编程还比较陌生,所以需要我能得到的所有帮助!我正在尝试修改此处 API 的现有实例,因此没有对其进行编码,这无疑增加了我的困惑。
谢谢。
最佳答案
使用等号代替冒号:
var componentRestrictions = {country: 'us'};
autocomplete.setComponentRestrictions(componentRestrictions);
关于google-maps-api-3 - Google Places 自动完成 API 国家/地区限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14649540/