如果选择了地址,我只想在特定国家/地区获取地址。我的代码是:
var country="";
$("select[name='country_id']").change(function () {
co=$("select[name='country_id']").val();
});
function initialize() {
var options={};
if(country){
options = {
types: ['geocode'],
componentRestrictions: {country: country}
};
}else{
options = {
types: ['geocode']
};
}
var input = document.getElementById('address');
var autocomplete = new google.maps.places.Autocomplete(input,options);
google.maps.event.addListener(autocomplete, 'place_changed', function () {
//...
});
}
google.maps.event.addDomListener(window, 'load', initialize);
但我的国家/地区值始终为空。如何处理国家/地区以获取其值?请帮帮我。谢谢!
每次都必须
使用新的国家/地区onchange
初始化自动完成
小提琴演示
.JS
var country = "";
$("select[name='country_id']").change(function() {
country = $("select[name='country_id']").val();
$('#searchTextField').attr('placeholder','search form '+$("select[name='country_id'] option:selected").text())
$('#input').show();
initialize()
});
function initialize() {
var input = document.getElementById('searchTextField');
var options = {
componentRestrictions: {
country: country
}
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
var lat = place.geometry.location.lat();
var long = place.geometry.location.lng();
alert(lat + ", " + long);
});
}