【问题标题】:I am unable to use geolocation in react-native with redux我无法在带有 redux 的 react-native 中使用地理定位
【发布时间】:2019-02-13 05:39:34
【问题描述】:

我在带有 redux 的 react-native 中使用地理定位,但我遇到了 2 个问题..

  1. 在我的 android studio 模拟器中显示错误的纬度和经度,我的位置是巴基斯坦拉合尔,但它显示的是美国位置。
  2. 当我签署了 apk 文件并将该 apk 文件安装到我的手机中时,它没有显示任何位置,为空。

谁能帮帮我?

这是我的代码..!

Reducer.js

let initialState = {
    lng: null,
    lat: null,
}

export default function prayerTimes(state = initialState, action) {
    switch (action.type) {

         case GET_LOCATION:
             return {
                ...state,
                lat: action.lat,
                lng: action.lng
         }

    }
}

Action.js

export function getLocation() {
    return dispatch => {
        navigator.geolocation.getCurrentPosition((position) => {
            dispatch({
                type: GET_LOCATION,
                lat: position.coords.latitude,
                lng: position.coords.longitude
            });
        });
    };
}

App.js

componentDidMount() {
    const { getLocation } = this.props;
    getLocation();
}

render() {
   const { lng, lat } = this.props;
   return (
       <View style={{justifyContent: 'center'}}>
            <Text style={{color: 'white'}}>Latitude: {lat}</Text>
            <Text style={{color: 'white'}}>Latitude: {lng}</Text>
       </View>
    );
}

【问题讨论】:

    标签: api react-native redux geolocation maps


    【解决方案1】:

    您是否尝试过启用高精度?

    navigator.geolocation.getCurrentPosition(
        (position) => {
            dispatch({
                type: GET_LOCATION,
                lat: position.coords.latitude,
                lng: position.coords.longitude
            });
        },
        (error) => {},
        { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
    );
    

    【讨论】:

    • 高精度选项会花费一些时间,请确保添加正确的超时值。
    • 当然可以,但是我可以将 maximumAge 从 1000 更改为 10,000 吗?
    • 不推荐。最大年龄就像一个缓存。如果您将其设置得太高,它将永远不会更新。
    • 当我在我的手机上安装这个应用程序时,它没有显示任何位置,就像空荡荡的世界一样
    【解决方案2】:

    以下代码可用于获取我当前的国家/地区名称。

    constructor(props) {
                super(props);
                this.state = {
                    lat: null,
                    long: null,
                };
              }
    
              componentDidMount ()  {    
                navigator.geolocation.getCurrentPosition(
                    (position) => {
                       var lat = parseFloat(position.coords.latitude);
                       var long = parseFloat(position.coords.longitude);
    
                       this.setState({ lat: lat, long: long });
                    },
                    (error) => {  },
                ); 
                }
    
                componentDidUpdate () {
    
                    var pos = {
                        lat: this.state.lat,
                        lng: this.state.long,
                    };
    
               Geocoder.geocodePosition(pos).then(res => {  // import Geocoder from 'react-native-geocoder';
                   AsyncStorage.setItem('location',res[0].country); // res[0].country gives you the country name of your location
              })
                    .catch(error => alert(error));
    
                }
    

    【讨论】:

    • 它在 react-native 中,我不知道如何将代码从 react-native 转换为 react-native redux..
    • 对不起。我从未使用过 Redux。
    猜你喜欢
    • 2018-11-29
    • 1970-01-01
    • 2023-03-20
    • 2021-11-22
    • 2016-10-26
    • 2016-11-08
    • 2016-01-25
    • 2019-06-18
    • 2017-02-27
    相关资源
    最近更新 更多