Autocomplete 反应本机-谷歌自动完成API

Autocomplete 反应本机-谷歌自动完成API,autocomplete,google-places-api,react-native-android,Autocomplete,Google Places Api,React Native Android,我的问题是描述的文本结果相互重叠。我不知道这是否是一个错误,我只是无法让它解决它。我尝试了各种风格,但都不管用。我需要来自不同角度的帮助。感谢您花时间阅读此文章 重叠文本的图像: 我还收到一条关于unique键的警告,只是添加了这个问题,因为它可能与我的问题有关 警告键的图像道具: 这是我的密码: async onChangeDestination(destination) { this.setState({ destination }); const apiUrl = `https://m

我的问题是描述的
文本
结果相互重叠。我不知道这是否是一个错误,我只是无法让它解决它。我尝试了各种风格,但都不管用。我需要来自不同角度的帮助。感谢您花时间阅读此文章

重叠文本的图像:

我还收到一条关于unique
键的警告,只是添加了这个问题,因为它可能与我的问题有关

警告键的图像
道具:

这是我的密码:

async onChangeDestination(destination) {
this.setState({ destination });
const apiUrl = `https://maps.googleapis.com/maps/api/place/autocomplete/json?key=${apiKey}
  &input=${destination}&location=${this.state.focusedLocation.latitude}, 
  ${this.state.focusedLocation.longitude
}&radius=2000`;
try {
  const result = await fetch(apiUrl);
  const json = await result.json();
  console.log(json);
  this.setState({
    predictions: json.predictions
  });
} catch (err) {
  console.log(err)
}
}

render() {
//For Prediction
const predictions = this.state.predictions.map(prediction => (
  <View style={styles.descriptionContainer}>
    <Text key={prediction.id} style={styles.descriptionText}>{prediction.description}</Text>
  </View>
));
//For Marker
let marker = null;
if(this.state.locationChosen) {
  marker = <MapView.Marker coordinate={this.state.markerPosition}/>
}
return(
  <View style={styles.container}>
    {/* <StatusBar backgroundColor={'transparent'} translucent={true}/> */}
    <MapView
      style={styles.map}
      initialRegion={this.state.focusedLocation}
      onPress={this.pickLocationHandler}
      showsUserLocation={true}
      ref={ref => this.map = ref} //For animating map movement
    >
      {marker}
    </MapView>
    {/* <TouchableOpacity onPress={this.getLocationHandler} style={styles.iconContainer}>
      <Icon name="md-locate" size={30} color="blue"/>
    </TouchableOpacity> */}
    <TextInput 
      placeholder="Search for an event or place!"
      style={styles.destinationInput}
      onChangeText={destination => {
        this.setState({destination});
        this.onChangeDestinationDebounced(destination)
      }}
      value={this.state.destination}
    />
    {predictions}
  </View>
);
} 

const styles = StyleSheet.create({
container: {
  zIndex: -1,
  alignItems: 'center',
},
map: {
  height: '100%',
  width: '100%'
},
iconContainer: {
  position: 'absolute',
  top: 60,
  right: 15,
  zIndex: 1
},
destinationInput: {
  position: 'absolute',
  zIndex: 10,
  width: '97%',
  top: 15,
  backgroundColor: 'white',
  borderRadius: 8,
  padding: 8
},
descriptionText: {
  color: 'black',
  position: 'absolute',
  zIndex: 10,
  top: 60,
},
descriptionContainer: {
  zIndex: 2,
  position: 'absolute',
  top: 20,
  height: Dimensions.get('window').height,
  width: Dimensions.get('window').width,
  justifyContent: 'space-between'
}
})
async onChangeDestination(目的地){
this.setState({destination});
常量apiUrl=`https://maps.googleapis.com/maps/api/place/autocomplete/json?key=${apiKey}
&input=${destination}&location=${this.state.focusedLocation.latitude},
${this.state.focusedLocation.longitude
}&半径=2000`;
试一试{
const result=等待获取(apirl);
const json=wait result.json();
log(json);
这是我的国家({
预测:json.predictions
});
}捕捉(错误){
console.log(错误)
}
}
render(){
//预测
const predictions=this.state.predictions.map(预测=>(
{prediction.description}
));
//标记
设marker=null;
如果(此.state.locationselected){
标记=
}
返回(
{/*  */}
this.map=ref}//用于设置贴图移动动画
>
{marker}
{/* 
*/}
{
this.setState({destination});
此.onChangeDestinationBounched(目标)
}}
值={this.state.destination}
/>
{预测}
);
} 
const styles=StyleSheet.create({
容器:{
zIndex:-1,
对齐项目:“居中”,
},
地图:{
高度:“100%”,
宽度:“100%”
},
iconContainer:{
位置:'绝对',
排名:60,
右:15,
zIndex:1
},
目的输入:{
位置:'绝对',
zIndex:10,
宽度:“97%”,
前15名,
背景颜色:“白色”,
边界半径:8,
填充:8
},
描述上下文:{
颜色:'黑色',
位置:'绝对',
zIndex:10,
排名:60,
},
描述容器:{
zIndex:2,
位置:'绝对',
前20名,
高度:尺寸。获取(“窗口”)。高度,
宽度:尺寸。获取('窗口')。宽度,
justifyContent:“间距”
}
})

因此我无法运行代码,但是您的
descriptionText
样式的位置为:“绝对”。因此,您看到的行为是预期的,因为每个文本都位于相同的“绝对”位置。删除此选项将获得所需的结果

当您的descriptionContainer视图是map函数中的根元素时,会引发key警告,因此它应该具有key。希望这会有所帮助,更新了下面的源代码,并声明我无法对其进行测试

async onChangeDestination(destination) {
this.setState({ destination });
const apiUrl = `https://maps.googleapis.com/maps/api/place/autocomplete/json?key=${apiKey}
  &input=${destination}&location=${this.state.focusedLocation.latitude}, 
  ${this.state.focusedLocation.longitude
}&radius=2000`;
try {
  const result = await fetch(apiUrl);
  const json = await result.json();
  console.log(json);
  this.setState({
    predictions: json.predictions
  });
} catch (err) {
  console.log(err)
}
}

render() {
//For Prediction
const predictions = this.state.predictions.map(prediction => (
  <Text key={prediction.id} style={styles.descriptionText}>{prediction.description}</Text>
));
//For Marker
let marker = null;
if(this.state.locationChosen) {
  marker = <MapView.Marker coordinate={this.state.markerPosition}/>
}
return(
  <View style={styles.container}>
    {/* <StatusBar backgroundColor={'transparent'} translucent={true}/> */}
    <MapView
      style={styles.map}
      initialRegion={this.state.focusedLocation}
      onPress={this.pickLocationHandler}
      showsUserLocation={true}
      ref={ref => this.map = ref} //For animating map movement
    >
      {marker}
    </MapView>
    {/* <TouchableOpacity onPress={this.getLocationHandler} style={styles.iconContainer}>
      <Icon name="md-locate" size={30} color="blue"/>
    </TouchableOpacity> */}
    <TextInput 
      placeholder="Search for an event or place!"
      style={styles.destinationInput}
      onChangeText={destination => {
        this.setState({destination});
        this.onChangeDestinationDebounced(destination)
      }}
      value={this.state.destination}
    />
    {this.state.predictions && this.state.predictions.length > 0 && (
       <View style={styles.descriptionContainer}>{predictions}</View>
    )}
  </View>
);
} 

const styles = StyleSheet.create({
container: {
  zIndex: -1,
  alignItems: 'center',
},
map: {
  height: '100%',
  width: '100%'
},
iconContainer: {
  position: 'absolute',
  top: 60,
  right: 15,
  zIndex: 1
},
destinationInput: {
  position: 'absolute',
  zIndex: 10,
  width: '97%',
  top: 15,
  backgroundColor: 'white',
  borderRadius: 8,
  padding: 8
},
descriptionText: {
  color: 'black',
},
descriptionContainer: {
  zIndex: 2,
  position: 'absolute',
  top: 20,
  height: Dimensions.get('window').height,
  width: Dimensions.get('window').width,
}
})
async onChangeDestination(目的地){
this.setState({destination});
常量apiUrl=`https://maps.googleapis.com/maps/api/place/autocomplete/json?key=${apiKey}
&input=${destination}&location=${this.state.focusedLocation.latitude},
${this.state.focusedLocation.longitude
}&半径=2000`;
试一试{
const result=等待获取(apirl);
const json=wait result.json();
log(json);
这是我的国家({
预测:json.predictions
});
}捕捉(错误){
console.log(错误)
}
}
render(){
//预测
const predictions=this.state.predictions.map(预测=>(
{prediction.description}
));
//标记
设marker=null;
如果(此.state.locationselected){
标记=
}
返回(
{/*  */}
this.map=ref}//用于设置贴图移动动画
>
{marker}
{/* 
*/}
{
this.setState({destination});
此.onChangeDestinationBounched(目标)
}}
值={this.state.destination}
/>
{this.state.predictions&&this.state.predictions.length>0&&(
{预测}
)}
);
} 
const styles=StyleSheet.create({
容器:{
zIndex:-1,
对齐项目:“居中”,
},
地图:{
高度:“100%”,
宽度:“100%”
},
iconContainer:{
位置:'绝对',
排名:60,
右:15,
zIndex:1
},
目的输入:{
位置:'绝对',
zIndex:10,
宽度:“97%”,
前15名,
背景颜色:“白色”,
边界半径:8,
填充:8
},
描述上下文:{
颜色:'黑色',
},
描述容器:{
zIndex:2,
位置:'绝对',
前20名,
高度:尺寸。获取(“窗口”)。高度,
宽度:尺寸。获取('窗口')。宽度,
}
})

因此我无法运行代码,但是您的
descriptionText
样式的位置为:“绝对”。因此,您看到的行为是预期的,因为每个文本都位于相同的“绝对”位置。删除此选项将获得所需的结果

当您的descriptionContainer视图是map函数中的根元素时,会引发key警告,因此它应该具有key。希望这会有所帮助,更新了下面的源代码,并声明我无法对其进行测试

async onChangeDestination(destination) {
this.setState({ destination });
const apiUrl = `https://maps.googleapis.com/maps/api/place/autocomplete/json?key=${apiKey}
  &input=${destination}&location=${this.state.focusedLocation.latitude}, 
  ${this.state.focusedLocation.longitude
}&radius=2000`;
try {
  const result = await fetch(apiUrl);
  const json = await result.json();
  console.log(json);
  this.setState({
    predictions: json.predictions
  });
} catch (err) {
  console.log(err)
}
}

render() {
//For Prediction
const predictions = this.state.predictions.map(prediction => (
  <Text key={prediction.id} style={styles.descriptionText}>{prediction.description}</Text>
));
//For Marker
let marker = null;
if(this.state.locationChosen) {
  marker = <MapView.Marker coordinate={this.state.markerPosition}/>
}
return(
  <View style={styles.container}>
    {/* <StatusBar backgroundColor={'transparent'} translucent={true}/> */}
    <MapView
      style={styles.map}
      initialRegion={this.state.focusedLocation}
      onPress={this.pickLocationHandler}
      showsUserLocation={true}
      ref={ref => this.map = ref} //For animating map movement
    >
      {marker}
    </MapView>
    {/* <TouchableOpacity onPress={this.getLocationHandler} style={styles.iconContainer}>
      <Icon name="md-locate" size={30} color="blue"/>
    </TouchableOpacity> */}
    <TextInput 
      placeholder="Search for an event or place!"
      style={styles.destinationInput}
      onChangeText={destination => {
        this.setState({destination});
        this.onChangeDestinationDebounced(destination)
      }}
      value={this.state.destination}
    />
    {this.state.predictions && this.state.predictions.length > 0 && (
       <View style={styles.descriptionContainer}>{predictions}</View>
    )}
  </View>
);
} 

const styles = StyleSheet.create({
container: {
  zIndex: -1,
  alignItems: 'center',
},
map: {
  height: '100%',
  width: '100%'
},
iconContainer: {
  position: 'absolute',
  top: 60,
  right: 15,
  zIndex: 1
},
destinationInput: {
  position: 'absolute',
  zIndex: 10,
  width: '97%',
  top: 15,
  backgroundColor: 'white',
  borderRadius: 8,
  padding: 8
},
descriptionText: {
  color: 'black',
},
descriptionContainer: {
  zIndex: 2,
  position: 'absolute',
  top: 20,
  height: Dimensions.get('window').height,
  width: Dimensions.get('window').width,
}
})
async onChangeDestination(目的地){
this.setState({destination});
常量apiUrl=`https://maps.googleapis.com/maps/api/place/autocomplete/json?key=${apiKey}
&input=${destination}&location=${this.state.focusedLocation.latitude},
${this.state.focusedLocation.longitude
}&半径=2000`;
试一试{
const result=等待获取(apirl);
const json=wait result.json();
log(json);
这是我的国家({
预测:json.predictions
});
}捕捉(错误){
console.log(错误)
}
}
render(){
//预测
const predictions=this.state.predictions.map(预测=>(
{prediction.description}
));
//标记
设marker=null;
如果(此.state.locationselected){
标记=
}
返回(
{/*  */}
this.map=ref}//用于设置贴图移动动画
>
{marker}
{/* 
*/}
{
this.setState({destination});
此.onChangeDestinationBounched(目标)
}}
值={this.state.destination}
/>
{this.state.predictions&&this.state.predictions.length>0&&