首页 > 解决方案 > 未处理的承诺拒绝类型错误 undefined is not a function near ...navigator.geolocation.getcurrentpositionasync...]

问题描述

我的应用程序请求访问用户在屏幕 A 中的位置的权限,因此他们的位置呈现在屏幕 B 中的地图上。导航到屏幕 B 时,我被承诺拒绝,并且地图未加载。

下面是我的代码:

旋转直到授予当前位置权限

const renderLoading = () =>  {
    if (isLoading) {
      return (         
          <View style = {styles.indicator}>
            <ActivityIndicator
              color = 'black'
              size = 'large'
              animated = {false}
            />
            <Text style = { styles.text }>{i18n.t('locate')}</Text>
          </View>
          );
          }else {
            return null;
          }
    }

获取地理位置的 UseFocusEffect

useFocusEffect( 
      React.useCallback(()=> {
        let isActive = true;
          
          const fetchGeoPosition = async() => {
             navigator.geolocation.getCurrentPositionAsync( //claims undefined is not a function
              position => { 
                if (isActive){
                  setUserLatitude(position.coords.latitude);
                  setUserLongitude(position.coords.longitude);
                  setPositionError(null);
                  console.log('Location Accessed')
                } 
                setIsLoading(false)
              }, 

              error => isActive && setPositionError(error.message),
              {enableHighAccuracy: true, timeout: 0, maximumAge: 1000} 
            ); 
          }

我如何部署到地图上

<View style = {styles.container}>
          {(renderLoading())}

            <View style = {styles.header}>
       
            <MapView
                provider={PROVIDER_GOOGLE}
                style={styles.map}
                region= {{
                  latitude: user_latitude,
                  longitude: user_longitude,
                  latitudeDelta: 0.1451,
                  longitudeDelta: 0.1451
                }}
            >
            <Marker 
              coordinate={ 
                {latitude: user_latitude,
                 longitude: user_longitude,
                 error: position_error,
                }}
            />
        </MapView>

标签: javascriptreact-nativereact-hooksgeolocationexpo-permissions

解决方案


推荐阅读