【问题标题】:Use sim network to detect user current location without GPS or internet in react native使用 sim 网络在没有 GPS 或互联网的情况下检测用户当前位置以做出本机反应
【发布时间】:2018-07-14 05:47:39
【问题描述】:

我想在没有互联网的情况下获取用户当前位置(仅在 android 中)并通过 SMS 发送。我尝试在本机反应中使用此代码:

navigator.geolocation.getCurrentPosition(
            (position) => {
                let initialPosition = JSON.stringify(position);
                console.log(initialPosition);
            },
            (error) => console.log(JSON.stringify(error)),
            {enableHighAccuracy: true, timeout: 15000, maximumAge: 2000 }
        );

但是这段代码给了我这个错误:

error : 3 , Location request timed out

我在谷歌和https://github.com/facebook/react-native/issues/ 搜索这个问题并尝试了很多方法来解决它,像这样:

enableHighAccuracy: false

或 删除选项参数

 {enableHighAccuracy: true, timeout: 15000, maximumAge: 2000 }

或 删除maximumAge 和当删除maximumAge 这个工作,但是当删除它 设备给我最后检测到的用户位置,可能远离用户当前位置

也可以试试这个库https://github.com/Agontuk/react-native-geolocation-service 最后一个解决方案并实现库示例代码: https://github.com/Agontuk/react-native-geolocation-service/tree/master/example

但是这段代码只适用于 WIFI,当 WIFI 是给我Time out 错误时。

我的问题是:

如何在没有 GPS 或 Internet 网络的情况下使用 sim 网络检测用户当前位置,在许多 Android 设备和 SDK 版本中都支持?

【问题讨论】:

  • 有什么突破吗?

标签: react-native gps


【解决方案1】:
  1. 不用删除maximumAge,直接填0(maximumAge: 0)。如果你删除它,那么最后一个位置将被缓存。

  2. 并且 enableHighAccuracy 设置为 false。

  3. 设置更多超时时间。

Geolocation

maximumAge (ms) - 是一个正值,表示可接受返回的可能缓存位置的最大年龄(以毫秒为单位)。如果设置为 0,则意味着设备不能使用缓存的位置,必须尝试检索真实的当前位置。如果设置为 Infinity,设备将始终返回缓存位置,无论其年龄如何。 默认为 INFINITY。

记得在 AndroidManifest.xml 文件中使用权限

Manifest.permission

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-05
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 2011-08-26
    • 2017-07-18
    相关资源
    最近更新 更多