将当前位置更新为实时数据库未更新

时间:2021-05-18 10:47:57

标签: javascript firebase react-native firebase-realtime-database geolocation

我想使用 React-Native 为 Food Delivery App 制作驱动程序。当我将驱动程序的当前位置更新到 Firebase 实时数据库时,当前的纬度和经度状态在代码中仅更改一次,更新后它将再次更改为 Firebase 数据库中的默认值(“0”)。这是我的代码...

const App = () => {
var [lat, setLat] = useState(0)

useEffect(() => {
subscribeLocationLocation();
    const interval = setInterval(() => {
        firebase.database()
        .ref('/ongoing_orders/49')
        .update({
            latitude: lat,
        })
        .then(() => console.log('Data updated.'));  
    }, 6000);
    return () => clearInterval(interval);
}, []);

const subscribeLocationLocation =async() => {
        const _watchId = Geolocation.watchPosition(
        (position) => {
            console.log("...........")
            const lati = position.coords.latitude
            setLat(lati)
        },
        (error) => {
            setLocationStatus(error.message);
        },
        {
            enableHighAccuracy: true,
            distanceFilter: 0,
            interval: 5000,
            fastestInterval: 2000,
        },
        );

    return () => {
        if (_watchId !== null) {
            Geolocation.clearWatch(_watchId);
        }
    };

};

0 个答案:

没有答案