我尝试在redux的react-native中使用地理位置来显示用户的当前位置,但是页面中没有任何结果或值...有人可以帮我吗?
这是我的代码..
Reducer文件:
let initialState = {
coords: { lng: null, lat: null },
}
export default function prayerTimes(state = initialState, action) {
switch (action.type) {
case GET_LOCATION:
return Object.assign({},
state,
{
coords: action.payload
}
)
}
}
动作文件:
export function getLocation() {
return dispatch => {
const geolocation = navigator.geolocation;
geolocation.getCurrentPosition((position) => {
dispatch({
type: GET_LOCATION,
payload: position
});
});
};
}
主页:
render() {
const { getLocation, coords } = this.props;
return (
<View>
<Text>{getLocation}</Text>
<Text>Latitude: {coords.lng}</Text>
</View>
);
}
}