Google maps 按此按钮列表不会关闭
其次,要回答您的问题:结果在GooglePlacesAutocomplete的onPress功能中,您可以使用所选位置更新状态,然后在组件中的任何位置使用它:Google maps 按此按钮列表不会关闭,google-maps,react-native,autocomplete,react-native-maps,react-props,Google Maps,React Native,Autocomplete,React Native Maps,React Props,其次,要回答您的问题:结果在GooglePlacesAutocomplete的onPress功能中,您可以使用所选位置更新状态,然后在组件中的任何位置使用它: onPress={(data, details = null) => { this.setState({ latitude: details.geometry.location.lat, longitude: details.geometry.location.lng,
onPress={(data, details = null) => {
this.setState({
latitude: details.geometry.location.lat,
longitude: details.geometry.location.lng,
moveToUserLocation: true
});
this._gotoLocation();
}
}
正如我写的那样,onPress还触发了移动地图以显示新位置的函数。import React,{Component}from'React';
import React, {Component} from 'react';
import { View, Image, Text, StyleSheet } from 'react-native';
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';
export default class google extends Component {
constructor(props) {
super(props);
this.state = {
address:null,
lat:null,
lng:null,
}
}
getAdd(data){
console.log("add",data);
this.setState(
{
address: data.formatted_address, // selected address
lat: data.geometry.location.lat,// selected coordinates latitude
lng:data.geometry.location.lng, // selected coordinates longitute
}
);
console.log("this.state.address",this.state.address); ///to console address
console.log("this.state.coordinates",this.state.lat,this.state.lng); /// to console coordinates
}
render(){
return (
<View style={styles.container}>
<View style={styles.top}>
<GooglePlacesAutocomplete
placeholder='Search'
minLength={2} // minimum length of text to search
autoFocus={false}
fetchDetails={true}
returnKeyType={'default'}
onPress={(data, details = null) => { // 'details' is provided when fetchDetails = true
var data = details;
this.getAdd(data);
}}
query={{
// available options: https://developers.google.com/places/web-service/autocomplete
key: 'AIzaS#################',
language: 'en',
types: 'geocode', // default: 'geocode'
}}
listViewDisplayed={this.state.showPlacesList}
textInputProps={{
onFocus: () => this.setState({ showPlacesList: true }),
onBlur: () => this.setState({ showPlacesList: false }),
}}
styles={{
textInputContainer: {
width: '100%'
},
description: {
fontWeight: 'bold'
},
predefinedPlacesDescription: {
color: '#1faadb'
}
}}
currentLocation={true} // Will add a 'Current location' button at the top of the predefined places list
currentLocationLabel="Current location"
nearbyPlacesAPI='GooglePlacesSearch' // Which API to use: GoogleReverseGeocoding or GooglePlacesSearch
filterReverseGeocodingByTypes={['locality', 'administrative_area_level_3']} // filter the reverse geocoding results by types - ['locality', 'administrative_area_level_3'] if you want to display only cities
// predefinedPlaces={[]}
predefinedPlacesAlwaysVisible={true}
/>
</View>
{ this.state.address !=null ?(
<View style={styles.container2}>
<Text>
Address: {this.state.address}
</Text>
</View>
):null }
</View>
);
}
}
const styles = StyleSheet.create({
container: {
width: '100%',
height: '100%',
},
welcome: {
fontSize: 40,
textAlign: 'center',
margin: 10,
color:'black',
},
instructions: {
textAlign: 'center',
color: 'black',
marginBottom: 5,
},
top: {
height: '50%',
justifyContent: 'center',
alignItems: 'center',
},
container2: {
height:'75%',
justifyContent: 'center',
},
buttonContainer: {
alignItems: 'center',
backgroundColor: 'rgba(255, 255,255, 0.5)',
padding: 1,
margin: 50,
borderRadius: 25,
borderWidth: 2,
borderColor: '#0B0B3B'
},
textoboton: {
textAlign: 'center',
color: 'black',
marginBottom: 5,
fontSize: 12
},
})
从“react native”导入{视图、图像、文本、样式表};
从“react native google places autocomplete”导入{GooglePlacesSautoComplete};
导出默认类GoogleExtends组件{
建造师(道具){
超级(道具);
此.state={
地址:空,
lat:null,
lng:null,
}
}
getAdd(数据){
控制台日志(“添加”,数据);
这是我的国家(
{
地址:data.formatted\u地址,//所选地址
lat:data.geometry.location.lat,//所选坐标纬度
lng:data.geometry.location.lng,//所选坐标长度
}
);
console.log(“this.state.address”,this.state.address);///到控制台地址
console.log(“this.state.coordinates”,this.state.lat,this.state.lng);///到控制台坐标
}
render(){
返回(
{//'details'在fetchDetails=true时提供
var数据=详细信息;
这个.getAdd(数据);
}}
质疑={{
//可用选项:https://developers.google.com/places/web-service/autocomplete
关键词:“艾扎斯”,
语言:"en",,
类型:'geocode',//默认值:'geocode'
}}
listViewDisplayed={this.state.showPlacesList}
文本输入道具={{
onFocus:()=>this.setState({showPlacesList:true}),
onBlur:()=>this.setState({showPlacesList:false}),
}}
风格={{
textInputContainer:{
宽度:“100%”
},
说明:{
fontWeight:“粗体”
},
预定义位置说明:{
颜色:“#1faadb”
}
}}
currentLocation={true}//将在预定义位置列表的顶部添加一个“Current location”按钮
currentLocationLabel=“当前位置”
nearbyPlacesAPI='GooglePlacesSearch'//使用哪种API:GoogleReverseGeoCode或GooglePlacesSearch
filterReverseGeocodingByTypes={['Location','administrative_area_level_3']}//如果只想显示城市,请按类型['Location','administrative_area_level_3']过滤反向地理编码结果
//预定义位置={[]}
预定义的PlaceSalwaysVisible={true}
/>
{this.state.address!=null(
地址:{this.state.Address}
):null}
);
}
}
const styles=StyleSheet.create({
容器:{
宽度:“100%”,
高度:“100%”,
},
欢迎:{
尺寸:40,
textAlign:'中心',
差额:10,
颜色:'黑色',
},
说明:{
textAlign:'中心',
颜色:'黑色',
marginBottom:5,
},
顶部:{
身高:50%,
为内容辩护:“中心”,
对齐项目:“居中”,
},
集装箱2:{
身高:'75%',
为内容辩护:“中心”,
},
按钮容器:{
对齐项目:“居中”,
背景颜色:“rgba(255,255255,0.5)”,
填充:1,
差额:50,
边界半径:25,
边界宽度:2,
边框颜色:“#0B0B3B”
},
textoboton:{
textAlign:'中心',
颜色:'黑色',
marginBottom:5,
字体大小:12
},
})
经过长途旅行,这些步骤帮助我解决了问题
1) Installnpm Install react native google places autocomplete--save
2) 然后将下面的代码用作组件中的元素
<GooglePlacesAutocomplete
query={{
key: "GOOGLE_PLACES_API_KEY",
language: "en", // language of the results
}}
onPress={(data, details = null) => {
console.log(details);
console.log(data);
console.log("data.description",data.description.split(','));
}}
onFail={(error) => console.error(error)}
listViewDisplayed="false"
requestUrl={{
url:
"https://cors-
anywhere.herokuapp.com/https://maps.googleapis.com/maps/api",
useOnPlatform: "web",
}} // this in only required for use on the web. See https://git.io/JflFv
more for details.
styles={{
textInputContainer: {
backgroundColor: "rgba(0,0,0,0)",
borderTopWidth: 0,
borderBottomWidth: 0,
},
textInput: {
marginLeft: 0,
marginRight: 0,
height: 38,
color: "#5d5d5d",
fontSize: 16,
},
predefinedPlacesDescription: {
color: "#1faadb",
},
}}
/>
4) 要保存地址,您可以检查代码中的控制台.log
,然后使用setState或类似的方法
5) 有关这些元素的更多信息和选项,请查看此存储库:
希望这对您有所帮助:)通常最好解释一个解决方案,而不是仅仅发布几行匿名代码。你可以阅读,也可以阅读
import React, {Component} from 'react';
import { View, Image, Text, StyleSheet } from 'react-native';
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';
export default class google extends Component {
constructor(props) {
super(props);
this.state = {
address:null,
lat:null,
lng:null,
}
}
getAdd(data){
console.log("add",data);
this.setState(
{
address: data.formatted_address, // selected address
lat: data.geometry.location.lat,// selected coordinates latitude
lng:data.geometry.location.lng, // selected coordinates longitute
}
);
console.log("this.state.address",this.state.address); ///to console address
console.log("this.state.coordinates",this.state.lat,this.state.lng); /// to console coordinates
}
render(){
return (
<View style={styles.container}>
<View style={styles.top}>
<GooglePlacesAutocomplete
placeholder='Search'
minLength={2} // minimum length of text to search
autoFocus={false}
fetchDetails={true}
returnKeyType={'default'}
onPress={(data, details = null) => { // 'details' is provided when fetchDetails = true
var data = details;
this.getAdd(data);
}}
query={{
// available options: https://developers.google.com/places/web-service/autocomplete
key: 'AIzaS#################',
language: 'en',
types: 'geocode', // default: 'geocode'
}}
listViewDisplayed={this.state.showPlacesList}
textInputProps={{
onFocus: () => this.setState({ showPlacesList: true }),
onBlur: () => this.setState({ showPlacesList: false }),
}}
styles={{
textInputContainer: {
width: '100%'
},
description: {
fontWeight: 'bold'
},
predefinedPlacesDescription: {
color: '#1faadb'
}
}}
currentLocation={true} // Will add a 'Current location' button at the top of the predefined places list
currentLocationLabel="Current location"
nearbyPlacesAPI='GooglePlacesSearch' // Which API to use: GoogleReverseGeocoding or GooglePlacesSearch
filterReverseGeocodingByTypes={['locality', 'administrative_area_level_3']} // filter the reverse geocoding results by types - ['locality', 'administrative_area_level_3'] if you want to display only cities
// predefinedPlaces={[]}
predefinedPlacesAlwaysVisible={true}
/>
</View>
{ this.state.address !=null ?(
<View style={styles.container2}>
<Text>
Address: {this.state.address}
</Text>
</View>
):null }
</View>
);
}
}
const styles = StyleSheet.create({
container: {
width: '100%',
height: '100%',
},
welcome: {
fontSize: 40,
textAlign: 'center',
margin: 10,
color:'black',
},
instructions: {
textAlign: 'center',
color: 'black',
marginBottom: 5,
},
top: {
height: '50%',
justifyContent: 'center',
alignItems: 'center',
},
container2: {
height:'75%',
justifyContent: 'center',
},
buttonContainer: {
alignItems: 'center',
backgroundColor: 'rgba(255, 255,255, 0.5)',
padding: 1,
margin: 50,
borderRadius: 25,
borderWidth: 2,
borderColor: '#0B0B3B'
},
textoboton: {
textAlign: 'center',
color: 'black',
marginBottom: 5,
fontSize: 12
},
})
<GooglePlacesAutocomplete
query={{
key: "GOOGLE_PLACES_API_KEY",
language: "en", // language of the results
}}
onPress={(data, details = null) => {
console.log(details);
console.log(data);
console.log("data.description",data.description.split(','));
}}
onFail={(error) => console.error(error)}
listViewDisplayed="false"
requestUrl={{
url:
"https://cors-
anywhere.herokuapp.com/https://maps.googleapis.com/maps/api",
useOnPlatform: "web",
}} // this in only required for use on the web. See https://git.io/JflFv
more for details.
styles={{
textInputContainer: {
backgroundColor: "rgba(0,0,0,0)",
borderTopWidth: 0,
borderBottomWidth: 0,
},
textInput: {
marginLeft: 0,
marginRight: 0,
height: 38,
color: "#5d5d5d",
fontSize: 16,
},
predefinedPlacesDescription: {
color: "#1faadb",
},
}}
/>
...
onFocus={onFocus ? () => {this._onFocus(); onFocus()} : this._onFocus}
// onBlur={this._onBlur}
underlineColorAndroid={this.props.underlineColorAndroid}
...