我关注了这个tutorial自动根据地址填写城市。
但是当我尝试实现它时,我并没有获得我想要的功能。我希望在填写地址后自动填写城市字段。下面是我的代码:
<div class="form-group">
<label class="col-md-3 control-label" for="example-text-input">City</label>
<div class="col-md-3">
<input type="text" id="crack" name="City" class="form-control" >
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="example-text-input">Area</label>
<div class="col-md-3">
<input type="text" id="ar" name="Area" class="form-control" >
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="example-text- input">Address</label>
<div class="col-md-3">
<input type="text" id="add" name="Address" class="form-control" >
</div>
</div>
下面是javascript代码
<script>
var autocomplete = new google.maps.places.Autocomplete($("#crack")[0], {});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
console.log(place.address_components);
});
</script>
<script>
var autocomplete = new google.maps.places.Autocomplete($("#add")[0], {});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
console.log(place.address_components);
});
var placeSearch, autocomplete;
var componentForm = {
crack: 'long_name',
};
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
// Get each component of the address from the place details
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
}
</script>
<?php include 'inc/template_end.php'; ?>
<script type="text/javascript">
function hi()
{
var ele=(document.getElementById("sla").value);
if(ele==1)
{
document.getElementById("g1").style.display="none";
}
else
{
document.getElementById("g1").style.display = "initial";
}
}
</script>
最佳答案
它没有填充的原因是由于您使用的 input
标签 id
:id="crack"
, id="ar"
, & id="add"
. Google 分别使用 id="locality"
、id="administrative_area_level_1"
和 id="route"
。
更不用说它们会返回更多的信息字段,因此如果没有 input
标签来接收它们,您也会收到错误信息。
因此,在 fillInAddress()
函数中对您的 input
标记 id
进行一些映射,并为任何其他返回的字段,我们能够像您希望的那样完成字段的填充:
<div class="form-group">
<label class="col-md-3 control-label" for="example-text-input">City</label>
<div class="col-md-3">
<input type="text" id="crack" name="City" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="example-text-input">Area</label>
<div class="col-md-3">
<input type="text" id="ar" name="Area" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="example-text- input">Address</label>
<div class="col-md-3">
<input type="text" id="add" name="Address" onFocus="geolocate()" class="form-control">
</div>
</div>
<script>
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
function initialize(){
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('add')),
{
types: ['geocode']
}
);
google.maps.event.addListener(autocomplete, 'place_changed', function(){
fillInAddress();
});
}
function fillInAddress(){
var place = autocomplete.getPlace();
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
input = 1;
if (addressType === 'street_number') addressType = 'add';
else if (addressType === 'route'){
addressType = 'add';
val = document.getElementById(addressType).value + ' ' + val;
} else if (addressType === 'locality') addressType = 'crack';
else if (addressType === 'administrative_area_level_1') addressType = 'ar';
else input = 0;
if (input) document.getElementById(addressType).value = val;
}
}
}
function geolocate(){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
var geolocation = new google.maps.LatLng(
position.coords.latitude,
position.coords.longitude
);
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
initialize();
</script>
关于javascript - 谷歌自动填充 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31555643/