Javascript 我能';i don’我的钩子没法用,我做错了什么?
我正在制作一个应用程序来比较GPS和惯性导航(INS)之间的电池使用情况,我已经制作了GPS应用程序。现在,当我制作这个INS应用程序的时候,我无法让逻辑按照我想要的方式工作。我不太习惯本地反应,但我认为我会有足够的经验,我不知道功能组件是新事物,我不能真正按照我想要的方式工作。早些时候,我使用maps update interval作为钩子的依赖项使它“工作”,但是map只能以最快的速度每秒更新一次,这对于我得到的用例来说有点太低了。所以我遇到的问题是,我更新值的钩子没有更新值。更新地理位置、加速计和陀螺仪值的挂钩可以工作,但是根据这些值计算速度和角度的挂钩根本不起作用Javascript 我能';i don’我的钩子没法用,我做错了什么?,javascript,android,react-native,Javascript,Android,React Native,我正在制作一个应用程序来比较GPS和惯性导航(INS)之间的电池使用情况,我已经制作了GPS应用程序。现在,当我制作这个INS应用程序的时候,我无法让逻辑按照我想要的方式工作。我不太习惯本地反应,但我认为我会有足够的经验,我不知道功能组件是新事物,我不能真正按照我想要的方式工作。早些时候,我使用maps update interval作为钩子的依赖项使它“工作”,但是map只能以最快的速度每秒更新一次,这对于我得到的用例来说有点太低了。所以我遇到的问题是,我更新值的钩子没有更新值。更新地理位置、
// Map Screen
const MapScreen = () => {
// Coordinates
const [latitude, setLatitude] = React.useState(0);
const [longitude, setLongitude] = React.useState(0);
// Toggle for map update
const [count, setCount] = React.useState(true);
//Center map on user
const [centerOnUser, setCenterOnUser] = React.useState(false);
// Measured coordinates
const [startLat, setStartLat] = React.useState(-1);
const [startLong, setStartLong] = React.useState(-1);
const [endLat, setEndLat] = React.useState(-1);
const [endLong, setEndLong] = React.useState(-1);
//INS Speed and Distance
const [speed, setSpeed] = React.useState(0);
const [distance, setDistance] = React.useState(0);
//INS Rotation and Direction
const [rotation, setRotation] = React.useState(0);
const [direction, setDirection] = React.useState(0);
// Init coords
React.useEffect(() => {
// Get initial Coordinates from GPS
Geolocation.getCurrentPosition(
(position) => {
setLatitude(position.coords.latitude);
setLongitude(position.coords.longitude);
},
(error) => {
console.log(error);
},
{ enableHighAccuracy: true }
);
// Subscribe to accelerometer data and measure speed and distance
const accelSub = accelerometer.subscribe(({ x, y, z }) => {
setSpeed(parseFloat((speed * 0.94 + ((Math.abs(x) + y + z) - 9.82) * 0.06).toFixed(2)));
});
//Subscribe to gyroscope data and measure direction and rotation
const gyroSub = gyroscope.subscribe(({ z }) => {
setRotation(parseFloat((rotation * 0.9 + z * 0.1).toFixed(2)));
});
return () => {
accelSub.unsubscribe();
gyroSub.unsubscribe();
}
}, []);
// Update coords on map on user movement
React.useEffect(() => {
const interval = setInterval(() => {
//Measures Acceleration from Accelerometer to measure Distance
setDistance(parseFloat((distance + (speed * 5)).toFixed(2)));
//Measures Rotation from Gyroscope to measaure Direction
setDirection(parseFloat((direction + (rotation * 5)).toFixed(2)));
}, 1000);
return () => {
clearInterval(interval);
};
}, []);
var myRegion = {
latitude: latitude,
longitude: longitude,
latitudeDelta: 0.2,
longitudeDelta: 0.2
};
// Returns View
return (
<View>
<View>
<Button title="Start measurement" onPress={() => {
setCenterOnUser(true);
setStartLat(latitude);
setStartLong(longitude);
}} />
<Button title="Stop measurement" onPress={() => {
setCenterOnUser(false);
setEndLat(-1);
setEndLong(-1);
}} />
<MapView
region={myRegion}
style={{ top: 0, left: 0, height: 450 }}
showsUserLocation={true}
userLocationUpdateInterval={1000}
onUserLocationChange={() => {
}}
/>
</View >
<View>
<Text>
Starting Coordinates:
</Text>
<Text>
Latitude: {startLat} Longitude: {startLong}
</Text>
<Text>
Ending Coordinates:
</Text>
<Text>
Latitude: {endLat} Longitude: {endLong}
</Text>
<Text>Distance: {distance}</Text>
<Text>Speed: {(speed * 3.6).toFixed(2)}</Text>
<Text>Rotation: {rotation}</Text>
<Text>Direction: {direction}</Text>
<Text>{centerOnUser ? 'measuring' : 'waiting'}</Text>
</View >
</View>
)
};
//地图屏幕
常量映射屏幕=()=>{
//坐标
常量[纬度,设置纬度]=React.useState(0);
const[longitude,setLongitude]=React.useState(0);
//切换地图更新
const[count,setCount]=React.useState(true);
//用户中心地图
常量[centerOnUser,setCenterOnUser]=React.useState(false);
//测量坐标
常量[STARTAT,setStartLat]=React.useState(-1);
const[startrong,setStartLong]=React.useState(-1);
const[endLat,setEndLat]=React.useState(-1);
const[endLong,setEndLong]=React.useState(-1);
//惯性导航系统速度和距离
const[速度,设置速度]=React.useState(0);
const[distance,setDistance]=React.useState(0);
//惯性导航系统的旋转和方向
const[rotation,setRotation]=React.useState(0);
const[direction,setDirection]=React.useState(0);
//初始坐标
React.useffect(()=>{
//从GPS获取初始坐标
Geolocation.getCurrentPosition(
(职位)=>{
设置纬度(位置坐标纬度);
设置经度(位置坐标经度);
},
(错误)=>{
console.log(错误);
},
{enableHighAccurance:true}
);
//订阅加速计数据并测量速度和距离
const accelersub=加速计。订阅({x,y,z})=>{
设定速度(parseFloat((速度*0.94+((数学绝对值(x)+y+z)-9.82)*0.06)。toFixed(2));
});
//订阅陀螺仪数据并测量方向和旋转
const gyroSub=陀螺仪订阅({z})=>{
setRotation(parseFloat((rotation*0.9+z*0.1).toFixed(2));
});
return()=>{
accelSub.unsubscribe();
gyroSub.unsubscribe();
}
}, []);
//在用户移动时更新地图上的坐标
React.useffect(()=>{
常量间隔=设置间隔(()=>{
//测量加速度计的加速度以测量距离
setDistance(parseFloat((距离+(速度*5)).toFixed(2));
//测量从陀螺仪到测量方向的旋转
设置方向(parseFloat((方向+(旋转*5)).toFixed(2));
}, 1000);
return()=>{
间隔时间;
};
}, []);
var myRegion={
纬度:纬度,
经度:经度,
纬度德尔塔:0.2,
纵向德尔塔:0.2
};
//返回视图
返回(
{
setCenterOnUser(真);
设置起点(纬度);
setStartLong(经度);
}} />
{
setCenterOnUser(错误);
setEndLat(-1);
setEndLong(-1);
}} />
{
}}
/>
起始坐标:
纬度:{startat}经度:{startong}
结束坐标:
纬度:{endLat}经度:{endLong}
距离:{Distance}
速度:{(速度*3.6).toFixed(2)}
旋转:{Rotation}
方向:{Direction}
{centerOnUser?正在测量:'waiting'}
)
};
您面临的问题是什么?问题是旋转和方向的值没有得到更新,虽然控制台中显示了console.log,但它们保持在0,但它们上的日志为0。“速度”和“旋转”的值会不断更新,这两个值在控制台日志和应用程序的渲染部分中都有效。我希望更新方向和旋转速度比我当前的解决方案快,该解决方案受到地图最低更新频率1000ms的限制