You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

react-native-google-places-autocomplete搜索无结果问题求助

React Native Google Places 自动补全无结果问题

我开发了一个React Native位置搜索组件,目标是实现用户输入城市或地址时的自动补全功能,基于Google Places API实现。已集成react-native-google-places-autocomplete npm包,并在Google Cloud Console创建了API密钥完成所有配置,但搜索未返回任何结果。

组件代码

import React, { useContext, useEffect, useState } from 'react'
import { TextInput, View, Text, TouchableOpacity, StyleSheet, Dimensions } from 'react-native'
import axios from 'axios'
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete'

import {Feather} from 'react-native-vector-icons'

import { SearchFilterContext } from '../../Context/SearchFilterContext'
import { PropertiesContext } from '../../Context/PropertiesContext'

const deviceWidth = Dimensions.get('window').width
const aspectWidth = deviceWidth - 16

const SearchComponent = () => {
  const [search, setSearch] = useState('')
  const [autocompelete, setAutoComplete] = useState([])

  const {currentSearch, setCurrentSearch, results} = useContext(PropertiesContext)
  const {setResults, getProperties, setLoading} = useContext(PropertiesContext)

  const SubmitSearch = () => {
    setLoading(true)
    setCurrentSearch(search)
    setResults([])
    resetSearch()
  }

  const updateSearch = (value) => {
    setCurrentSearch(value)
  }

  const handlePlaceSelect = (data, details) => {
    console.log(data, details);
  };

  const resetSearch = () => {
    getProperties()
  }

  return (
    <View style={styles.SearchBar}>
        <GooglePlacesAutocomplete
          placeholder="Search"
          styles={{
            textInputContainer: {
              marginBottom: 8,
            },
            textInput: {
              height: 32,
              color: 'black',
              fontSize: 16,
              backgroundColor: 'lightgrey'
            },
            predefinedPlacesDescription: {
              color: 'black',
            },
          }}
          onPress={(data, details = null) => {
            console.log(data, details)}}
          fetchDetails={true}
          query={{
            key: '...',
            language: 'en',
          }}
        />
    </View>
  )
}

const styles = StyleSheet.create({
  SearchBar: {
    width: aspectWidth,
    height: 36,
    paddingBottom: 8
  },
})

export default SearchComponent

API密钥配置截图

  • API启用状态:API已启用
  • API密钥激活状态:API密钥已激活

搜索界面截图

搜索界面

内容的提问来源于stack exchange,提问作者ojandali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 08:53:17