Multiple Google Autocomplete and Places Search Issue

时间秒杀一切 提交于 2020-01-14 03:32:11

问题


I am in need of some help regarding Google Maps API and using Autocomplete and Address complete.

This is currently my code:

JS

function fillInAddress(show) {
                    console.log(show);
                    var place = autocomplete.getPlace();
                    for (var i = 0; i < place.address_components.length; i++){
                        var base = place.address_components[i].types[0];
                        if(base === "postal_code"){
                            console.log(place.address_components[i].long_name);
                        }
                    }
                }

function initialize(out) {
            autocomplete = new google.maps.places.Autocomplete(
                    (document.getElementById(out)),
                    {types: ['geocode']});
            google.maps.event.addListener(autocomplete, 'place_changed', function () {
                fillInAddress(out);
            });
        }
        initialize("from");
        initialize("to");

HTML

<input type="text" id="from" class="location">
<input type="text" id="to" class="location">
<input id="fromPc" type="text">
<input id="toPc" type="text">

The places dropdown is working perfectly and allows you to type and then will come up with the suggestions fine.

However when I click on one of the suggestions in the from input I get this error in the console.

Uncaught TypeError: Cannot read property 'address_components' of undefined

But when I do it in the To input box it works fine and returns the postcode.

If I then go back to the first input "from" and then enter the address again it returns a postcode this time but it returns the postcode from the second input "to".

Does anyone know how to resolve this.

I appreciate your help in advance!


回答1:


You are overwriting the (global) variable autocomplete inside initialize.

Make the variable local inside initialize and pass it as argument to fillInAddress

function fillInAddress(show,ac) {

                    var place = ac.getPlace();
                    for (var i = 0; i < place.address_components.length; i++){
                        var base = place.address_components[i].types[0];
                        if(base === "postal_code"){
                            document.getElementById(show+'Pc').value
                              = place.address_components[i].long_name;
                              return;
                        }
                    }
                }

function initialize(out) {
           var  autocomplete = new google.maps.places.Autocomplete(
                    (document.getElementById(out)),
                    {types: ['geocode']});

            google.maps.event.addListener(autocomplete, 'place_changed', 
               function () {
                document.getElementById(out+'Pc').value='';
                fillInAddress(out,autocomplete);
            });
        }
initialize("from");
initialize("to");


来源:https://stackoverflow.com/questions/28023254/multiple-google-autocomplete-and-places-search-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!