使用 GooglePlacesAutocomplete 和 react-native 限制在多个国家/地区

问题描述

我看过一些帖子,解释了如何在 javascript/react 中为 google 地方自动完成设置多个国家/地区,如下所示: componentRestrictions: {country: ["gb","fr"]}

但是对于本机的 GooglePlacesAutocomplete,建议是在查询中放置一个组件选项。不确定在这里我们是否可以传递多个国家...已经尝试过但未能在此处传递数组,因为它看起来只需要一个包含一个国家/地区代码的字符串。将不胜感激任何帮助...

 <GooglePlacesAutocomplete
        placeholder={placeholder}
        listViewdisplayed={true}
        fetchDetails={true}
        textInputProps={{
          borderColor: color,fontSize: 18,borderWidth: 1,borderRadius: 4,flex: 1,padding: 7
        }}
        onPress={(data,details = null) => {
          console.log(data,details)
        }}
        query={{
          key: googleApiKey,language: "en",components: "country:gb",types: types ? types : null
        }}
      />

解决方法

您可以像这样使用组件限制components: "country:gb|country:us"

查看下面的完整代码示例

<GooglePlacesAutocomplete
            placeholder={placeholder}
            listViewDisplayed={true}
            fetchDetails={true}
            textInputProps={{
              borderColor: color,fontSize: 18,borderWidth: 1,borderRadius: 4,flex: 1,padding: 7
            }}
            onPress={(data,details = null) => {
              console.log(data,details)
            }}
            query={{
              key: googleApiKey,language: "en",components: "country:gb|country:us",types: types ? types : null
            }}
          />