这是使用Google的API来响应本地地图的代码。
import React, { useEffect, useState } from 'react';
import { PermissionsAndroid, StyleSheet, View, Dimensions } from 'react-native';
import MapView, { PROVIDER_GOOGLE, Marker } from 'react-native-maps';
import Geolocation from 'react-native-geolocation-service';
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';
const GOOGLE_API_KEY = *GOOGLE API*
const screenWidth = Dimensions.get('window').width
function App() {
const [location, setLocation] = useState({ latitude: 0, longitude: 0 })
async function getPermission() {
await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
)
}
useEffect(() => {
getPermission().then(() => {
Geolocation.getCurrentPosition(
(position) => {
setLocation({
latitude: position.coords.latitude,
longitude: position.coords.longitude
})
},
(error) => {
// See error code charts below.
console.log(error.code, error.message);
},
{ enableHighAccuracy: false, timeout: 15000 }
);
})
}, []);
return (
<View style={styles.container}>
<MapView
style={styles.map}
provider={PROVIDER_GOOGLE}
region={{
latitude: 38.695794,
longitude: -101.807704,
// latitude: location.latitude,
// longitude: location.longitude,
latitudeDelta: 0.015,
longitudeDelta: 0.0121,
}}>
<Marker
coordinate={{
latitude: 38.695794,
longitude: -101.807704,
}}
/>
</MapView>
<GooglePlacesAutocomplete
style={styles.searchBar}
placeholder='Search Places'
query={{
key: GOOGLE_API_KEY,
language:'en'
}}
GooglePlacesDetailsQuery={{
fields:'geometry'
}}
/>
</View>
)
}
const styles = StyleSheet.create({
container:{
flex:1,
alignItems:'center',
justifyContent:'center',
backgroundColor:'#369',
paddingTop:5
},
map:{
left:0,
right:0,
top:0,
bottom:0,
position:'absolute'
},
searchBar:{
description:{
fontWeight:"bold"
},
predefinedPlacesDescription:{
color:"red"
},
textInputContainer:{
backgroundColor:'#369',
top:50,
width:screenWidth - 20,
borderWidth:0
},
textInput:{
marginLeft:0,
marginRight:0,
height:38,
color:'#5d5d5d',
fontSize:16,
borderWidth:0
},
listView:{
backgroundColor:'rgba(192,192,192,0.9)',
top:23
}
}
})
export default App
搜索栏太小了。但它还能用。单击搜索栏时,搜索栏的宽度等于屏幕宽度。当移除容器样式alignItems:'center'
时,这个问题就解决了。
样式也不申请搜索栏。
发布于 2022-04-18 10:32:30
搜索栏太小了。但它还能用。单击搜索栏时,搜索栏的宽度等于屏幕宽度。删除容器样式alignItems:'center'
时解决了该问题
样式也不应用于搜索栏。
发布于 2022-03-21 08:00:25
您是否尝试过在搜索组件之外添加视图?
也许就像下面
<View
style={{
position: "absolute",
zIndex: 1,
width: deviceWidth || "100%" || "custom width value" // with width
}}>
<GooglePlacesAutocomplete
style={styles.searchBar}
placeholder='Search Places'
query={{
key: GOOGLE_API_KEY,
language:'en'
}}
GooglePlacesDetailsQuery={{
fields:'geometry'
}}
/>
</View>
https://stackoverflow.com/questions/71526230
复制相似问题