Can the Google Autocomplete place be preloaded with information?

When I retrieve an address as an object from the browser navigation, it looks like this:

"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"
    },

The customer request is to prefill these details in the autocomplete component. I am unsure how to achieve this.

Below is my component code snippet:

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

                }
            })

Can anyone assist me with this?

Answer №1

To begin with, it is important to specify the frontend technology being utilized, whether it be React, Angular, or any other.

Unfortunately!

The main concept behind pre-filling an input-based component such as Autocomplete is that you must initialize the component with the necessary data.

Take some time to explore:

  1. How data binding functions?
  2. Understanding how to set up an input component with a specific value bound to it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing controllers to append objects to an array in the model

Hi there, I'm currently working on a project where I need to add an object to an array using Ember. The friends controller model contains an array of objects (App.friends). In the friends/new template, I collect a user's first name, last name, a ...

Error: The gaugeData2 property is not defined and cannot be read at line 1465 in the evaluation of dashboard.v1.vue in Vue.js

Utilizing PHP with a MySQL database to present data on highcharts in vue.js, I have defined an array in the data section of the page like so to use for displaying the chart data: data() { return { gaugeData2: [], } To retrieve the data from the database ...

AngularJS does not clear the array after a POST request is made

ISSUE Greetings! I am encountering an odd behavior with my backend. When I submit data, everything works flawlessly. However, if I press ENTER and submit an empty field, it reposts the previous value. Initially, I cannot submit an empty field, but after e ...

Navigate to a different HTML page using React material-ui and display an initial component within it

Currently, I am in the process of creating a website using material-ui (https://material-ui.com/). Within this design, there is a toolbar containing buttons. My objective is to be able to navigate to another HTML page when one of these buttons is clicked a ...

Modify the size of the fabricjs text box to perfectly match the chosen text

I'm currently facing an issue with my code snippet that is supposed to create a new Textbox: new fabric.Textbox('Add some text') The problem I'm encountering is that the text box appears too small compared to its content: https://i.s ...

Required environment variables are absent in the application form

Currently, I am working on developing a Next.js application and implementing CRUD functionality. However, I encountered an error while creating the "Create" page, which seems to be related to environment detection. Just to provide some context, I am using ...

Koa fails to extract the 'body' from a fetch POST request

Recently, I've encountered an issue with my JavaScript code that sends POST data to my server. Even though the body data appears to be fine when printed out, Koa is not parsing the 'body' from the request using koa-bodyparser. This problem h ...

Creating a dynamic trio of graphs with HTML5, CSS3, and Vanilla JavaScript

I successfully created a tree graph using HTML5 and CSS3, but currently the nodes are static. I am looking to enhance the graph by making it dynamic. By dynamic, I mean that if the number of nodes increases or there are multiple children added, the graph ...

There seems to be an issue with the Link component from react-router-dom when used in conjunction with

I am currently utilizing react-router-dom along with Material-ui My main objective is to create a clickable row in a table that will lead to a specific path. Here is the code snippet: .map(client => ( <TableRow key={client.id} component={Link} to ...

What is the method for fetching "related" documents from MongoDB?

Currently, I am utilizing node.js, express, and the MongoSkin driver for MongoDB in my blog application. In order to display a list of blog posts with their authors, I am facing a challenge. Each post contains a reference to an author ID, and I am struggl ...

JavaScript first, middle, and last names

When working with Javascript, I have encountered a challenge. I am attempting to extract the First, Middle, and Last names from a full name input into three separate fields - Character Length, Middle Name, and Initials. At this point, I have successfull ...

Updating a Mongoose model with a dynamically generated field name

I am attempting to pass a field name as a variable, but here is the code I tried and it isn't working: var update = {}; update[req.body.field] = req.body.value; Model.update( {"email": req.user.email}, {$set: {update}}, function (err, suc ...

Leveraging ng-class with an Angular $scope attribute

My HTML structure includes: <div class="myDiv"> <div style="width:200px; height:200px;background-image:url('img/200x200/{{largeImg}}.png');" ng-class="{'magictime foolishIn': 1}"> <span> { ...

Switching the positions of the date and month in VueJS Datepicker

Recently, I have been utilizing the datepicker component from vuejs-datepicker. However, I encountered an issue where upon form submission, the date and month switch places. For instance, 10/08/2018 (dd/MM/yyyy) eventually displays as 08/10/2018, leading ...

The anchor links fail to navigate to specific page sections when refreshing the browser or navigating back and forth

I've created a webpage using HTML that contains links within the page. <div> <a href="#first">first</a> <a href="#second">second</a> <div id="first">First div</div> <div id="second">Second div</div&g ...

What steps should I take to ensure the text layout algorithm effectively scales the text based on the "font size" option when using SVG paths?

I have included a fully functional JavaScript code snippet at the end of this post, demonstrating how Hebrew text can be displayed in a "stretched" layout design, as referenced here. The output shows the text in various font sizes: 32 The text appears mos ...

Ways to prevent images from vanishing when the mouse is swiftly glided over them

Within the code snippet, the icons are represented as images that tend to disappear when the mouse is swiftly moved over them. This issue arises due to the inclusion of a transition property that reduces the brightness of the image on hover. However, when ...

Exploring D3 to extract nested information and build an interactive navigation tree

Recently, I've been working with d3's zoom circle feature from here: The data I'm dealing with is structured as a json array with various levels of arrays and objects. object = { class: "show", name: "Game of Thrones", childre ...

Is the OpenLayers layer switcher unable to display image overlays?

Recently, I incorporated a large image onto my OpenLayers map using the code provided below. However, for some reason, the image is not showing up on the map. Oddly enough, when I expand the layerswitcher, the layer appears but is grayed out and disabled. ...

How to retrieve the error message in the front end that originates from the back end

Whenever a certain condition is met, I trigger an error from the backend. However, in various scenarios, multiple errors are sent, and I need to retrieve only the error message generated by the backend (using node.js and express). In my frontend code (buil ...