简体   繁体   English

从 react-google-places-autocomplete 获取所选项目

[英]Get selected item from react-google-places-autocomplete

I'm trying to get the selected address from react-google-places-autocomplete in React Js but I don't know how to get the value.我正在尝试从 React Js 中的 react-google-places-autocomplete 获取所选地址,但我不知道如何获取该值。 Sorry to bother you and thank you!很抱歉打扰您,谢谢!

In your function, you should have a useState hook for the value you'd like to get.在你的 function 中,你应该有一个useState钩子来获取你想要的值。 Something like this:像这样的东西:

    const [location, setLocation] = useState(null);

Then, in your JSX you'll have something similar to the following:然后,在您的 JSX 中,您将拥有类似于以下内容的内容:

                    <GooglePlacesAutocomplete
                        selectProps={{
                            location,
                            onChange: setLocation,
                          }}                        
                        query={{key: YOUR_API_KEY}}
                        fetchDetails={true}
                        onFail={error => console.log(error)}
                      />

Now, each time the user clicks on the auto suggested address by Google Places, your location variable will store a map of the information returned.现在,每次用户点击 Google Places 自动推荐的地址时,您的location变量都会存储返回信息的 map。 To get the value of the address, you'll need to just grab it using the "label" key.要获取地址的值,您只需使用“标签”键获取它。

location["label"]位置[“标签”]

IOW, this will return the selected address. IOW,这将返回选定的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM