【问题标题】:Checking if location services are enabled using react native检查是否使用本机反应启用了位置服务
【发布时间】:2016-08-15 01:53:06
【问题描述】:

我目前正在开发一个使用 react native 的应用程序,该应用程序的某些功能需要启用位置服务。我一直在研究是否有一种简单的方法来测试用户是否打开了它们,如果它们没有打开,我可以显示不同的视图。除了特定于 IOS 或 Android 的结果外,我没有找到任何结果。有其他人找到解决此问题的方法吗?

【问题讨论】:

标签: android ios geolocation react-native native


【解决方案1】:

你也可以试试这两个库:

https://github.com/c19354837/react-native-system-setting

https://github.com/efkan/react-native-system-settings

我有以下内容: 我收到此错误代码 1 或错误消息:“没有可用的位置提供程序。”当我的手机定位服务关闭时。

其他时候,当它超时时,我收到此错误代码 3 或错误消息:'位置请求超时。'。

因此,根据错误代码和消息,我显示不同的 toast 以通知用户。

可以参考MDN上的这个位置错误doc

【讨论】:

  • 我使用了第一个库:)。它工作正常,但如果我们不使用任何库会更好。
  • 它会持续检查设备的状态吗? @Jun711
【解决方案2】:

做一个watchPosition或getCurrentPosition,你的第二个参数是失败时的回调,它的参数是失败的原因。

所以,从那里你知道它何时以及为什么失败。例如,如果您只想对“位置已禁用”而不是“请求超时”采取行动,则需要一个 if。

例如,您可以使用回调将标志添加到您的状态。

【讨论】:

    【解决方案3】:

    使用android的“react native android location services dialog box”模块

    https://www.npmjs.com/package/react-native-android-location-services-dialog-box

    安装

    npm install react-native-android-location-services-dialog-box --save
    

    js

    import LocationServicesDialogBox from "react-native-android-location-services-dialog-box";
    
    LocationServicesDialogBox.checkLocationServicesIsEnabled({
      message: "Use Location ?",
      ok: "YES",
      cancel: "NO"
    }).then(function(success) {
      console.log(success); // success => "enabled"
    ).catch((error) => {
      console.log(error.message); // error.message => "disabled"
    });
    

    【讨论】:

    • IOS呢?
    【解决方案4】:

    我能够通过在无法检索位置时显示错误消息来解决此问题。

    (error) => {
            this.setState({servicesOn: false}),
            console.log(error);
          },
    

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 2016-04-24
      • 2015-12-02
      • 1970-01-01
      • 2011-11-05
      • 2014-08-22
      相关资源
      最近更新 更多