未从 google place API 获取formatted_address

not getting formatted_address from google place API

提问人:samarjit singh 提问时间:11/4/2023 最后编辑:Chrissamarjit singh 更新时间:11/4/2023 访问量:11

问:

useEffect(() => {
    autoCompleteRef.current = new window.google.maps.places.Autocomplete(inputRef.current, options);
    autoCompleteRef.current.addListener('place_changed', async function () {
      const place = await autoCompleteRef.current.getPlace();
      console.log('Selected Place: ', place);

      const address = place.address_components;
      const extractedAddress = {
        streetName: address.find((comp) => comp.types.includes('route'))?.long_name || '',
        city: address.find((comp) => comp.types.includes('locality'))?.long_name || '',
        province:
          address.find((comp) => comp.types.includes('administrative_area_level_1'))?.long_name ||
          '',
        country: address.find((comp) => comp.types.includes('country'))?.long_name || '',
        countryCode: address.find((comp) => comp.types.includes('country'))?.short_name || '',
        postalCode: address.find((comp) => comp.types.includes('postal_code'))?.long_name || '',
        fullAddress: place.formatted_address || '',
      };

      setExtractedAddress(extractedAddress);
    });
  }, []);

在上面的代码中,为什么我是fullAddress作为空字符串

尝试了所有可用的方法

reactjs react-hooks 自动完成 google-places-api

评论

0赞 Yrll 11/7/2023
欢迎来到 Stack Overflow!您能否提供一个最小的可重复示例?这会重现您的问题,以便社区可以在其中进行操作并检查可能导致您的问题的原因。

答: 暂无答案