简体   繁体   English

当应用程序是本机响应的后台时,如何更新服务中的经度和纬度

[英]How to Update latitude and longitude in service when app is background in react native

I'm try to update a location in server every 15 minute in react native. 我尝试每15分钟更新一次服务器中的位置。 So, how can I do that... 那么,我怎么能这样做......

I'm using react-native-maps for map api 我正在使用react-native-maps for map api

 componentDidMount() {
    navigator.geolocation.getCurrentPosition(
        position => {
            var lat = parseFloat(position.coords.latitude);
            var long = parseFloat(position.coords.longitude);
            var initialRegion = {
                latitude: lat,
                longitude: long,
                latitudeDelta: LATITUDE_DELTA,
                longitudeDelta: LONGITUDEDELTA
            };
            this.setState({ intialposition: initialRegion });
        },
        error => {
            console.error(error);
        },
        { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
    );

    this.watchID = navigator.geolocation.watchPosition(position => {
        var lat = parseFloat(position.coords.latitude);
        var long = parseFloat(position.coords.longitude);
        var LastRegion = {
            latitude: lat,
            longitude: long,
            latitudeDelta: LATITUDE_DELTA,
            longitudeDelta: LONGITUDEDELTA
        };

        this.setState({ intialposition: LastRegion });
    });
}

componentWillUnmount() {
    navigator.geolocation.clearWatch(this.watchID);
}

by this code you can get the user`s location and show it ,you can change the time duration by yourself 通过此代码,您可以获取用户的位置并显示它,您可以自己更改持续时间

I was struggling with the same issue for two days, you have to consider the following points. 我在同一个问题上苦苦挣扎了两天,你必须考虑以下几点。

1: If you come from native Android (using Java), you must be familiar with the concept of background services, unfortunately, it doesn't support in react-native so far. 1:如果你来自原生Android(使用Java),你必须熟悉后台服务的概念,不幸的是,到目前为止它还不支持react-native。

2: if you want to accomplish your task you have to do the same as me I mean using an authorized library. 2:如果你想完成你的任务,你必须和我一样,我的意思是使用授权的库。

3: after a lot of googling I used this library , which I think you need to use it. 3:经过大量谷歌搜索后,我使用了这个库 ,我认为你需要使用它。

4: and finally this library is not free for commercial use in android so you must pay for them, it's so easy and so powerful. 4:最后这个库在Android中不能免费用于商业用途,所以你必须为它们付费,它是如此简单和如此强大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM