我试图通过使用react-native-maps中的“ coords”道具在我的React Native应用程序中使用用户的当前位置。
问题是,当我尝试console.log currentLocation.coords时,出现错误:
TypeError: null is not an object (evaluating 'currentLocation.coords')
但是当我console.log currentLocation时,我可以在响应中看到坐标道具。
Object {
"coords": Object {
"accuracy": 5,
"altitude": 5,
"altitudeAccuracy": 5,
"heading": 0,
"latitude": 13.184791,
"longtitude": 55.735991,
"speed": 0,
},
"timestamp": 1000000,
}
const locationReducer = (state, action) => {
switch (action.type) {
case 'add_current_location':
return { ...state, currentLocation: action.payload }
default:
return state;
}
};
const startRecording = dispatch => () => {};
const stopRecording = dispatch => () => {};
const addLocation = dispatch => (location) => {
dispatch({ type: 'add_current_location', payload: location })
};
export const { Context, Provider } = createDataContext(
locationReducer,
{ startRecording, stopRecording, addLocation },
{ recording: false, locations: [], currentLocation: null }
);
const Map = () => {
const { state: { currentLocation } } = useContext(LocationContext);
console.log(currentLocation.coords);
if (!currentLocation) {
return <ActivityIndicator size="large" style={{ marginTop: 200 }} />;
};
return (
<MapView
style={styles.map}
initialRegion={{
...currentLocation.coords,
latitudeDelta: 0.01,
longitudeDelta: 0.01
}}
>
</MapView>
);
};
const TrackCreateScreen = () => {
const { addLocation } = useContext(LocationContext);
const [err, setErr] = useState(null);
const startWatching = async () => {
try {
await requestPermissionsAsync();
await watchPositionAsync({
accuracy: Accuracy.BestForNavigation,
timeInterval: 1000,
distanceInterval: 10
}, (location) => {
addLocation(location);
});
} catch (e) {
setErr(e);
}
};
useEffect(() => {
startWatching();
}, []);
如何访问座标? (我需要使用经度和纬度)
答案 0 :(得分:0)
我已经使用'react-native-location'来获取用户的当前位置。在Auth0
中,我将按以下方式获取经度和纬度。
my_app_id
如您所见,我得到的位置数组可能与您的情况有关。请检查。因为数组也是对象。