Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌自动填充API_Javascript_Html_Google Api - Fatal编程技术网

Javascript 谷歌自动填充API

Javascript 谷歌自动填充API,javascript,html,google-api,Javascript,Html,Google Api,我按照这个地址自动填充城市 但是当我试图实现它时,我没有得到我想要的功能。我希望在填写地址后,城市字段应自动填写。下面是我的代码: <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" nam

我按照这个地址自动填充城市

但是当我试图实现它时,我没有得到我想要的功能。我希望在填写地址后,城市字段应自动填写。下面是我的代码:

<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>

var autocomplete=new google.maps.places.autocomplete($(“#破解”)[0],{};
google.maps.event.addListener(自动完成,'place\u changed',函数(){
var place=autocomplete.getPlace();
console.log(位置、地址和组件);
});
var autocomplete=new google.maps.places.autocomplete($(“#add”)[0],{});
google.maps.event.addListener(自动完成,'place\u changed',函数(){
var place=autocomplete.getPlace();
console.log(位置、地址和组件);
});
var placeSearch,自动完成;
变量组件形式={
破解:“长名字”,
};
函数fillInAddress(){
//从自动完成对象获取位置详细信息。
var place=autocomplete.getPlace();
for(componentForm中的var组件){
document.getElementById(组件).value='';
document.getElementById(组件).disabled=false;
}
//从place details中获取地址的每个组件
//并在表单上填写相应的字段。
对于(变量i=0;i
未填充的原因是由于您使用的
输入标记
id
s:
id=“crack”
id=“ar”
,&
id=“add”
。谷歌分别使用
id=“locality”
id=“administrative\u area\u level\u 1”
,&
id=“route”

更不用说它们会返回多个信息字段,因此如果没有
input
标记来接收它们,您也会收到错误

因此,通过对
fillInAddress()
函数中的
input
tag
id
s进行映射,以及对任何其他返回的字段进行捕获,我们能够按照您的期望完成字段的填充:

<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>

城市
地区
地址
var placeSearch,自动完成;
变量组件形式={
街道编号:“短名称”,
路线:'long_name',
地点:'long_name',
行政区域级别1:“短名称”,
国家:'long_name',
邮政编码:“短名称”
};
函数初始化(){
autocomplete=new google.maps.places.autocomplete(
(document.getElementById('add')),
{
类型:['geocode']
}
);
google.maps.event.addListener(自动完成,'place\u changed',函数(){
fillInAddress();
});
}
函数fillInAddress(){
var place=autocomplete.getPlace();
对于(变量i=0;i

我的回答对你@Legendary\u Hunter有帮助吗?