有没有办法预先填写谷歌自动完成的地方?

时间:2018-05-28 09:52:22

标签: javascript google-maps

从浏览器导航中,我得到的地址为:

"sender": {
        "name": "Mary Jane",
        "address": "Jalan Tekronat",
        "city": "LAKSI",
        "state": "Bangkok",
        "country": "Thailand",
        "email": "",
        "mobile": "1234567890",
        "postalcode":"10010",
        "formatedAddress":"G218 Rama I Road, Pathum Wan, Bangkok, Thailand",
        "countryShortName":"TH",
        "countrySearchShortName":"TH"
    },

由于我有地址,客户要求预先填写自动填充组件中的详细信息。怎么做?我找不到办法做到这一点。

这是我的组件:

this.sender = new google.maps.places.Autocomplete(this.searchSenderAddressRef.nativeElement, {
                types:["address"]
            });


            google.maps.event.addListener(this.senderAddressAutoComplete, 'place_changed', function(value){
                var place = that.senderAddressAutoComplete.getPlace();
                var address = '';
                if (place.address_components) {
                    address = [
                      (place.address_components[0] && place.address_components[0].short_name || ''),
                      (place.address_components[1] && place.address_components[1].short_name || ''),
                      (place.address_components[2] && place.address_components[2].short_name || '')
                    ].join(' ');

                    let getPostalCode = place.address_components.find( obj => obj.types.includes("postal_code") );
                    let getCountry = place.address_components.find( obj => obj.types.includes("country") );
                    let getCity = place.address_components.find( obj => obj.types.includes("locality") );
                    let getState = place.address_components.find( obj => obj.types.includes("administrative_area_level_2") );

                    that.sharableData.bd.shipment.sender.postalcode = getPostalCode && (getPostalCode.long_name || "");
                    that.sharableData.bd.shipment.sender.address = address || "";
                    that.sharableData.bd.shipment.sender.country = getCountry.long_name || "";
                    that.sharableData.bd.shipment.sender.state = getState && (getState.long_name || "");
                    that.sharableData.bd.shipment.sender.city = getCity && (getCity.long_name || "");
                    that.sharableData.bd.shipment.sender.formatedAddress = place.formatted_address || "";
                    that.sharableData.bd.shipment.sender.countryShortName = getCountry && (getCountry.short_name || "");

                    that.senderAddress.setValue(address);
                    that.sharedData.updateSharedQuoteDatas(that.sharableData);

                }
            })

任何人帮助我?

1 个答案:

答案 0 :(得分:-4)

首先,您需要具体了解您正在使用的前端技术(React,Angular等)。

唉!

好吧,预填充基于输入的组件(例如Autocomplete)背后的整个想法是,您必须initialise the component包含数据。

了解

  1. 数据绑定的工作原理?
  2. 了解如何初始化具有绑定到它的特定值的输入组件。