首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Google对本机地图作出反应,搜索栏与屏幕宽度不一致

使用Google对本机地图作出反应,搜索栏与屏幕宽度不一致
EN

Stack Overflow用户
提问于 2022-03-18 11:16:28
回答 2查看 603关注 0票数 0

这是使用Google的API来响应本地地图的代码。

代码语言:javascript
运行
复制
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'时,这个问题就解决了。

样式也不申请搜索栏。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-18 10:32:30

搜索栏太小了。但它还能用。单击搜索栏时,搜索栏的宽度等于屏幕宽度。删除容器样式alignItems:'center'时解决了该问题

样式也不应用于搜索栏。

票数 0
EN

Stack Overflow用户

发布于 2022-03-21 08:00:25

您是否尝试过在搜索组件之外添加视图?

也许就像下面

代码语言:javascript
运行
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71526230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档